Forum Discussion
Replace psexec with native powershell commands
Hi Lain,
thank you for your help on this. Both method seem to be working as for executing a remote code and using the value of a local variable as a parameter. So that's good.
Still, I decided to use your code because I trust more your knowledge than mine !! 😄
Now I have an odd issue.
On my remote code I basically run a vmware action (VM shutdown etc). Let's call it vmware.ps1
If I run vmware.ps1 script directly on the remote server => it works
If I make a call to the same script from a command (cmd.exe) window from the controler server => it works.
Now when using the controler script using the invoke-command from the controler server => the code is stuck when running the remote vmware command.
Controler' script :
...somecode....
$vm = myvm
Invoke-Command -ComputerName remoteserver -ArgumentList $vm -ScriptBlock {
param($LocalParam1);
C:\_Scripts\Pools_Scripts\vmware.ps1 "$LocalParam1" "shutdown";
}
...somecode...
vmware.ps1 script located on remoteserver
...somecode....
switch ($vmaction)
{
"shutdown" {
write-host "1"
if ($vmstate.State -eq "Running") {
write-host "2a"
Shutdown-VMGuest -VM $vmname -Confirm:$false #-InformationAction Ignore -ErrorAction Ignore
write-host "2b"
}
write-host "3"
}
...somecode...
When running the controler script my last screen return is "2a". Then nothing happens... while when run it locally or with my former cmd call with psexec, it's all fine.
Any idea? I tried to add a -informationaction and -erroraction to ignore but still the same. As an additionnal information, the command shutdown-vmguest normally returns some information :
PowerState : PoweredOff
Version : v13
HardwareVersion : vmx-13
Notes : blabla
Guest : vmname
NumCpu : 2
CoresPerSocket : 1
MemoryMB : 8192
.... etc
Any idea why this behavior would happen? it looks like it's waiting for a confirmation or something...
Out of curiosity, in line 2 from the controller's script, you assign a value to a variable named $var, but on line 3, you use a variable named $vm in the -Argumentlist.
Maybe this is valid (since we can't see all the code - which is fine), but if it's a mistake, then maybe that explains the pausing after 2a is written. That said, I'm really guessing here since I don't know how Shutdown-VMGuest behaves if you pass a bad value (including $null) for the -VM parameter since I don't use VMware (I'm a Hyper-V guy.)
Cheers,
Lain
- John_DodoOct 17, 2022Brass Contributor
Oh I'm so sorry, it's an error when I wrote it. I fixed $var to $vm
It's not the exact code only the important part of it. So the value passed is right and I can actually see it in the first informational outputs just before this vmware command is ran.I don't think it's specificaly related to beeing from vmware. I think it's more because this command does something particular. I don't know exactly how "far" I can go when using invoke-command, is there any limitations ?
Again, when I call the exact same script using psexec + parameters instead of invoke-command + parameters, both calls work but the second one get's stuck while the first one ends properly.
PS : Maybe I should say that the command returns this when running locally, it's normally not blocking as I don't use these parameters :
"WARNING: GuestId property is deprecated. Please use ConfiguredGuestId and RuntimeGuestId properties"
instead.- LainRobertsonOct 17, 2022Silver Contributor
I can't see anything wrong when looking at it from a PowerShell perspective.
If you comment out the call to Shutdown-VMGuest from your vmware.ps1 script and run it again, do you then see the 2b and 3 output?
As an aside, you should really have a "break;" statement after the Write-Host "3" in between the current lines 11 and 12, or else you'll possibly fall through that case block and into the next one (if there is one, that is.)
write-host "3"; break; }
This isn't going to help with your current issue, just figured I'd flag it to avoid future potential issues.
Cheers,
Lain
- John_DodoOct 17, 2022Brass Contributor
If I comment the line with the call to shutdown-vmguest it works all fine until the end of the script.