2014-10-15 3 views
0

Я пытаюсь создать скрипт powershell, содержащий инструкцию (IF, Else). В основном из-за проблем с совместимостью в том, как различные версии называют веб-запросы.Powershell 2.0 - Conditional (Else) не распознается как имя командлета, функция

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

&

V > 2.0 : Uses Invoke-WebRequest 

Вот мой код:

$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}; 

А вот ошибка, которую я получаю от Powershell об-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 ty над ключевым словом 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}; 
Смежные вопросы