2013-03-19 59 views
0

因此,一段時間後,我編寫了一個Powershell腳本來解析IIS日誌,然後做一些事情(電子郵件,創建報告和其他漂亮的東西)。那麼我正在努力從C#控制檯應用程序中刪除它。在我發佈我的代碼之前,我只想澄清一件事,我想嘗試遠離日誌解析器來解析這個日誌的原因有很多的原因,但具體來說,爲什麼使用某些東西,當你可以寫一些你喜歡的東西; )。因此,這裏是我的代碼C#執行Powershell

PS腳本:

$t1 =(get-date).AddMinutes(-10) 
    $t2 =$t1.ToUniversalTime().ToString("HH:mm:ss") 
    $IISLogPath = "C:\inetpub\logs\LogFiles\W3SVC1\"+"u_ex"+(get-date).AddDays(-3).ToString("yyMMdd")+".log" 
    $IISLogFileRaw = [System.IO.File]::ReadAllLines($IISLogPath) 
    $headers = $IISLogFileRaw[3].split(" ") 
    $headers = $headers | where {$_ -ne "#Fields:"} 
    $IISLogFileCSV = Import-Csv -Delimiter " " -Header $headers -Path $IISLogPath 
    $IISLogFileCSV = $IISLogFileCSV | where {$_.date -notlike "#*"} 
    $tape = $IISLogFileCSV | Format-Table time,s-ip,cs-uri-stem | Out-Host 

C#應用程序迄今:

Runspace runSpace = RunspaceFactory.CreateRunspace(); 
     runSpace.Open(); 
     Pipeline pipeline = runSpace.CreatePipeline(); 
     pipeline.Commands.AddScript(@"D:\PS-Scripts\IIS\IISLogScan.ps1"); 
     Collection<PSObject> results = pipeline.Invoke(); 
     foreach (PSObject obj in results) 
     { 
      Console.WriteLine(results); 

     } 
     Console.ReadLine(); 

現在,當我運行我的應用程序,它只是坐在那裏犯規顯示任何東西,設置我的斷點和它表示在我的foreach中沒有任何東西可以顯示,我完全掛起因爲運行我的ps1腳本,它完美地工作並顯示很多行。任何人都可以提供的洞察力會很棒。

回答

1

嘗試在名爲.ps1文件中更改:

$global:tape =$IISLogFileCSV | Format-Table time,s-ip,cs-uri-stem 

,並在C#

pipeline.Commands.AddScript(@"D:\PS-Scripts\IIS\IISLogScan.ps1"); 
pipeline.Commands.AddScript("$tape"); 
pipeline.Commands.AddScript("out-string"); 

或名爲.ps1:

$tape =$IISLogFileCSV | Format-Table time,s-ip,cs-uri-stem 
$tape 

,並在C#

pipeline.Commands.AddScript(@"D:\PS-Scripts\IIS\IISLogScan.ps1"); 
pipeline.Commands.AddScript("out-string"); 
+0

不幸的是,這些嘗試沒有奏效 – jladd 2013-03-19 14:29:13

+0

@jladd究竟是什麼失敗了? – 2013-03-21 13:15:09