@@ -1155,47 +1155,56 @@ func (w *windows) scanKBs() (*models.WindowsKB, error) {
1155
1155
}
1156
1156
}
1157
1157
1158
- var searcher string
1159
- switch c := w .getServerInfo ().Windows ; c .ServerSelection {
1160
- case 3 : // https://learn.microsoft.com/en-us/windows/win32/wua_sdk/using-wua-to-scan-for-updates-offline
1161
- searcher = fmt .Sprintf (`$UpdateSession = (New-Object -ComObject Microsoft.Update.Session); $UpdateServiceManager = (New-Object -ComObject Microsoft.Update.ServiceManager); $UpdateService = $UpdateServiceManager.AddScanPackageService("Offline Sync Service", "%s"); $UpdateSearcher = $UpdateSession.CreateUpdateSearcher(); $UpdateSearcher.ServerSelection = %d; $UpdateSearcher.ServiceID = $UpdateService.ServiceID;` , c .CabPath , c .ServerSelection )
1162
- default :
1163
- searcher = fmt .Sprintf ("$UpdateSession = (New-Object -ComObject Microsoft.Update.Session); $UpdateSearcher = $UpdateSession.CreateUpdateSearcher(); $UpdateSearcher.ServerSelection = %d;" , c .ServerSelection )
1164
- }
1165
- if r := w .exec (w .translateCmd (fmt .Sprintf (`%s $UpdateSearcher.search("IsInstalled = 1 and RebootRequired = 0 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs` , searcher )), noSudo ); r .isSuccess () {
1166
- kbs , err := w .parseWindowsUpdaterSearch (r .Stdout )
1167
- if err != nil {
1168
- return nil , xerrors .Errorf ("Failed to parse Windows Update Search. err: %w" , err )
1158
+ if err := func () error {
1159
+ var searcher string
1160
+ switch c := w .getServerInfo (); c .Windows .ServerSelection {
1161
+ case 3 : // https://learn.microsoft.com/en-us/windows/win32/wua_sdk/using-wua-to-scan-for-updates-offline
1162
+ searcher = fmt .Sprintf (`$UpdateSession = (New-Object -ComObject Microsoft.Update.Session); $UpdateServiceManager = (New-Object -ComObject Microsoft.Update.ServiceManager); $UpdateService = $UpdateServiceManager.AddScanPackageService("Offline Sync Service", "%s"); $UpdateSearcher = $UpdateSession.CreateUpdateSearcher(); $UpdateSearcher.ServerSelection = %d; $UpdateSearcher.ServiceID = $UpdateService.ServiceID;` , c .Windows .CabPath , c .Windows .ServerSelection )
1163
+ default :
1164
+ if c .Mode .IsOffline () {
1165
+ return nil
1166
+ }
1167
+ searcher = fmt .Sprintf ("$UpdateSession = (New-Object -ComObject Microsoft.Update.Session); $UpdateSearcher = $UpdateSession.CreateUpdateSearcher(); $UpdateSearcher.ServerSelection = %d;" , c .Windows .ServerSelection )
1169
1168
}
1170
- for _ , kb := range kbs {
1171
- applied [kb ] = struct {}{}
1169
+ if r := w .exec (w .translateCmd (fmt .Sprintf (`%s $UpdateSearcher.search("IsInstalled = 1 and RebootRequired = 0 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs` , searcher )), noSudo ); r .isSuccess () {
1170
+ kbs , err := w .parseWindowsUpdaterSearch (r .Stdout )
1171
+ if err != nil {
1172
+ return xerrors .Errorf ("Failed to parse Windows Update Search. err: %w" , err )
1173
+ }
1174
+ for _ , kb := range kbs {
1175
+ applied [kb ] = struct {}{}
1176
+ }
1172
1177
}
1173
- }
1174
1178
1175
- if r := w .exec (w .translateCmd (fmt .Sprintf (`%s $UpdateSearcher.search("IsInstalled = 0 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs` , searcher )), noSudo ); r .isSuccess () {
1176
- kbs , err := w .parseWindowsUpdaterSearch (r .Stdout )
1177
- if err != nil {
1178
- return nil , xerrors .Errorf ("Failed to parse Windows Update Search. err: %w" , err )
1179
- }
1180
- for _ , kb := range kbs {
1181
- unapplied [kb ] = struct {}{}
1179
+ if r := w .exec (w .translateCmd (fmt .Sprintf (`%s $UpdateSearcher.search("IsInstalled = 0 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs` , searcher )), noSudo ); r .isSuccess () {
1180
+ kbs , err := w .parseWindowsUpdaterSearch (r .Stdout )
1181
+ if err != nil {
1182
+ return xerrors .Errorf ("Failed to parse Windows Update Search. err: %w" , err )
1183
+ }
1184
+ for _ , kb := range kbs {
1185
+ unapplied [kb ] = struct {}{}
1186
+ }
1182
1187
}
1183
- }
1184
1188
1185
- if r := w .exec (w .translateCmd (fmt .Sprintf (`%s $UpdateSearcher.search("IsInstalled = 1 and RebootRequired = 1 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs` , searcher )), noSudo ); r .isSuccess () {
1186
- kbs , err := w .parseWindowsUpdaterSearch (r .Stdout )
1187
- if err != nil {
1188
- return nil , xerrors .Errorf ("Failed to parse Windows Update Search. err: %w" , err )
1189
- }
1190
- for _ , kb := range kbs {
1191
- unapplied [kb ] = struct {}{}
1189
+ if r := w .exec (w .translateCmd (fmt .Sprintf (`%s $UpdateSearcher.search("IsInstalled = 1 and RebootRequired = 1 and Type='Software'").Updates | ForEach-Object -MemberName KBArticleIDs` , searcher )), noSudo ); r .isSuccess () {
1190
+ kbs , err := w .parseWindowsUpdaterSearch (r .Stdout )
1191
+ if err != nil {
1192
+ return xerrors .Errorf ("Failed to parse Windows Update Search. err: %w" , err )
1193
+ }
1194
+ for _ , kb := range kbs {
1195
+ unapplied [kb ] = struct {}{}
1196
+ }
1192
1197
}
1193
- }
1194
1198
1195
- if w .getServerInfo ().Windows .ServerSelection == 3 {
1196
- if r := w .exec (w .translateCmd (`$UpdateServiceManager = (New-Object -ComObject Microsoft.Update.ServiceManager); $UpdateServiceManager.Services | Where-Object {$_.Name -eq "Offline Sync Service"} | ForEach-Object { $UpdateServiceManager.RemoveService($_.ServiceID) };` ), noSudo ); ! r .isSuccess () {
1197
- return nil , xerrors .Errorf ("Failed to remove Windows Update Offline Sync Service: %v" , r )
1199
+ if w .getServerInfo ().Windows .ServerSelection == 3 {
1200
+ if r := w .exec (w .translateCmd (`$UpdateServiceManager = (New-Object -ComObject Microsoft.Update.ServiceManager); $UpdateServiceManager.Services | Where-Object {$_.Name -eq "Offline Sync Service"} | ForEach-Object { $UpdateServiceManager.RemoveService($_.ServiceID) };` ), noSudo ); ! r .isSuccess () {
1201
+ return xerrors .Errorf ("Failed to remove Windows Update Offline Sync Service: %v" , r )
1202
+ }
1198
1203
}
1204
+
1205
+ return nil
1206
+ }(); err != nil {
1207
+ return nil , xerrors .Errorf ("Failed to check Windows Update Serach. err: %w" , err )
1199
1208
}
1200
1209
1201
1210
if r := w .exec (w .translateCmd ("$UpdateSearcher = (New-Object -ComObject Microsoft.Update.Session).CreateUpdateSearcher(); $HistoryCount = $UpdateSearcher.GetTotalHistoryCount(); $UpdateSearcher.QueryHistory(0, $HistoryCount) | Sort-Object -Property Date | Format-List -Property Title, Operation, ResultCode" ), noSudo ); r .isSuccess () {
0 commit comments