Reputation: 35
I am trying to run a command from the command line which I derived from a variable and it is not working. If I copy the output of the variable and run it from the command line it works, just not from within Powershell script
$psexec = "c:\sysinternalsSuite\psexec.exe"
$computer = "localhost"
$port = 5482
$urlacl_cmd = "$psexec \\$computer netsh http add urlacl url=http://+:$port/ user=everyone"
# tried both of the lines below; neither worked
invoke-command -scriptblock{$urlacl_cmd}
& $urlacl_cmd
output from the above results in this:
`
c:\sysinternalsSuite\psexec.exe \\localhost netsh http add urlacl url=http://+:5484/ user=everyone
& : The term 'c:\sysinternalsSuite\psexec.exe \\localhost netsh http add urlacl url=http://+:5484/ user=everyone' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Temp\add-websocket.ps1:11 char:3
+ & $urlacl_cmd
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (c:\sysinternals.../ user=everyone:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
`
If I just copy the output and past it in the Powershell prompt it works
c:\sysinternalsSuite\psexec.exe \\localhost netsh http add urlacl url=http://+:5484/ user=everyone
Upvotes: 0
Views: 268
Reputation: 35
thanks @js2010 I got it working how I wanted by doing the following:
param (
[String]$computer = $env:COMPUTERNAME,
[Parameter(mandatory=$true,Position=0)][int]$port,
$user = "everyone"
)
$psexec = "c:\sysinternalsSuite\psexec.exe"
$urlacl_cmd = "netsh http add urlacl url=http://+:$port/ user=$user"
& $psexec \\$computer netsh http add urlacl url=http://+:$port/ user=$user
It didn't work using the $urlacl_cmd var so I just put the command and it worked.
Upvotes: 1
Reputation: 27423
This is a common question. For the call operator, the arguments have to be in an array (or completely seperate), like $myargs. $args is reserved.
set-content -path file -value hi
$cmd = 'findstr'
$myargs = '/n','hi','file' # line number
& $cmd $myargs
1:hi
Upvotes: 0