2015-11-18 5 views
5

sc createをpowershellスクリプトから呼び出すとします。ここにコードがあります。powershellスクリプトからsc createを呼び出すにはどうすればいいですか?

Set-Content : A positional parameter cannot be found that accepts argument 'binpath='. 
At line:1 char:1 

問題はどのようなものです:次のエラーを与える

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 

?位置的な議論は何ですか?

+0

コマンドの=記号の後にはスペースを入れないでください。 – AutomatedOrder

+6

エラーが示す通り、 'sc'は' Set-Content'のエイリアスです。 'sc.exe'フルファイル名を使用する –

+1

PS4以降、' New-Service'コマンドレットを使うことができますhttps://technet.microsoft.com/en-us/library/hh849830(v=wps.630).aspx – sodawillow

答えて

7

この問題はsc実行ファイルでは発生しません。エラー状態として、scSet-Contentに解決されます。あなたがGet-Alias -Name scを発行する場合は、表示されます:

gal sc

、別名をバイパスするには(ファイル拡張子を含む)を実行可能ファイルの完全な名前を使用します。

PS C:\> sc.exe query wuauserv 

SERVICE_NAME: wuauserv 
     TYPE    : 20 WIN32_SHARE_PROCESS 
     STATE    : 4 RUNNING 
           (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN) 
     WIN32_EXIT_CODE : 0 (0x0) 
     SERVICE_EXIT_CODE : 0 (0x0) 
     CHECKPOINT   : 0x0 
     WAIT_HINT   : 0x0 

あなたがかもしれませんコマンドライン引数を組み立てるときに-f演算子を使用して、それらの厄介な引用エスケープバックティックがその場所全体を避けるようにします:

$CmdLine = 'sc.exe create "{0}" binpath= "{1}" displayname= "{2}" ' -f $ServiceName,$TargetPath,$DisplayName 
Execute-Command -Command $CmdLine