Hello,
i will create a script that start all vm´s there are offline.
The Output will be send via mail.
The Script work basicly
But if one of the vm´s is poweredOff that script say "VM is running"
i Think there is a problem with the foreach loop.
i dont now where, can you help me?
Behind "VM1**" are round about 60VM´s
$VMS = "VM1**"
$VirtualVC = "vcenter.company.com"
$AllowMultiple = Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false
$MyHosts | Foreach {
$connection = Connect-VIServer $VirtualVC
}
$vm = Get-VM $VMS
$output =
$vm | foreach {
if ($vm.PowerState -eq "PoweredOff") {
Write-Output "Starting $($_.name) on $($_.VMHost)"
$StaringVMs = Start-VM $_ -Confirm:$false -RunAsync
sleep 7
}
else {Write-Output "$($_.name) is already running on $($_.VMHost)"
sleep 0}
}
$body = $output|Out-String;
Send-MailMessage -To me@company.com -from admin@company.com -Subject Reporting -Body $body -SmtpServer mailserver.company.com
My mistake, I didn't notice that the $vm variable was used instead of the $_.
Try with this version
$VMS = "VM1**"
$VirtualVC = "vcenter.company.com"
$AllowMultiple = Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false
$connection = Connect-VIServer $VirtualVC
$vm = Get-VM $VMS
$output = $vm | foreach {
if ($_.PowerState -eq "PoweredOff") {
"Starting $($_.name) on $($_.VMHost)"
$StaringVMs = Start-VM $_ -Confirm:$false -RunAsync
sleep 7
}
else {
"$($_.name) is already running on $($_.VMHost)"
sleep 0
}
}
$body = $output | Out-String;
Send-MailMessage -To me@company.com -from admin@company.com -Subject Reporting -Body $body -SmtpServer mailserver.company.com
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
To capture the output stream just place the strings in the pipeline.
They will all be collected in the $output variable.
$VMS = "VM1**"
$VirtualVC = "vcenter.company.com"
$AllowMultiple = Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false
$connection = Connect-VIServer $VirtualVC
$vm = Get-VM $VMS
$output = $vm | foreach {
if ($vm.PowerState -eq "PoweredOff") {
"Starting $($_.name) on $($_.VMHost)"
$StaringVMs = Start-VM $_ -Confirm:$false -RunAsync
sleep 7
}
else {
"$($_.name) is already running on $($_.VMHost)"
sleep 0
}
}
$body = $output | Out-String;
Send-MailMessage -To me@company.com -from admin@company.com -Subject Reporting -Body $body -SmtpServer mailserver.company.com
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
it dosent work. the script will start vms the are already powered on.
On the screenshot you can see, there are some error messages.
The VM´s are poweredOn an i send a poweredOn again.
its strange.
My mistake, I didn't notice that the $vm variable was used instead of the $_.
Try with this version
$VMS = "VM1**"
$VirtualVC = "vcenter.company.com"
$AllowMultiple = Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false
$connection = Connect-VIServer $VirtualVC
$vm = Get-VM $VMS
$output = $vm | foreach {
if ($_.PowerState -eq "PoweredOff") {
"Starting $($_.name) on $($_.VMHost)"
$StaringVMs = Start-VM $_ -Confirm:$false -RunAsync
sleep 7
}
else {
"$($_.name) is already running on $($_.VMHost)"
sleep 0
}
}
$body = $output | Out-String;
Send-MailMessage -To me@company.com -from admin@company.com -Subject Reporting -Body $body -SmtpServer mailserver.company.com
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
perfect it works. thank you very much for your support LucD