问题描述
所以我有一个问题,我有一个小脚本可以在无人看管的情况下更新我的 Windows 电脑,但为了确保它确实安装了我想要记录的更新.现在差不多完成了,但我想添加最后一件事.
So i have a question, i have a little script that updates my windows pc unattended, but to make sure it actually installs the updates i want to log it. Now its almost done, but i want to add a last thing.
我找到了一个字符串,该字符串定义了对更新进行下载、安装、卸载或验证操作的可能结果.但是它以数字显示,从 0 到 5.但是当它显示某个数字时,我想像这样在它后面添加它的含义:
I found a string that defines the possible results of a download, install, uninstall, or verification operation on an update. But it shows in in numbers, from 0 to 5. But when it shows a certain number i want to add the meaning of it behind it like this:
0 = not started
1 = in progress
2 = succeeded
3 = succeededwitherrors
4 = failed
5 = aborted
有谁知道如何使用 If 函数来完成此操作?
Does anyone knows how this can be done with something like an If function?
非常感谢,
亲切的问候
编辑(添加完整代码):
EDIT (added full code):
option explicit
Dim fso, f, ssDefault, ssManagedServer, ssWindowsUpdate, ssOthers, updates
Dim strComputer, intSearchStartChar, updateSession, updateSearcher
Dim searchResult, I, update, updatesToDownload, addThisUpdate, downloader, updatesToInstall, rebootMayBeRequired, installer, installationResult, objWMIService, colOS, objOS
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set f = fso.OpenTextFile("C:\outpuut.txt", 8, True)
'End of Script
'ServerSelection values
ssDefault = 0
ssManagedServer = 1
ssWindowsUpdate = 2
ssOthers = 3
strComputer = "." ' Local Computer
'InStr values
intSearchStartChar = 1
dim strTitle
Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()
updateSearcher.ServerSelection = ssWindowsUpdate
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
f.WriteLine "<Gevonden updates>"
For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
f.WriteLine "<Update info>"
f.WriteLine vbCRLF & "<titel>" & update.Title & "</titel>"
f.WriteLine "<status>" & update.MsrcSeverity & "</status>"
f.WriteLine "<verborgen>" & update.IsHidden & "</verborgen>"
f.WriteLine "<omschrijving>" & update.Description & "</omschrijving>"
f.WriteLine vbCRLF & "</Update info>"
Next
If searchResult.Updates.Count = 0 Then
f.WriteLine "<Update info>"
f.WriteLine "<Error> Geen updates beschikbaar </Error>"
f.WriteLine "</Update info>"
f.WriteLine vbCRLF & "</Gevonden updates>"
WScript.Quit
End If
f.WriteLine vbCRLF & "</Gevonden updates>"
f.WriteLine vbCRLF & "<Gedownloadde updates>"
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
addThisUpdate = false
If update.InstallationBehavior.CanRequestUserInput = true Then
f.WriteLine I + 1 & "> skipping: " & update.Title & _
" because it requires user input"
Else
If update.EulaAccepted = false Then
f.WriteLine I + 1 & "> note: " & update.Title & _
" has a license agreement that must be accepted:"
update.AcceptEula()
addThisUpdate = true
Else
addThisUpdate = true
End If
End If
If addThisUpdate = true Then
f.WriteLine "<Download info>"
f.WriteLine "<titel>" & update.Title & "</titel>"
f.WriteLine "<downloadpriority>" & update.DownloadPriority & "</downloadproirity>"
f.WriteLine "<isdownloaded>" & update.IsDownloaded & "</isdownloaded>"
f.WriteLine "<maxdownloadsize>" & update.MaxDownloadSize & "</maxdownloadsize>"
f.WriteLine "<mindownloadsize>" & update.MinDownloadSize & "</mindownloadsize>"
f.WriteLine vbCRLF & "</Download info>"
updatesToDownload.Add(update)
End If
Next
If updatesToDownload.Count = 0 Then
f.WriteLine "All applicable updates were skipped."
WScript.Quit
End If
Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()
Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
rebootMayBeRequired = false
For I = 0 To searchResult.Updates.Count-1
set update = searchResult.Updates.Item(I)
If update.IsDownloaded = true Then
updatesToInstall.Add(update)
If update.InstallationBehavior.RebootBehavior > 0 Then
rebootMayBeRequired = true
End If
End If
Next
f.WriteLine vbCRLF & "</Gedownloadde updates>"
If updatesToInstall.Count = 0 Then
f.WriteLine "<Error>No updates were successfully downloaded.</Error>"
WScript.Quit
End If
If rebootMayBeRequired = true Then
End If
f.WriteLine "<geinstalleerde updates>"
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
'Output results of install
f.WriteLine "<Installatie resultaat>" & installationResult.ResultCode & "</Installatie resultaat>"
f.WriteLine "<Reboot nodig>" & installationResult.RebootRequired & "</Reboot nodig>" & vbCRLF
f.WriteLine "Listing of updates installed " & _
"and individual installation results:"
For I = 0 to updatesToInstall.Count - 1
f.writeline "<Updates resultaat>"
f.writeline "<Titel>" & updatesToInstall.Item(i).Title & "</Titel>"
f.writeline "<ResultaatCode>" & installationResult.GetUpdateResult(i).ResultCode & "</ResultaatCode>"
Set d = CreateObject("Scripting.Dictionary")
f.writeline d(0) = "not started"
f.writeline d(2) = "succeeded"
Next
f.WriteLine "</geinstalleerde updates>"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!\\" & _
strComputer & "\root\cimv2")
Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOS
objOS.Rebooot()
Next
f.close
推荐答案
function resultcode(strCode)
result = "Unknown"
if strCode = 0 then result = "not started"
if strCode = 1 then result = "in progress"
if strCode = 2 then result = "succeeded"
if strCode = 3 then result = "succeededwitherrors"
if strCode = 4 then result = "failed"
if strCode = 5 then result = "aborted"
resultcode = result
end function
然后用
I + 1 & "> " & updatesToInstall.Item(i).Title & ": " & resultcode(installationResult.GetUpdateResult(i).ResultCode)
这篇关于结果代码枚举 VBScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!