【发布时间】:2020-08-24 10:42:57
【问题描述】:
我想将更新部署到我们域中的 Windows 服务器。 为此,我想使用模块“PSWindowsUpdate”Here is the Official Release。 我将此模块与 PSSessions 结合使用,并将其本地导入到默认模块路径之外的所有服务器上。
它应该接受更新并在不重新启动的情况下安装它们。此脚本使用域管理员运行
在接受更新后,它应该开始下载发生这种情况的地方:The Error of the Job
我在安装 2018 年 7 月安全补丁后开始收到此错误。
由于公司原因,我无法分享所有代码,因此这里是重要的部分:
function invokeUpdate{
param(
$session
)
if($Script:My.Reboot.isChecked){
$job = Invoke-Command -Session $session -ScriptBlock {Import-Module "C:\Scripts\updateModule\$($Using:My.ModuleVersion)\PSWindowsUpdate"; get-windowsupdate -install -AcceptAll} -AsJob
}else {
$job = Invoke-Command -Session $session -ScriptBlock {Import-Module "C:\Scripts\updateModule\$($Using:My.ModuleVersion)\PSWindowsUpdate"; get-windowsupdate -install -ignoreReboot -AcceptAll} -AsJob
}
return $job
}
function initSession{
param(
$serverHostname
)
$ses = New-PSSession -Computername $serverHostname
if(!(Invoke-Command -Session $ses -ScriptBlock {Test-Path "C:\Scripts\updateModule\" })){
Copy-Item "$modpath\$($Script:My.ModuleVersion)" -Destination "C:\Scripts\updateModule\$($Script:My.ModuleVersion)" -ToSession $ses -Recurse
}
Invoke-Command -Session $ses -ScriptBlock {
if((Get-ChildItem -Path "C:\Scripts\updateModule\").count -gt 1){
Get-ChildItem | Where-Object Name -NotLike "$($Using:My.ModuleVersion)" | Remove-Item -Recurse -Force
}
}
return $ses
}
$sessions = [System.Collections.ArrayList]@()
$Script:My.ModuleVersion = "2.1.1.2"
foreach ( $server in $Script:My.ServerActive.Items){
$sessions.Add( (initSession -serverHostname $server) )
}
foreach ($ses in $sessions){
invokeUpdate -session $ses
}
$Script:My.ServerActive.Items : 包含服务器 fqdns 列表
任何想法或解决方案都会拯救我, 谢谢!
尼克
编辑 1:
这是错误信息:
访问被拒绝。 (来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED)) + CategoryInfo : NotSpecified: (:) [Get-WindowsUpdate], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,PSWindowsUpdate.GetWindowsUpdate + PSComputerName : fs02.azubi.netz
这会破坏我的会话,但输出是 $true
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
调用命令:无法绑定参数“会话”。无法将值“True”转换为类型“System.Management.Automation.Runspaces.PSSession”。 ...
【问题讨论】:
-
您能否将输出/错误粘贴到问题中,而不是链接到错误图像?
-
我首先想到的是您的远程会话没有以提升的权限运行。尝试通过 PSRemoting 在远程系统上以您的 DA 用户身份运行它,如果您具有管理权限,它将返回
$True,如果没有,它将返回$False:([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")。如果这是问题,请回复,我有一个可以解决这个问题的答案。