2017-12-23 15 views
0

내가 PowerShell 스크립트 실행하여 IIS 응용 프로그램 풀을 중지 다음과 같은 코드가 있습니다C# 코드에서 PowerShell 스크립트를 실행하는 경우 인식되지

 using (PowerShell ps = PowerShell.Create()) 
     { 
      ps.AddScript("Set-ExecutionPolicy -Scope LocalMachine Unrestricted"); 
      string stopCommand = File.ReadAllText($"{scriptPath}StopIISPool.ps1");     
      ps.AddScript(stopCommand); 
      ps.AddScript($"StopIISAppPool -StopScriptPath {scriptPath}IISStopCommand.ps1 -poolName {paths.IISAppPoolName}"); 
      ps.Invoke(); 

      Collection<ErrorRecord> errors = ps.Streams.Error.ReadAll(); 
     } 

그리고 여기 StopIISPool.ps1는 다음과 같은 파워 쉘 스크립트가 포함를 :

function StopIISAppPool 
    { 
     Param(
     [parameter(Mandatory=$true)] 
     [string]$stopScriptPath, 
     [parameter(Mandatory=$true)] 
     [string]$poolName 
     ) 
     process 
     { 
      PowerShell.exe -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -windowstyle hidden -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -noexit -File $stopScriptPath -poolName $poolname' -Verb RunAs}"; 
     } 
    } 

그리고 여기 stopScriptPath는 관리자 권한으로 오류없이 실행 다음 명령의 경로입니다 :

Param(
     [parameter(Mandatory=$true)] 
     [string]$poolName 
    ) 
    Stop-WebAppPool -Name $poolName; 

위의 C# 코드는 ASP.NET 핵심 응용 프로그램에서 실행됩니다. 나는 윈도우 서버 2012 R2에 IIS에서 응용 프로그램을 배포하고 내가 ps.Invoke에() 라인을 다음과 같은 오류가 실행할 때 :

시작 - 프로세스가 cmdlet에, 기능, 스크립트의 이름으로 인식되지 않습니다 파일 또는 작동 가능한 프로그램.

누군가 그 이유를 설명해 주시겠습니까?

+0

powershell의 버전은 무엇입니까? powershell 윈도우에 $ PSVersionTable.PSVersion을 입력하십시오. – P6345uk

+0

메이저 5, 마이너 1, 빌드 16299, 리비전 98 이것은 내 Windows 10 Pro의 powershell 버전이며, 위의 코드는 모두 잘 작동합니다. 그것은 윈도우 서버 2012 R2에서 실행할 때 오류가 발생합니다. PowerShell 버전이 더 오래되었다고 추측합니다. 나중에 버전을 쓸 것입니다. – Aspram

+0

PowerShell 버전은 4.0입니다. PowerShell에서 스크립트를 실행하면 작동하지만 C#에서 동일한 스크립트를 실행하면 오류가 발생합니다. PowerShell 버전에서 문제가 발생할 수 있습니까? – Aspram

답변

0

이 문제가 해결되었습니다. 실제로 해결 방법을 찾았습니다. IIS 응용 프로그램 풀을 조작하기 위해 PowerShell 대신 Windows 명령 줄로 전환했습니다. 다음 명령을 사용했습니다 :

C:\Windows\System32\inetsrv\appcmd start apppool /apppool.name:$poolName.