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?
sc
is an alias forSet-Content
. Use the full filenamesc.exe
New-Service
cmdlet technet.microsoft.com/en-us/library/hh849830(v=wps.630).aspx{ ... }
) is the preferred way to pass commands as arguments;Invoke-Expression
should be avoided.