问题描述
我想将更新部署到我们域中的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:未指定:(:) [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")
Invoke-Command:无法绑定参数“ Session”。无法将值“ True”转换为“ System.Management.Automation.Runspaces.PSSession”。 ...
解决方法
要解决此问题,我不得不更改复制到其他系统的方式以及get-windowsupdate的实际调用。
Mooudle必须位于$ env:PSModPath中,因此要修复该Mooudle,必须将其复制到其中一个文件夹中。
Copy-Item "$modpath\$($Script:My.ModuleVersion)\" -Destination "$psmod\PSWindowsUpdate\$($Script:My.ModuleVersion)" -ToSession $ses -Recurse -ErrorAction Stop
此更新不需要运行“通过调用命令”。
Get-WindowsUpdate -AcceptAll -Install -IgnoreReboot -ComputerName $session.ComputerName
希望这会在遇到类似问题时有所帮助!