파워셸 스크립트에서 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
, 알게 될 것입니다.
별칭을 무시하려면 파일 확장명을 포함하여 실행 파일의 전체 이름을 사용합니다.
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
operator 명령행 인수를 구성할 때 여기저기에 있는 성가신 따옴표 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 콘솔에서 정확한 명령이 작동했습니다.사실 다sc
Command 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
'programing' 카테고리의 다른 글
MySQL의 원시 바이너리 플로트 데이터 액세스 (0) | 2023.11.05 |
---|---|
C 표준은 n개 요소 배열의 크기가 요소 크기의 n배가 되도록 요구합니까? (0) | 2023.11.05 |
새 변수에서 각도 변수로 변경 - 계산된 변수 (0) | 2023.11.05 |
LESS-CSS 덮어쓰기 calc() 사용 안 함 (0) | 2023.11.05 |
C# XML 문서 웹사이트 링크 (0) | 2023.11.05 |