2017-04-07 168 views
1

我想運行一個powershell腳本並將其輸出到我的asp.net站點。我已用一個非常簡單的腳本,該腳本唯一的命令是在asp.net網站上運行powershell腳本

Get-Service | Out-String

這個輸出工作到我的網站的一切,我預料

,但是當我使用的腳本其實我是想信息從它不輸出任何東西 我可以告訴它運行(或TRYS運行),因爲當我的網站擊中代碼,調用腳本它掛起約10秒。

我試圖運行的腳本

$user = "user" 
$token = "token" 

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token))) 

$result = Invoke-WebRequest -Method Get -Uri 'https://site.vsrm.visualstudio.com/defaultcollection/product/_apis/release/releases?definitionId=1&api-version=3.0-preview.2&$expand=environments' -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} 

$releaseArr = $result.Content | ConvertFrom-Json 
[System.Collections.ArrayList]$enviromentName = @() 
[System.Collections.ArrayList]$latestRelease = @() 


foreach($env in $releaseArr.value[0].environments) 
{ 
    $enviromentName.Add($env.name) | Out-Null 
} 


foreach($releaseValue in $releaseArr.value) 
{ 
    For($i = 0; $i -lt $enviromentName.Count; $i++) 
    { 
     if($latestRelease[$i] -eq $null) 
     { 
      foreach($release in $releaseValue.environments) 
      { 
       if($release.name -eq $enviromentName[$i] -and $release.status -eq "succeeded") 
       { 
        $latestRelease.Add($releaseValue.name) | Out-Null 
       } 
      } 
     } 
    } 
} 

For($i = 0; $i -lt $enviromentName.Count; $i++) 
{ 
    Write-Host $enviromentName[$i] " : " $latestRelease[$i] 
} 

我知道這個腳本運行和輸出,但有在該腳本一些代碼,會導致它無法正確輸出。

在我的asp.net網站的代碼我使用調用腳本是

ResultBox.Text = string.Empty; 

     // Initialize PowerShell engine 
     var shell = PowerShell.Create(); 

     // Add the script to the PowerShell object 
     shell.Commands.AddScript(@"C:\Users\user\Desktop\script.ps1"); 

     // Execute the script 
     var results = shell.Invoke(); 

     // display results, with BaseObject converted to string 
     // Note : use |out-string for console-like output 
     if (results.Count > 0) 
     { 
      // We use a string builder ton create our result text 
      var builder = new StringBuilder(); 

      foreach (var psObject in results) 
      { 
       // Convert the Base Object to a string and append it to the string builder. 
       // Add \r\n for line breaks 
       builder.Append(psObject.BaseObject.ToString() + "\r\n"); 
      } 

      // Encode the string in HTML (prevent security issue with 'dangerous' caracters like < > 
      ResultBox.Text = Server.HtmlEncode(builder.ToString()); 
     } 
+1

你爲什麼要使用PowerShell來調用從網站的API?您可以直接在應用程序中調用REST方法。 –

+0

我們已經有這個腳本創建並尋找一種方式,我們可以使用,而不是重新創建已經存在的腳本它 – Nick

+0

我建議你可以考慮一下這款包https://www.nuget.org/packages/Microsoft.VisualStudio.Services .Release.Client /,很簡單。 –

回答

0

改變「寫主機」到「寫 - 輸出」。寫主機僅輸出到交互式控制檯。

您可以在行動中看到這一點:

建立一個新的PowerShell文件,並添加寫主機聲明它:

[[email protected] temp]$ New-Item -Type File -Path .\example.ps1 -Force 
[[email protected] temp]$ Set-Content .\example.ps1 "Write-Host 'Hello World'" 

然後嘗試和一個變量設置爲腳本的結果:

[[email protected] temp]$ $what = .\example.ps1 
Hello World 
[[email protected] temp]$ $what 
[[email protected] temp]$ 

Hello World在腳本執行時顯示,但變量爲空。

現在改變它來寫輸出:

[[email protected] temp]$ Set-Content .\example.ps1 "Write-Output 'Hello World'" 
[[email protected] temp]$ $what = .\example.ps1 
[[email protected] temp]$ $what 
Hello World 

變量實際上包含什麼是應該到現在。

一個PowerShell中的基本規則是不使用寫主機除了將要交互式運行的腳本。 .NET需要輸出流中的結果而不是主機流。