2018年4月3日 星期二

PowerShell-DSC 學習筆記 -- 用 DSC 更新 Windows Patch


1.在開始之前,請先確認 各 Windows 作業系統的 WMF 可用性
https://docs.microsoft.com/zh-tw/powershell/wmf/readme
image

若之前有安裝 WMF 3.0 ,須先移除 再安裝 WMF 5.1

2.把 RebootNodeIfNeeded 設定為 True,這樣更新完成後,如果有需要重新開機,就會自動重開機。
安裝完hotfix後,並不會自動重開機
查看Read ME文件,裡面有表示 RebootNodeIfNeeded 須為 True , 才會重開機
使用 Get-DscLocalConfigurationManager -CimSession $test1 查看
Image
使用下面的設定,把  RebootNodeIfNeeded 設定為 True

Configuration ConfigureRebootOnNode
{
param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]
$NodeName
    )
Node $NodeName
    {
LocalConfigurationManager
        {
            RebootNodeIfNeeded = $true
        }
    }
}
Write-Host "Creating mofs"
ConfigureRebootOnNode -NodeName twitwin7-03 -OutputPath .\rebootMofs
Write-Host "Starting CimSession"
$pass = ConvertTo-SecureString "SCOMej03xu35k3" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("coretronic\opsaction", $pass)
$cim = New-CimSession -ComputerName twitwin7-03 -Credential $cred
Write-Host "Writing config"
Set-DscLocalConfigurationManager -CimSession $cim -Path .\rebootMofs -Verbose

3.下載 DSC Module
Install-Module -Name xWindowsUpdate
Install-Module -Name xPendingReboot
Image(1)

4.設定 DSC
#MOF 文件設定
configuration WSUS-Reboot {
Import-DscResource -ModuleName xWindowsUpdate
Node twitwin7-03 {
xWindowsUpdateAgent MuSecurityImportant {
            Category = @('Security','Important','Optional')
            IsSingleInstance = 'Yes'
            Source = 'WindowsUpdate'
            UpdateNow = $True
        }
    }
Node twitwin7-03 {
xPendingReboot Reboot1
          {
            Name = 'BeforeSoftwareInstall'
          }
LocalConfigurationManager
               {
             RebootNodeIfNeeded = $True
               }
       }
}
#匯出 MOF 檔案
WSUS-Reboot -OutputPath E:\temp\7
#執行
Start-DscConfiguration -computername twitwin7-03 -Path E:\temp\7 -Wait -Verbose -force

Start-DscConfiguration -computername twitwin7-03 -Path E:\temp\7 -Wait -Verbose -force
詳細資訊: 使用下列參數來執行操作 '叫用 CimMethod': 'methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguration。
詳細資訊: 收到來自電腦 NEOTEST,使用者 SID S-1-5-21-669332786-2716586810-2093476240-120181 的 LCM 方法呼叫。
詳細資訊: [TWITWIN7-03]: LCM:  [ 啟動     設定       ]
詳細資訊: [TWITWIN7-03]: LCM:  [ 啟動     資源       ]  [[xWindowsUpdateAgent]MuSecurityImportant]
詳細資訊: [TWITWIN7-03]: LCM:  [ 啟動     測試       ]  [[xWindowsUpdateAgent]MuSecurityImportant]
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] TryGet RebootRequired...
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Got rebootRequired: False
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Searching for updating using: IsHidden=0 and IsInstalled=0
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Get default search service: 3da21691-e39d-4da6-8a4b-b43877bcb1b7
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] updateNow compliant: False
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] notifications compliant: True
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] service compliant: True
詳細資訊: [TWITWIN7-03]: LCM:  [ 結束     測試       ]  [[xWindowsUpdateAgent]MuSecurityImportant]  (在 55.1080 秒內)。
詳細資訊: [TWITWIN7-03]: LCM:  [ 啟動     設定       ]  [[xWindowsUpdateAgent]MuSecurityImportant]
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] TryGet RebootRequired...
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Got rebootRequired: False
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Searching for updating using: IsHidden=0 and IsInstalled=0
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Get default search service: 3da21691-e39d-4da6-8a4b-b43877bcb1b7
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] updateNow compliant: False
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] notifications compliant: True
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] service compliant: True
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Performing the operation "Set-TargetResource" on target "Install Updates".
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Searching for updating using: IsHidden=0 and IsInstalled=0
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Installing updates...
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Found update: Microsoft Office 2010 (KB2553140) 32 位元版本 的更新
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Found update: Microsoft Office 2010 (KB2956076) 32 位元版本 的安全性更新
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Downloading updates...
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Installing updates...
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Checking for a reboot...
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] TryGet RebootRequired...
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Got rebootRequired: True
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] A reboot was required
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Performing the operation "Set-TargetResource" on target "Disable Microsoft Update Service".
詳細資訊: [TWITWIN7-03]:                            [[xWindowsUpdateAgent]MuSecurityImportant] Disable the Microsoft Update setting
詳細資訊: [TWITWIN7-03]: LCM:  [ 結束     設定       ]  [[xWindowsUpdateAgent]MuSecurityImportant]  (在 159.8550 秒內)。
詳細資訊: 作業 '叫用 CimMethod' 完成。
詳細資訊: 完成設定工作所花費的時間為 216.013 秒

可以觀察  ,目標 已經重開機
Image(2)








































































































沒有留言:

張貼留言