I want to invoke sc create
from a powershell script. Here is the code.
function Execute-Command
{
param([string]$Command, [switch]$ShowOutput=$True)
echo $Command
if ($ShowOutput) {
Invoke-Expression $Command
} else {
$out = Invoke-Expression $Command
}
}
$cmd="sc create `"$ServiceName`" binpath=`"$TargetPath`" displayname=`"$DisplayName`" "
Execute-Command -Command:$cmd
which gives the following error:
Set-Content : A positional parameter cannot be found that accepts argument 'binpath=...'.
At line:1 char:1
What is the problem? What are positional arguments?
The exact command worked for me from a simple Command Prompt console while it fails on PowerShell and VS Code Integrated Terminal. In fact, all
sc
commands had to be run from Command Prompt as Administrator.The issue here is not with the
sc
executable. As the error states,sc
resolves toSet-Content
. If you issueGet-Alias -Name sc
, you'll see:To bypass the alias, use the full name of the executable (including the file extension):
You might want to use the
-f
operator when constructing your command line arguments, to avoid those annoying quote-escaping back ticks all over the place: