VMware Cloud Community
TheOnlyOne1986
Contributor
Contributor
Jump to solution

PowerCLI start multiple VM if poweredOff

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
TheOnlyOne1986
Contributor
Contributor
Jump to solution

it dosent work. the script will start vms the are already powered on.

2018-10-08 17_00_52-Window.png

On the screenshot you can see, there are some error messages.

The VM´s are poweredOn an i send a poweredOn again.

its strange.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
TheOnlyOne1986
Contributor
Contributor
Jump to solution

perfect it works. thank you very much for your support LucD

0 Kudos