VMware Cloud Community
apnetwork
Contributor
Contributor

Help with scripting.....

Hello Guys,

I wrote a script in PowerGUI but i need a little help... so normal the script should find any vm's with the description Converter. after that he should start it the Servers, wait 500 secounds, then update VM Tools, wait of the updates VM's and the shutdown the Servers again...

Connect-VIServer "IP" -User Administrator -Password "secret"

Start-VM | where-object { $_Notes -match "converter" }

sleep 500

Update-Tools | where-object { $_Notes -match "converter" }

sleep 300

Shutdown-VMGuest | where-object { $_Notes -match "converter" }

Sorry for i, is my first Script and i hope you can help me for make it a little bit simpler.... Smiley Happy

Reply
0 Kudos
7 Replies
xacolabril
Expert
Expert

It's a good script. Simple and easy.

You don't nedd nothing else. Smiley Happy

Best regards..

Xavier

VMware Certified Professional VCP3 and VCP4.

-


Si encuentras que esta o cualquier otra respuesta ha sido de utilidad, vótalas. Gracias.

If you find this or any other information helpful or correct, please consider awarding points. Thank you.

Xavier Colomé Abril. VMware Certified Professional VCP3, VCP4 and VCP5. [Si encuentras que esta o cualquier otra respuesta ha sido de utilidad, vótalas. Gracias.] [If you find this or any other information helpful or correct, please consider awarding points. Thank you.]
Reply
0 Kudos
LucD
Leadership
Leadership

I'm afraid some of the cmdlets you use will need a -Vm parameter.

One way of doing this would be to use the ForEach-Object (alias %) and loop through all the VMs one by one.

Something like this

Connect-VIServer "IP" -User Administrator -Password "secret"

Get-VM | where-object { $_.Notes -match "converter" } | % {
	Start-VM -VM $_
	sleep 500

	Update-Tools -VM $_
	sleep 300

	Shutdown-VMGuest -VM $_
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
apnetwork
Contributor
Contributor

thanks for the fast help... but how should i make the right -match searchstring?

I will find any VM's that have a "vConverter" String in the Notes section....

{ $_Notes -match ".converter.*" }

thanks for you help

Reply
0 Kudos
LucD
Leadership
Leadership

That's what the script does, or else I don't understand your question.

- Get-VM : gets all the virtual machines

- Where-Object: filters out specific virtual machines so that only the virtual machines with "Converter" in the Notes field will be passed to the ForEach-Object cmdlet

- The Foreach-Object cmdlet will take each of the passed virtual machines and will execute the script block against each virtual machine. In the script block the virtual machine is accessible through the $_ variable

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jeveenj
Enthusiast
Enthusiast

Hi,

Try this

 
“{$_.Notes -match "converter"}” instead of “{$_Notes -match "converter"}”

(using the dot between $_ and Notes)

Does this help you?

-If you found this information useful, please consider awarding points for Correct or Helpful.
Reply
0 Kudos
LucD
Leadership
Leadership

Thanks, missed that typo.

The script above has been corrected.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
apnetwork
Contributor
Contributor

jeeee, it works Smiley Happy

Thanks LucD and Jeveenj for you help

Reply
0 Kudos