C# execute Powershell
本文关键字:Powershell execute | 更新日期: 2023-09-27 18:04:49
所以不久前我写了一个Powershell脚本,它可以解析IIS日志,然后做一些事情(电子邮件,创建报告和其他漂亮的东西)。在我发布我的代码之前,我只是想澄清一件事,我想尝试远离日志解析器来解析这个日志,原因有很多,但具体来说,为什么要使用一些东西,当你可以写一些你喜欢的东西;)。这是我的代码
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# app迄今为止:
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");