Reputation: 59
I am experimenting with the following script in Powershell ISE, but this returns an error when executed.
$computerName = Read-Host "Enter name of remote computer"
psexec \\"$computerName" cmd
The Read-Host part works fine, but when it moves to the psexec line it returns
Enter name of remote computer: Computer
psexec :
At line:2 char:1
+ psexec \\"$computerName" cmd
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
So it seems that the script is not passing the value of $computer. I have tried various " ' combinations to no avail.
Any help would be much appreciated, I'm rather novice at powershell scripting.
Upvotes: 0
Views: 814
Reputation: 249
To expand on some of the other answers, a method a coworker showed me for more complex psexec strings is as follows:
#Use Psexec to Allow all remote connections and Enable PSRemoting
$psexec = "psexec -accepteula \\"+$targetIP+" -u "+$localadmin+" -p "+'"'+$str+'"'+' -h powershell.exe "&{"Set-Item wsman:localhost\client\trustedhosts -Value * -Force" ; "Enable-PSRemoting -SkipNetworkProfileCheck -Force"}"'
invoke-command -ScriptBlock {cmd /c $args[0]} -Argumentlist $psexec
While the above code actually calls psexec on the remote machine then delivers two powershell commands, the problem of authentication causes all sorts of crazy errors in PS.
Hope this helps someone or the OP.
Upvotes: 1
Reputation: 4979
updated:
right, I tried this, seems to work:
$computerName = Read-Host "Enter Name of computer"
$cred = Get-Credential
psexec \\$computerName -u $cred.UserName -p $cred.GetNetworkCredential().Password ipconfig 2> $null #hide errors
Upvotes: 0
Reputation: 24545
You don't need to quote anything. PowerShell will quote automatically if needed.
psexec \\$computerName cmd
Upvotes: 0