VMware Cloud Community
mazdajai
Contributor
Contributor

Argument with InvokeVMScript

I have two questions regarding InvokeVMscript - I have written a short script, but I couldn't code the quotes in the script. I tried to escape the quote but it didn't work. Ultimately I want to avoid typing the quotes on the arguments - myscript vmname "uname -a".

Part of the script:

param($vm,$script)

Invoke-VMScript -ScriptText $script -VM $vm -HostUser root -HostPassword $hostpasswd -GuestUser root -GuestPassword $guestpasswd

I found some code that can query vm tools status, how can I intergate them so Invoke-VMScript only execute if vmware tools is running?

Get-VM | select Name,
    @{N="Tools Status";E={
        if($_.Guest.Extensiondata.GuestState -eq "notRunning"){
            "Not running"        }
        else{
            $_.Guest.Extensiondata.ToolsVersionStatus.Replace("guestToolsNeedUpgrade","Out of date").Replace("guestToolsNotInstalled","Not installed").Replace("guestToolsCurrent","OK").Replace("guestToolsUnmanaged","Unmanaged")
        }
    }}
0 Kudos
1 Reply
mattboren
Expert
Expert

Hello, mazdajai-

For the first part, one way that you could pass the script string to the myScript.ps1 as a parameter would be to have the script text itself as a variable.  For example:

## store the script text for Invoke-VMScript in a variable
$strScriptTxt = 'uname -a'
## call the PowerShell script with the two parameters
myScript.ps1 myVMName $strScriptTxt

As for how to only try to run something on VMs that have Tools running, you could do something like:

Get-VM | ?{$_.ExtensionData.Guest.ToolsRunningStatus -eq "guestToolsRunning"} | %{
   
## do stuff here
    Invoke-VMScript -VM $_ -GuestCredential ... ## and so on
} ## end foreach-object

How does that do?

0 Kudos