C#执行Powershell
所以不久前我写了一个Powershell脚本,可以解析IIS日志,然后做一些事情(发送电子邮件、创建报告和其他漂亮的事情)。嗯,我正在从一个C#console应用程序中执行该操作。在我发布我的代码之前,我只想澄清一件事,我想试着远离日志解析器来解析这个日志,原因有很多,但有一个特别的原因,当你可以写你喜欢的其他东西时,为什么要使用它;)。这是我的代码 PS脚本:C#执行Powershell,c#,powershell,C#,Powershell,所以不久前我写了一个Powershell脚本,可以解析IIS日志,然后做一些事情(发送电子邮件、创建报告和其他漂亮的事情)。嗯,我正在从一个C#console应用程序中执行该操作。在我发布我的代码之前,我只想澄清一件事,我想试着远离日志解析器来解析这个日志,原因有很多,但有一个特别的原因,当你可以写你喜欢的其他东西时,为什么要使用它;)。这是我的代码 PS脚本: $t1 =(get-date).AddMinutes(-10) $t2 =$t1.ToUniversalTime().
$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();
Runspace Runspace=RunspaceFactory.CreateRunspace();
Open();
Pipeline Pipeline=runSpace.CreatePipeline();
pipeline.Commands.AddScript(@“D:\PS Scripts\IIS\IISLogScan.ps1”);
收集结果=pipeline.Invoke();
foreach(结果中的PSObject对象)
{
控制台写入线(结果);
}
Console.ReadLine();
现在,当我运行我的应用程序时,它只是坐着不显示任何内容,我设置了断点,它说在我的foreach中没有显示任何内容,我完全挂断了,因为运行我的ps1脚本,它工作得很好,显示了许多行。任何人能提供的任何见解都将是伟大的 尝试在.ps1文件中更改:
$global:tape =$IISLogFileCSV | Format-Table time,s-ip,cs-uri-stem
在c中#
或在.ps1中:
$tape =$IISLogFileCSV | Format-Table time,s-ip,cs-uri-stem
$tape
在c中#
@到底什么失败了?
$tape =$IISLogFileCSV | Format-Table time,s-ip,cs-uri-stem
$tape
pipeline.Commands.AddScript(@"D:\PS-Scripts\IIS\IISLogScan.ps1");
pipeline.Commands.AddScript("out-string");