简体   繁体   中英

Output Vbscript info to txt file

Newbie question - I have here a VBScript that looks for an Upgrade Code, and based on that finds the Product Codes for the specified Upgrade Code. The Upgrade Code is always the same, but Product Code changes from version to version, and that can make uninstalling software troublesome. And no, I didn't make this script myself.

This script works very well, but I'd like to make it output all the product codes it found to a text file. I've looked on Google for hours, found some clues, but I've not been able to make it work. Always turns up with a blank text file.

Here's the code:

strComputer = "."
Set WshShell = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

On Error Resume Next 

Set colSoftware = objWMIService.ExecQuery _ 
    ("Select * from Win32_Property Where Property = 'UpgradeCode'") 

For Each objSoftware in colSoftware 
    If objSoftware.Value = "{BCCCB25E-C6A6-4340-9018-DA0FB34AF226}" Then
    strCMD = "MsiExec.exe /x " & objSoftware.ProductCode & " /qn"
    objExec = WshShell.Run(strCMD,1,True)
    If objExec <> 0 Then
       WScript.Quit objExec
    End If
    End If
Next
WScript.Quit 0

How do I output objSoftware.ProductCode to a text file? Or do I need to output something else to get the Product Codes I'm looking for?

The easy way to write text to a file is to WScript.Echo the desired info and run the script like cscript x.vbs > output.txt .

If that seems to pedestrian, start your research here .

Played around with it, Googled it, and I found a solution that works for me. This prints out all the Product Codes on your computer based on the specificed Upgrade Code. Here's the script:

strComputer = "."
Set WshShell = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

On Error Resume Next 

Set colSoftware = objWMIService.ExecQuery _ 
    ("Select * from Win32_Property Where Property = 'UpgradeCode'") 

For Each objSoftware in colSoftware 
    If objSoftware.Value = "{BCCCB25E-C6A6-4340-9018-DA0FB34AF226}" Then
    Wscript.Echo objSoftware.ProductCode
    strCMD = "MsiExec.exe /x " & objSoftware.ProductCode & " /qn"
    objExec = WshShell.Run(strCMD,1,True)
    If objExec <> 0 Then
       WScript.Quit objExec
    End If
    End If
Next
WScript.Quit 0

Running //NoLogo scriptname.vbs > log.txt in command prompt, gives me a txt file with all the product codes for the upgrade code specified.

Please note this code also uninstalls the software afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM