Reputation: 921
I my trying to pass a variable server name to use it with psexec, so I have a variable $hostname and i'd like to do
psexec \\$hostname
When I do this I am getting an error. What is the correct way to do this?
Upvotes: 0
Views: 3192
Reputation: 1
I have run into a similar issue passing $
variables into psexec cmds. You can use this in your case:
$hostname = Read-Host 'Enter host name' | psexec \\\\\$_ cmd
or for a list (and Powershell 7 - drop the parallel switch for 5.1 or 6)
$hostname = Get-Content "...list.txt" | Foreach-Object -Parallel { psexec \\\\$_ cmd }
Upvotes: 0
Reputation: 27463
This works for me at an elevated prompt:
$myhost = 'localhost'
.\psexec.exe \\$myhost cmd /c dir
Upvotes: 0
Reputation: 3784
Are you setting the $hostname variable? if not then you have to use $env:COMPUTERNAME
as Powershell is different to the standard CMD.
Upvotes: 0