[英]C# execute Powershell
所以不久前,我写了一个Powershell脚本,该脚本将解析IIS日志,然后做一些事情(发送电子邮件,创建报告和其他一些漂亮的东西)。 好吧,我正在努力从C#控制台应用程序中执行该任务。 在发布代码之前,我只想澄清一件事,我想尝试远离日志解析器来解析此日志原因,原因有很多,但有一个特别的原因,为什么在可以根据自己的喜好编写其他内容时使用某些内容; )。 所以这是我的代码
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脚本可以正常工作并显示很多行。 任何人都能提供的任何见解都将是很棒的。
尝试更改.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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.