VBScript/HTA - Using WMI to search registry - vbscript

I'm running into an interesting problem when I attempt to run VBScript code from within an HTA application. Specifically, when I query the Registry using WMI. Below is the VBscript (within .HTA file) code I am using to determing instance names of SQL server installations:
<script language="VBScript">
Sub searchRegistry
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Microsoft SQL Server\Instance Names\SQL"
oReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes
msgbox "SQL Instances already installed: "
For i=0 To UBound(arrValueNames)
msgbox arrValueNames(i)
Next
End Sub
Null is returned and the loop throws a bound error. However, when I run this same code from an independent VBScript (.vbs) file it returns the proper values no problem. I assume this is a permissions issue but don't know where to start; don't know how to give windows HTA files permission to use WMI to search registry. Moreover, I am able to use WMI from HTA to do other things (get drive space, etc.) without issue so it must be registry restrictions? Any ideas?

As per this , Windows do allow HTA to update registry.
If it does not work for you, It could be related to your 'User Account Control' settings. Disable and try!

After days of toiling, I've figured it out...
On some 64-bit systems, HTA is incorrectly associated with the 32-bit MSHTA version (%windir%\SYSWOW64\mshta.exe) - resulting in only being able to access certain WMI classes.
In this case, add the path to the proper (64-bit) MSHTA to the command line. Example: "%windir%\system32\mshta.exe" "c:\whatever.hta" and the HTA should run with full access to WMI.

Related

Shared folder not visible in Win32_Share

I have shared a folder.
When I right-click it, it says under "Sharing":
"Network path: \DESKTOP-K77052H\Users\VMWareUser\Desktop\ausgaber"
(I have attached a screenshot).
However, this folder is not listed when I query Win32_Share:
Dim strComputer As String = "."
Dim objWMIService As Object = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Dim colShares As Object = objWMIService.ExecQuery("Select * from Win32_Share where type=0")
For Each objShare In colShares
Debug.Print(objShare.path)
I do see other shared folders, but not this one.
The other ones that our output in the above code do not have special permissions, just like the folder in question, so I don't understand the difference between those and the folder that I expect to the output in the above function.
I've also restarted the computer, deleted the share, shared it again, it wouldn't help.
What might be the issue here?
I found myself in similar situation when few folders were shared when I enable sharing via property dialog and few weren't.
To fix this you need to enable sharing by clicking on Advanced Sharing... button, see attached image below:

How to add a license key through VB Script?

i am new to the vb script. I have installed a program and have to call an License.exe and fill the License name and license key. Batch file to add it into the reg key is not working on this app. So it would be great if you can help me in this.When i open the License.exe with installed product . it asks me to fill License Name and the License Key. I dont know how to create to add that key through script but i was able to call the .exe. here is an example.
Dim objShell
Dim StrLicensename
Dim StrLicensekey
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""C:\ProgramFiles\Resources\licensing.exe""")
StrLicensename = CStr("Melbourne Victoria")
StrLicensekey = Cstr("1234-4567835")
Set objShell = Nothing
Any help would be much appreciated.
Thank you in advance.
Well we need to package it and deploy through SCCM. Thats the reason our team was thinking to do ao rather than activating 50 machines manually.

Get list of installed updates as part of Windows Updates using VBScript

I am very new to VBScript and I need to get the list of installed Softwares(For example Microsoft Visual C++ 2010 etc) as part of Windows Updates using VBScript or any.
If installed Softwares are listing under Add/Remove programs using WMIC we can get the list.
wmic product where \"Name like
But, for example Microsoft Visual C++ 2010 is installed as a part of Windows Updates which is not listing under Add/Remove programs.
Microsoft Visual C++ 2010 is listed in the Registries. Here I need to get the list of such Softwares using VBScript or any.
I need this script for Windows 2008 R2 Standard Operating System.
Any kind of directions or solutions will be of immense help.
Thanks In Advance.
You need to use wmi registry class to enumerate keys in the registry.
EnumKey Method of the StdRegProv Class
The EnumKey method enumerates the subkeys for a path. See Obtaining Registry Data for general information on accessing the registry through WMI.
This topic uses Managed Object Format (MOF) syntax. For information about using this method, see Calling a Method.
uint32 EnumKey(
uint32 hDefKey = 2147483650,
string sSubKeyName,
string sNames[]
);
Parameters
hDefKey
[in, optional] A registry tree, also known as a hive, that contains the sSubKeyName path. The default value is HKEY_LOCAL_MACHINE.
Note that HKEY_DYN_DATA is a valid tree for Windows 95 and Windows 98 computers only.
The following trees are defined in Winreg.h.
Name Value
HKEY_CLASSES_ROOT 2147483648
0x80000000
HKEY_CURRENT_USER 2147483649
0x80000001
HKEY_LOCAL_MACHINE 2147483650
0x80000002
HKEY_USERS 2147483651
0x80000003
HKEY_CURRENT_CONFIG 2147483653
0x80000005
HKEY_DYN_DATA 2147483654
0x80000006
sSubKeyName
[in] A path that contains the subkeys to be enumerated.
sNames
[out] An array of subkey strings.
Return Value
In C++, the method returns a uint32 value that is 0 (zero) if successful. If the function fails, the return value is a nonzero error code that is defined in Winerror.h. In C++, you can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error. You can also look up return values under the WMI Error Constants.
In scripting or Visual Basic, the method returns an integer value that is 0 (zero) if successful. If the function fails, the return value is a nonzero error code that you can look up in WbemErrorEnum.
Example Code
For script code examples, see WMI Tasks: Registry and the TechNet ScriptCenter Script Repository. Other examples are in books and articles listed in Further Information.
For C++ code examples, see WMI C++ Application Examples.
The following VBScript example shows how to use the EnumKey method to enumerate the services listed as subkeys in the registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
You can save the script as a file with a .vbs extension and send the output to a file by executing the command line in the folder that contains the script:
cscript Filename.vbs > output.txt
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Services"
objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
WScript.Echo "Subkeys under " _
& "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services"
For Each subkey In arrSubKeys
WScript.Echo subkey
Next
This explains where it is stored.
Start - All Programs - Accessories - Right click Command Prompt and choose Run As Administrator. Type (or copy and paste by right clicking in the Command Prompt window and choosing Paste). Type for table format
wmic /output:"%userprofile%\desktop\WindowsInstaller.html" product get /format:htable
or in a form format
wmic /output:"%userprofile%\desktop\WindowsInstaller.html" product get /format:hform
It will create a html file on the desktop.
Note
This is not a full list. This is only products installed with Windows Installer. There is no feature for everything.
However as I said in my previous post nearly everything is listed in the registry.
So to see it in a command prompt
reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /s
or in a file
reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /s>"%userprofile%\desktop\WindowsUninstall.txt"
To see it in notepad in a different format
Click Start - All Programs - Accessories - Right click Command Prompt and choose Run As Administrator. Type Regedit and navigate to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
Right click the Uninstall key and choose Export. If you save as a reg file (there is also text file, they are slightly different text formats) you need to right click the file and choose Edit to view it.
To view Windows Updates
wmic /output:"%userprofile%\desktop\WindowsUpdate.html" qfe get /format:htable
.

Executing VBA out of Shell

I am generating Excel Files with Pentaho Data Integration and I want to start a Macro automaticly after creation.
Until now, the Macro is started while opening the file.
But this is not a good way: Some users dont have permissions to execute Macros and each time you open the file Excel is asking if you want to save the changes.
I am wondering if there is a way to execute a VBA Macro in MS Excel out of the Windows Shell.
What I found is this code to open a file:
Shell "C:\Programme\Office2k\Office\EXCEL.EXE " & _"C:\...\MyExcelFile.xls"
But this is not what I want. I want to start the Macro exactly one time, and before any user opened it.
Do you have any ideas?
The solution with vbscript looks like this (Open, Save, Close without User Interaction):
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\dev\testo.xls")
objExcel.Application.Run "testo.xls!test"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Quit

How to make this WSH script work? (details in message)

This script is supposed to open both Windows shell Status and Properties dialogs of the first found network connection which is enabled or connected. However, only the Properties dialog is opened. The verb for the Status dialog is already correct, which is "Stat&us". The script was tested and will be used under Windows XP Pro SP3 32-Bit. It was tested with a connected 3G dialup and a LAN Loopback. Both have the same problem.
dim a,b,c
set a=createobject("shell.application")
set b=a.namespace(0).parsename("::{7007ACC7-3202-11D1-AAD2-00805FC1270E}").getfolder
for i=0 to (b.items.count-1)
set c=b.items.item(i)
for j=0 to (c.verbs.count-1)
'only process connected/enabled
if (lcase(c.verbs.item(j)) = "disc&onnect") or (lcase(c.verbs.item(j)) = "disa&ble") then
'open status and properties dialogs
c.invokeverb("Stat&us") 'this doesn't work
c.invokeverb("P&roperties") 'this one works
msgbox "Press OK to close all and exit"
wscript.quit
end if
next
next
In Windows XP there is a bug whose effect requires the Status verb to be invoked from within the Explorer process. Since WScript/CScript is not a child of the Explorer process, any attempt to invoke the status verb with prove futile despite the lack of any apparent errors. This bug appears to have been fixed in later versions as the script below is tested and working in Vista x64.
Set objShell = CreateObject("Shell.Application")
Set objShellFolder = objShell.Namespace(0).ParseName("::{7007ACC7-3202-11D1-AAD2-00805FC1270E}").GetFolder
For Each objShellFolderItem in objShellFolder.Items
Set colShellFolderItemVerbs = objShellFolderItem.Verbs
For Each objShellFolderItemVerb in colShellFolderItemVerbs
strVerb = objShellFolderItemVerb.Name
If (strVerb = "C&onnect / Disconnect") Then
objShellFolderItem.InvokeVerb("Properties")
objShellFolderItem.InvokeVerb("Status")
MsgBox "Press OK to close and exit"
WScript.Quit(0)
End If
Next
Next
Option 1
Does that mean that you're out of luck? Not entirely. I have two different suggestions for you. The first uses a little trickery. Status is the default action for any network connection while it is in a connected state. Open up your network connections, right-click the connection your wish to monitor and choose Create Shortcut. You can place the shortcut anywhere you like. By default it will be named something like "Wireless Network Connection - Shortcut.lnk" on your Desktop. Typing that on the command line or via the Run or Exec methods in your script will do exactly what you need. I tried playing around with doing this all via scripting but ran into issues tryint to automate the Create Shortcut verb.
Option 2
A second option is also a bit of a workaround but may work if your 3G connection uses the dialup networking. The command line rundll32.exe rnaui.dll,RnaDial {name of connection to establish} will open the dialog to connect, however, if already connected, it opens the Status dialog for the connection. You could then try a script like this:
Set objShell = CreateObject("Shell.Application")
Set objShellFolder = objShell.Namespace(0).ParseName("::{7007ACC7-3202-11D1-AAD2-00805FC1270E}").GetFolder
For Each objShellFolderItem in objShellFolder.Items
strConnection = objShellFolderItem.Name
strCommandLine = "rundll32.exe rnaui.dll,RnaDial " & Chr(34) & strConnection & Chr(34)
Set colShellFolderItemVerbs = objShellFolderItem.Verbs
For Each objShellFolderItemVerb in colShellFolderItemVerbs
strVerb = objShellFolderItemVerb.Name
If (strVerb = "C&onnect / Disconnect") Then
objShellFolderItem.InvokeVerb("Properties")
CreateObject("WScript.Shell").Run strCommandLine
MsgBox "Press OK to close and exit"
WScript.Quit(0)
End If
Next
Next
Option 3
A final option would be to use WMI to display the information about your network connection. This is a more traditional scripting approach.
In any case, I hope this helps out. Don't forget to change the verbs as required. They do change from one version of Windows to the next.

Resources