You are on page 1of 3

Technical Note

Running Custom Scripts in the Guest Operating System with Invoke-VMScript


VMware vSphere PowerCLI 5.1 Release 1
InVMwarevSpherePowerCLIyoucanuseInvoke-VMScripttorunacustomscriptintheguestoperating systemsofyourvirtualmachines.WhenrunningInvoke-VMScriptdoesnotdelivertheexpectedresults,you candeterminewhenandwhereaproblemoccurredbyknowinghowInvoke-VMScriptworks. TolearnmoreaboutthefeaturesofInvoke-VMScript,runGet-Help Invoke-VMScript.

Step-By-Step Overview of Running Invoke-VMScript


WhenyourunInvoke-VMScript,thecmdletfollowsastepbystepprocedure. 1 2 Invoke-VMScriptdetectstheGuestInfodataobjectforthespecifiedvirtualmachine. FromtheGuestInfodataobject,Invoke-VMScriptverifiestherunningstatusandversionofVMware Toolsonthespecifiedvirtualmachine.Thecmdletalsodeterminestheguestoperatingsystemofthe virtualmachine. IfyouhavenotpassedavaluetotheScriptTypeparameter,Invoke-VMScriptdetectsthetypeofyour scriptautomatically.

WhentheguestoperatingsystemisWindows,Invoke-VMScriptrunsyourscriptasaPowerShell script. WhentheguestoperatingsystemisLinux,Invoke-VMScriptrunsyourscriptasaBASHscript.

BasedontheversionofyourVMwarevirtualizationsoftware,Invoke-VMScriptselectsaservicetorun yourscriptwith.

ForvCenterServer5.0andlaterorESX/ESXi5.0andlater,Invoke-VMScriptrunsyourscript throughtheVIMservice. ForearliervCenterServerandESX/ESXiversions,Invoke-VMScriptrunsyourscriptthroughthe VIXcomponent.TheVIXcomponentisincludedwithPowerCLI. NOTETheVIXcomponentdoesnotsupportSSPIforguestcredentials.

5 6

Invoke-VMScriptwaitsforVMwareToolstoconfirmthatthevirtualmachineispoweredon. AfterVMwareToolsconfirmsthatthevirtualmachineispoweredon,Invoke-VMScriptauthenticates withtheguestoperatingsystem.Thecmdletusestheauthenticationcredentialsyouprovidedwhenyou calledInvoke-VMScript.

VMware, Inc.

Running Custom Scripts in the Guest Operating System with Invoke-VMScript

7 8 9

Invoke-VMScriptcreatesaTEMPfileinthevirtualmachine.ThecmdletusestheTEMPfiletostorethe outputofthescript. Basedonthescripttype,Invoke-VMScriptmodifiesthescriptandredirectstheoutputtotheTEMPfile. Basedontheguestoperatingsystem,Invoke-VMScriptselectsascriptinterpreter.


WhentheguestoperatingsystemisWindows,thescriptinterpreteriscmd.exe. WhentheguestoperatingsystemisLinux,thescriptinterpreteris/bin/bash.

10 11 12 13 14

Invoke-VMScriptrunsthescriptinterpreterasaseparateprocessintheguestoperatingsystemand passesthemodifiedscripttotheprocess. Whentheprocesscompletes,Invoke-VMScriptcopiestheTEMPfilefromthevirtualmachinetothe defaulttemporaryfolderontheWindowssystemwherevSpherePowerCLIisrunning. Invoke-VMScriptreadsthecontentsofthelocalcopyoftheTEMPfileandstorestheminavariable. Invoke-VMScriptdeletesthecopiesoftheTEMPfilefromthelocalandthevirtualmachine. Invoke-VMScriptreturnstheoutputofthescript.

Running a PowerShell Script with Invoke-VMScript


WhenyourscriptisaPowerShellscript,Invoke-VMScriptusescmd.exetostartPowerShell. Invoke-VMScriptmodifiesthePowerShellscripttoescapethespecialcharactersthatthescriptcontains.In thescriptinterpreter,thecmdletrunspowershell.exe -Command.Invoke-VMScriptpassesthemodified scripttotheCommandparameter. Toredirectthescriptoutput,Invoke-VMScriptencodesthepowershell.exe -Commandcommandand passesittoanotherPowerShellprocess. TomodifyyourPowerShellscript,Invoke-VMScriptescapesthespecialcharactersthatthescriptcontainsin aparticularorder. 1 2 3 Asinglequotationmark()isreplacedwithadoublequotationmark(). Astringofabackslashandadoublequotationmark(\)isreplacedwithastringoftwobackslashesand adoublequotationmark(\\). Adoublequotationmark()isreplacedwithastringofabackslashandadoublequotationmark(\).

Running a BAT Script with Invoke-VMScript


WhenyourscriptisaBATscript,Invoke-VMScriptusescmd.exetorunthescript.Invoke-VMScript modifiestheBATscripttoescapethespecialcharactersthatthescriptcontains.Thecmdletsurroundsthe modifiedscriptwithdoublequotationmarks()andpassesittothescriptinterpreter. TomodifyyourBATscript,Invoke-VMScriptescapesonlythespecialcharactersthatindicateanewline.A stringforanewline(\r\n\or\n)isreplacedwithastringofaspace,anampersand,andaspace(&).

VMware, Inc.

Running Custom Scripts in the Guest Operating System with Invoke-VMScript

Running a BASH Script with Invoke-VMScript


WhenyourscriptisaBASHscript,Invoke-VMScriptuses/bin/bashtorunthescript.Invoke-VMScript modifiestheBASHscripttoescapethespecialcharactersthatthescriptcontains.Thecmdletsurroundsthe modifiedscriptwithstringsofabackslashandadoublequotationmark(\)andpassesittothescript interpreter. TomodifyyourBASHscript,Invoke-VMScriptescapesthespecialcharactersthatthescriptcontainsina particularorder. 1 2 3 Astringforanewline(\r\n)isreplacedwithastringofaspace,asemicolon,andaspace(;). Astringofabackslashandadoublequotationmark(\)isreplacedwithastringofasinglequotation mark,adoublequotationmark,andasinglequotationmark(). Adoublequotationmark()isreplacedwithastringofthreebackslashesandadoublequotationmark (\\\).

If you have comments about this documentation, submit your feedback to: docfeedback@vmware.com VMware, Inc. 3401 Hillview Ave., Palo Alto, CA 94304 www.vmware.com Copyright 20092012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents. VMware is a registered trademark or trademark of VMware, Inc. in the United States and/or other jurisdictions. All other marks and names mentioned herein may be trademarks of their respective companies. Item: EN-000909-00

You might also like