0

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

1
  • The args have to be an array. Commented Nov 3, 2022 at 19:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.