programing

파워셸 스크립트에서 sccreate를 호출하는 방법

stoneblock 2023. 11. 5. 10:51

파워셸 스크립트에서 sccreate를 호출하는 방법

호출하고 싶습니다.sc create힘있는 대본에서.코드는 여기 있습니다.

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

이로 인해 다음과 같은 오류가 발생합니다.

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

무엇이 문제입니까?위치 논쟁이란 무엇입니까?

여기서의 문제는 그 문제가 아닙니다.sc실행 가능한오류가 말하듯이,sc로 결의합니다.Set-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

당신은 사용하고 싶을지도 모릅니다.-foperator 명령행 인수를 구성할 때 여기저기에 있는 성가신 따옴표 escaping 백틱을 피하기 위해:

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

PowerShell 및 VS Code Integrated Terminal에서 실패하는 동안 간단한 Command Prompt 콘솔에서 정확한 명령이 작동했습니다.사실 다scCommand Prompt as Administrator에서 명령을 실행해야 했습니다.

sc create MyService binPath= "C:\svc\sampleapp.exe"
sc start MyService

마티아스의 대본은 잘 작동하는 것으로 알고 있지만, 교조가 올린 대본은 잘 작동하지 않는 이유가 있습니다.그것은 gotchain sc.exe와 관련이 있습니다.

binPath 및 displayName의 equals 뒤에 있는 공백은 선택 사항이 아닙니다.

저에게는 이 토막글이 작동하지 않습니다.

$cmd="sc create `"$ServiceName`" binpath=`"$TargetPath`" displayname=`"$DisplayName`" "

그러나 이것은 다음과 같습니다.

$cmd="sc create `"$ServiceName`" binpath= `"$TargetPath`" displayname= `"$DisplayName`" "

이 문제는 제가 이것을 기억할 때까지 계속해서 저를 괴롭히기 위해 가끔 제기됩니다.

언급URL : https://stackoverflow.com/questions/33782385/how-do-i-invoke-sc-create-from-a-powershell-script