2014-10-15 3 views
0

(IF, Else) 문을 포함하는 powershell 스크립트를 생성하려고합니다. 주로 서로 다른 버전이 웹 요청을 호출하는 방식의 호환성 문제로 인해 발생합니다. 여기 &Powershell 2.0 - 조건부 (Else)가 cmdlet의 이름으로 인식되지 않는 함수

V > 2.0 : Uses Invoke-WebRequest 

V < 2.0 : Uses [System.Net.WebRequest] for HTTP Requests 

내 코드입니다 :

$pcName="$ENV:COMPUTERNAME"; 
$psVers=$host.Version.Major; 

if ($psVers -le 2) 

      {$request = [System.Net.WebRequest]::Create('http://xxx.co.kr'); 
      $request.Method = "POST"; $request.ContentType = "application/x-www-form-urlencoded"; 
      $bytes = [System.Text.Encoding]::ASCII.GetBytes("pcName=$pcName"); 
      $request.ContentLength = $bytes.Length; $requestStream = $request.GetRequestStream(); 
      $requestStream.Write($bytes, 0, $bytes.Length); $requestStream.Close(); 
      $request.GetResponse();} 

Else 
      {$POSTdata = @{pcName=$pcName} 
      Invoke-WebRequest -Uri http://xxx.co.kr -Method POST -Body $POSTdata}; 

그리고 여기에 내가 파워 쉘의 V-2.0에서 수신하고 오류입니다 :

The term 'Else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelli 
ng of the name, or if a path was included, verify that the path is correct and try again. 
At line:1 char:5 
+ Else <<<< 
    + CategoryInfo   : ObjectNotFound: (Else:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

답변

0

제거 emp Else 키워드 위의 줄 :

$pcName="$ENV:COMPUTERNAME"; 
$psVers=$host.Version.Major; 

if ($psVers -le 2) 

      {$request = [System.Net.WebRequest]::Create('http://xxx.co.kr'); 
      $request.Method = "POST"; $request.ContentType = "application/x-www-form-urlencoded"; 
      $bytes = [System.Text.Encoding]::ASCII.GetBytes("pcName=$pcName"); 
      $request.ContentLength = $bytes.Length; $requestStream = $request.GetRequestStream(); 
      $requestStream.Write($bytes, 0, $bytes.Length); $requestStream.Close(); 
      $request.GetResponse();} 
Else 
      {$POSTdata = @{pcName=$pcName} 
      Invoke-WebRequest -Uri http://xxx.co.kr -Method POST -Body $POSTdata}; 
관련 문제