1
votes

I am getting the following error :

This command cannot be run due to the error: The system cannot find the file specified. + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand + PSComputerName : XXXXXX.yyy.com

Here is the snippet of the code:

if (($ToolsStatus -eq 'toolsOk' -OR $ToolsStatus -eq 'toolsOld') -AND $VMVSSStatus -eq $null -AND $OperatingSystem -match "64-bit" ) {
            try{
                Copy-Item -Path ".\64bit\$ToolsVersion.exe" -Destination "\\$FQDN\c$\"  -Container -Recurse -Force -ErrorAction Stop
                "File $ToolsVersion.exe copied on $vmName" | Do-Log
                try {
                    Invoke-Command -ComputerName $FQDN -ScriptBlock { 
                        Start-Process "C:\$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r ADDLOCAL=all"' -Wait 
                        Start-Process "C:\$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r REMOVE=VSS"' -Wait
                        Start-Process "C:\$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r ADDLOCAL=VSS"' -Wait 
                        "Installation completed on $vmName" | Do-Log
                        taskkill /IM vmtoolsd.exe /F
                        "VMtools process killed on $vmName" | Do-Log
                        Start-Service -Name VMTools
                        "VMware Tools service was started on $vmName" | Do-Log
                    } 
                }
                catch [System.Management.Automation.RuntimeException]#PSRemotingTransportException
                {
                     "Unable to install on $vmName. Following error was encountered:" +$_.Exception.GetType().FullName | Do-Log
                }

Please help.

1

1 Answers

1
votes

I guess the $ToolsVersion variable is undefined in the scope of Invoke-Command, since the scope is executed on the remote machine.

Try:

Start-Process "C:\$Using:ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r ADDLOCAL=all"' -Wait 
            

instead.

From about_Remote_Variables:

USING LOCAL VARIABLES

You can also use local variables in remote commands, but you must indicate that the variable is defined in the local session.

Beginning in Windows PowerShell 3.0, you can use the Using scope modifier to identify a local variable in a remote command. ...

UPDATE 1:

To retrieve the logs from the remote you've to change your code to:

 Invoke-Command -ComputerName $FQDN -ScriptBlock { 
                    $computerName = $env:COMPUTERNAME
                    Start-Process "C:\$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r ADDLOCAL=all"' -Wait 
                    Start-Process "C:\$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r REMOVE=VSS"' -Wait
                    Start-Process "C:\$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r ADDLOCAL=VSS"' -Wait 
                    $logging =  @()
                    $logging = "$computerName: Installation completed on $vmName"
                    taskkill /IM vmtoolsd.exe /F
                    $logging += "$computerName: VMtools process killed on $vmName" 
                    Start-Service -Name VMTools
                    $logging += "$computerName: VMware Tools service was started on $vmName" 
                    $logging
                } | Do-Log

Here the array $logging will be serialized over the network. On your machine, it's deserialized, and every entry of the array is sent to your local pipeline.

Hope that helps.