JoeRod
JoeRod

Reputation: 921

How can I pass a variable using psexec?

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

Answers (4)

Robert Threlkeld
Robert Threlkeld

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

js2010
js2010

Reputation: 27463

This works for me at an elevated prompt:

$myhost = 'localhost'
.\psexec.exe \\$myhost cmd /c dir

Upvotes: 0

Musaab Al-Okaidi
Musaab Al-Okaidi

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

Joey
Joey

Reputation: 354576

You need to give psexec something to run, e.g. cmd or powershell.

Upvotes: 1

Related Questions