【发布时间】:2017-09-08 14:54:27
【问题描述】:
所以我用 java 编写了 Windows update'r automat。为了从 Windows 服务器获取所需的数据,我正在使用 jPowerShell,并且在执行此脚本时遇到了奇怪的问题
Java 调用 PowerShell 脚本
PowerShell ps = PowerShell.openSession();
PowerShellResponse response;
response = ps.executeScript("C:\\Users\\Prezes\\Desktop\\IsUpdateToInstal.ps1");
System.out.println(response.getCommandOutput());
PowerShell 脚本
$pw = ConvertTo-SecureString 'password' -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentList domein\login, $pw
Enter-PSSession -ComputerName IP -Credential $cred
$UpdateSession = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session"))
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
$SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0")
$Critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" }
$important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" }
$other = $SearchResult.updates | where { $_.MsrcSeverity -eq $nul}
$totalUpdates = $($SearchResult.updates.count)
if($totalUpdates -gt 0)
{
$updatesToInstall = $true
}
else { $updatesToInstall = $false }
$other
$totalUpdates
$updatesToInstall
如果我在 PowerShell 标准控制台中逐行执行此脚本,则一切正常并返回正确的值。 但是当我在 PowerShell ISE 中逐行运行此脚本或通过 Java 运行时,我注意到这一行存在一些问题
$UpdateSession = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session"))
当我输入此行并按 Enter 时,我可以在 ISE 中看到“已经在运行命令,请稍候”,当我等待几分钟时,通信是相同的,没有任何变化,但是当我按 Enter 时,第二时间命令立即通过。如果现在我从他们那里运行其余的脚本,我工作得很好。
当我尝试在 ISE 中执行完整脚本时出现此错误
Exception form HRESULT: 0x80072EE2
At C:\Users\Prezes\Desktop\IsUpdateToInstal.ps1:6 char:1
+ $SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 a ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMExceptio
Java 给我 null 说我不能在引用 $UpdateSearcher 的 null 对象上运行方法
我是 PowerShell 的早期初学者,我正在使用的脚本是纯形式的,在谷歌中找到了一些示例。
【问题讨论】:
-
Java程序在什么账户下执行?
-
我的 Domein 帐户 Windows 10 家庭高级版
-
你能在另一台电脑上成功运行脚本吗?
-
星期一会测试
-
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()这行返回什么?如果它返回任何东西,请显示它返回什么$UpdateSearcher | get-member
标签: java powershell dcom windows-update