使用 Powershell 的 Invoke-Command 运行 Windows 更新时访问被拒绝

问题描述 投票:0回答:1

我一直在尝试设置一个 Powershell 模块,该模块可以使用 Invoke-Command 在服务器上远程调用 Windows/Microsoft 更新,然后处理更新,并将所有内容发送回调用服务器,以便它可以发送电子邮件报告。

当我尝试调用下载程序时,出现了问题:Powershell 似乎正在请求远程计算机上的提升权限。

这是我尝试运行但失败的片段:

Invoke-Command -ComputerName $Server -Credential $Credentials -ScriptBlock {
    $UpdateSession = New-Object -ComObject "Microsoft.Update.Session"
    Write-Progress -Activity "Updating" -Status "Checking for new updates"
    $Criteria = "IsInstalled=0 and Type='Software'"
    $Updates = $UpdateSession.CreateUpdateSearcher().Search($Criteria).updates
    $Downloader = $UpdateSession.CreateUpdateDownloader()
    $Downloader.Updates = $Updates
}

我知道问题不在于远程处理,因为前 4 个命令工作正常。

$Credentials
变量指向预定义的凭据,这些凭据是远程服务器上的本地管理员。

当脚本到达第 5 行时,

$Downloader = $UpdateSession.CreateUpdateDownloader()
,我从 Powershell 收到此错误:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
    + CategoryInfo          : OperationStopped: (:) [], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException
    + PSComputerName        : SERVER.sidlee.inc

到底是什么原因造成的?

预先感谢您的帮助!

powershell powershell-remoting invoke-command windows-update
1个回答
0
投票

由于我刚刚遇到了同样的问题,谷歌也没有多大帮助,这就是我可以挖掘的内容。

郑重声明,我几乎在做同样的事情(使用自定义 PS 代码来检查远程系统的 Windows 更新),但使用 Python 上的 WinRM 而不是

Invoke-Command
,并且还陷入了
Microsoft.Update.Searcher.Search()
抛出
E_ACCESSDENIED
错误.

UnauthorizedAccessException
确实与Powershell无关,而是与底层API有关。

我怀疑微软在最近的一些更新(Powershell v5?)中开始切断远程会话中的模拟,因为这在旧的 Windows 版本(例如带有 Powershell v3 的 Server 2012 或带有 v4 的 2012 R2)上工作得很好(并且仍然)

要解决这个问题,您需要在使用

PSCredential
对象执行您的操作之前进行身份验证(在远程服务器上)。

所以

Remote Auth -> Local Auth -> Run stuff
例如使用
Start-Process -Credential ...

例如

$pass = ConvertTo-SecureString "PA$$W0RD" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential "User", $pass
Start-Process -Credential $creds powershell -ArgumentList "-Command & { ... whatever you want to do ... }"

请记住,这会带来安全风险,因为您的密码将以明文形式进行解析,因此请勿在 未加密的频道!

© www.soinside.com 2019 - 2024. All rights reserved.