我在c#代码中使用以下powershell脚本:
Runspace runSpace = RunspaceFactory.CreateRunspace();
Pipeline pipeline = runSpace.CreatePipeline();
runSpace.Open();
string script = @"
$Session = New-Object -ComObject 'Microsoft.Update.Session'
$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
$UpdateHistory = $Searcher.QueryHistory(0, $historyCount)
$KBs = @()
foreach ($Update in $UpdateHistory) {
[regex]::match($Update.Title,'(KB[0-9]{6,7})').value | Where-Object {$_ -ne ""} | foreach {
$KB = New-Object -TypeName PSObject
$KB | Add-Member -MemberType NoteProperty -Name KB -Value $_
$KB | Add-Member -MemberType NoteProperty -Name Title -Value $Update.Title
$KB | Add-Member -MemberType NoteProperty -Name Description -Value $Update.Description
$KB | Add-Member -MemberType NoteProperty -Name Date -Value $Update.Date
$KBs += $KB
}
}
$KBs | Select Title,KB,Date";
pipeline.Commands.AddScript(script);
Collection<PSObject> results = pipeline.Invoke();
该脚本在PowerShell中运行良好。但是在c#代码中使用它时,它会返回提到的错误。我已经浏览了谷歌提供的各种相关帖子,但直到现在都没有运气。