VMware Cloud Community
wbrunc
Contributor
Contributor

Simple PowerCLI 6 stop-vmguest question

Can someone tell me why the following won't work. "stop-vmguest"  not able to see the value of $vm . Keeps saying it's null.

I can run the command manually with no problem  "stop-vmguest -vm host_01 -confirm:$true" .

Thanks,

wbrunc

Contents of C:\temp\test1.csv

"host_01"

"host_02"

-----------------------------

#start of script

$TEST_vms_to_shut_snap_on=import-csv c:\temp\test1.csv

foreach ($vm in $TEST_vms_to_shut_snap_on)

         {

           stop-vmguest -vm $vm -confirm:$true

          

           sleep 60

         }

Reply
0 Kudos
5 Replies
Prakas
Enthusiast
Enthusiast

Give a try with

Stop-VMGuest -VM (Get-VM $vm) -Confirm:$true

Reply
0 Kudos
wbrunc
Contributor
Contributor

That didn't work.

Reply
0 Kudos
Prakas
Enthusiast
Enthusiast

Ok.

Guess I figured out the problem.

Feed the input to csv file like below (firstline is a title)

VMname

server1

server2

and modify the script as below

Stop-VMGuest -VM (Get-VM $vm.VMname) -Confirm:$true

Reply
0 Kudos
Prakas
Enthusiast
Enthusiast

Alternatively you can just input the VM names in a text file

Contents on C:\temp\test1.txt

server1

server2

..

and below script block should do the trick

$TEST_vms_to_shut_snap_on=Get-Content C:\temp\test1.txt

foreach ($vm in $TEST_vms_to_shut_snap_on)

         {

           stop-vmguest -vm $vm -confirm:$true         

           sleep 60

         }

Reply
0 Kudos
wbrunc
Contributor
Contributor

Prakash,

Using the txt file  worked, thank you .

Bill

Reply
0 Kudos