Hi There,
I'm I've been using Powershell more and more lately and at my current job they've PowerCLI available, oh boy!
I've been busy with creating a script over the weekend that works with a menu, which my fellow sys-admins can use to snapshot their VMs.
I've been having a certain issue, but I don't now what I’m doing wrong. I hope someone can help me or tell me what I’m doing wrong.
The function can read a .csv with some server names and it does correctly make a snapshot with the given name. But now I want to give me feedback on what he just did. I want feedback on what machines just received a snapshot. I've been using multiple commands, but nothing seems to work. Whenever I run the function it just doesn’t output anything, not even an error.
Whenever I run it in the shell it self with out using any function : get-vm | Get-Snapshot | select VM, Created, Description, SizeGB | sort Created | ft -au I’ll get the result I want it shows me the latest snapshots I just created.
vmware.csv content:
Name
S-test01
S-test02
S-test03
Earlier defined variables:
$svice03 = "s-vice03.test.int"
$serversvice3= Import-Csv "C:\temp\vmware.csv"
This is the function:
function vmsnapvice03
{
Connect-viserver $svice03
cls
write-host "Connected to the S-Vice03"
$serversvice3 | %{Get-VM $_.Name | New-Snapshot -Name "Pre-Patching" -Description "Before Patching" -Quiesce:$false -Confirm:$false}
sleep 5
get-vm | Get-Snapshot | select VM, Created, Description, SizeGB | sort Created| ft -au
# write-host "Created snapshot for " $serversvice3
}
I've been also trying to use:
write-host "Created a snapshot for " $serversvice3
Which works but it print it out in a ugly format: Created snapshot for @ {name=s-Apps-18} , If I could make this just s-Apps18 I would be happy. But then again, what am I doing wrong in the function that it doesn’t work with get-vm.
Additional information, we're using PowerCLI 6.5 release 1 build 4624819
Thanks for reading and thinking with me.
You are confusing the PowerShell output engine by mixing Write-Host and Format-Table.
Try adding an Out-Default to that line
select VM, Created, Description, SizeGB |
sort VM | Format-Table -AutoSize | Out-Default
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You are confusing the PowerShell output engine by mixing Write-Host and Format-Table.
Try adding an Out-Default to that line
select VM, Created, Description, SizeGB |
sort VM | Format-Table -AutoSize | Out-Default
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the quick reply LucD,
Now it's looking like :
function vmsnapvice03
{
Connect-viserver $svice03
cls
write-host "Connected to the S-Vice03"
$serversvice3 | %{Get-VM $_.Name | New-Snapshot -Name "Pre-Patching" -Description "Before Patching" -Quiesce:$false -Confirm:$false}
sleep 5
$serversvice3 | %{Get-VM $_.Name | Get-Snapshot | select VM, Created, Description, SizeGB | sort created | Out-Default}
}
And it's working like a charm. Cheers, have a good weekend!