Get sizes of registry hives using WMI - windows

I want to find out the file sizes of the hives in the registry using WMI and VBScript. This is what I have so far:
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
strComputer & "\root\cimv2:StdRegProv")
strKeyPath = "System\CurrentControlSet\Control\Hivelist"
objReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrVals, arrTypes
WScript.Echo "Values under System\CurrentControlSet\Control\Hivelist"
For Each val In arrVals
objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, val, dwValue
WScript.Echo dwValue
Next
This gives me the correct list, but then I need to get the file sizes. What is the best way to accomplish this?

To avoid type prefix fraud and meaningless variable names, use strRFSpec instead of dwValue
Convert the registry file spec (e.g. "\Device\HarddiskVolume1\Documents and Settings\NetworkService\NTUSER.DAT") into a strFSpec understandable to the FileSystemObject (e.g. "C:\Documents and Settings\NetworkService\NTUSER.DAT")
Check existence and accessability of strFSpec
WScript.Echo goFS.GetFile(strFSpec).Size
(tested under Win XP)
ADDED (wrt comment):
The conversion from strRFSpec to strFSpec may need more effort than a simple Replace() using hardcoded strings. Your Documents and Settings or your WINDOWS could live on F:\. So maybe you'll have to look for a WMI class that maps "\Device\HarddiskVolume... to a drive letter, to use %windir% on strFSpecs containing \system\, or ask WshShell.SpecialFolders("MyDocuments") for a drive letter. As my setup is simple, I can't give further - tested - advice.

Related

Getting actual powershell return value in vb6?

Alright, so I have been trying to do this for a while, and I have come to the realization that this isn't really something that is often asked, and with vb6 getting phased out more and more, there seems to be less help than I would like regarding the language.
The title doesn't say it all actually, as I am looking to do something very specific. I need to execute a shell command (that I know how to do), however, after I execute it, I want to be able to save the return value of that command as a string. For example, if the command is ipconfig, I want the entire return value of that, all the text I would see in powershell after executing that command, saved to a string in my program.
As far as I know, I need to "import" a few things, because I have to use WshShell, which I don't know where to get. So that's part of the question, what classes do I have to add and how, or if there is a way to do it without adding classes then even better. In addition, I have heard a lot about the use of CreatePipe and such regarding similar problems, but I don't know how to use it.
Basically, what I'm saying is that I am quite uneducated regarding the subject, and any insight would be much appreciated, and thanks to all who reply.
There are many ways. Using VBScript's WSHShell.Exec is the easiest.
This is VBScript but VBScript can be pasted into VB (not vice versa though).
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("ipconfig")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
MsgBox oExec.StdOut.ReadAll
Slightly modified from help.
This is how to ping from VBS/VB
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From win32_PingStatus where address='104.43.195.251'")
'msgbox colItems
For Each objItem in colItems
msgbox "Status" & objItem.statuscode & " Time " & objItem.ResponseTime
Next

VBS Scripting to move computers from one OU to the next

I am working on packaging a script for the company that I work for that will allow field service techs to convert computers in a private workstation OU to a team workstation OU and vice versa.
That is a small part of this script for now and one that has had me puzzled for most of the day. I've tried different variations of this script and landed on one that I believe will get me on the right track.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Name from Win32_ComputerSystem",,48)
For Each objItem in colItems
strPCName = objItem.Name
Next
Set objNewOU = GetObject("LDAP://OU=Computers,OU=Corporate,DC=xxxxx,DC=net")
Set objMoveComputer = objNewOU.MoveHere("LDAP://CN=" & strPCName & ",OU=Computers,OU=Corporate,DC=xxxxx,DC=net",vbnullstring)
I get an error, There is no such object on the server. When I put the computer manually in the OU in question, I don't get that error message. This is where I'm stuck at the moment.
The scripting is in my personal lab at the moment.
I was able to get a solution that did work, by using the following script.
Set objSysInfo = CreateObject("ADSystemInfo")
strComputerDN = objSysInfo.ComputerName
Set objNewOU = GetObject("LDAP://OU=Private Workstations,OU=xxxx,OU=xxxx,DC=xxxx,DC=xxxx")
Set objMoveComputer = objNewOU.MoveHere _ ("LDAP://" & strComputerDN, vbNullString)
Judging by the script's behavior, it uses strComputerDN to determine where the computer is, and the objNewOU determines where the computer is going. The objmoveComputer consolidates this information as best as I can determine to move the computer to it's OU.

How to find all folders within a folder in Windows Registry

There are already lots of questions on how to read/write windows and their are number of good jni/non-jni solutions available. I've gone through a few, but neither of them provided means to traverse the registry.
By traversal, I mean to say that I want to know all sub-folders of a particular folder in Windows Registry and then all keys within that folder.
The available solutions, let's one read the registry key, but not the registry folder to find the sub-folders within.
Leverage the Windows API for registry access, and expose it via JNI.
Windows Registry API: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724875(v=vs.85).aspx
Key function being RegEnumKeyEx
Here is a tutorial on walking the registry keys in c++: http://www.codeguru.com/cpp/w-p/ce/registry/article.php/c8301/Walking-The-Registry-Tree.htm
I was requiring some solution in java, but then found some VBScrip that can do this much easily
Const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARE\MICROSOFT\Windows\CurrentVersion\MMDevices\Audio\Capture"
Sub EnumerateKeys(hive, key)
'WScript.Echo key
reg.EnumKey hive, key, arrSubKeys
If Not IsNull(arrSubKeys) Then
For Each subkey In arrSubKeys
EnumerateKeys hive, key & "\" & subkey
Next
End If
reg.GetDWORDValue hive,key,"DeviceState",nn
'WScript.Echo key & " = " & nn
If nn=268435457 Then
'WScript.Echo "Good morning!"
reg.SetDWORDValue hive,key,"DeviceState",1
ElseIf nn=1 Then
reg.SetDWORDValue hive,key,"DeviceState",268435457
Else
'WScript.Echo "Have a nice day!"
End If
End Sub
Set reg = GetObject("winmgmts://./root/default:StdRegProv")
EnumerateKeys HKEY_LOCAL_MACHINE, strKeyPath
Above script not only iterates, over keys & subkeys, it also reads and compares and then sets DWORD values.
Quick Reference

can you get the pc user name in vbs?

Is there way to find the PC user in visual basic (C:\User\"here").
After we get it, just save it as a string.
I know the answer might be a bit obvious, but I cannot find out how to do this
Fairly simple, from here ( http://blogs.msdn.com/b/alejacma/archive/2008/03/11/how-to-get-the-user-running-a-vbscript.aspx )
Dim networkInfo
Set networkInfo = CreateObject("WScript.NetWork")
Dim infoStr
infoStr = "User name is " & networkInfo.UserName & vbCRLF & _
"Computer name is " & networkInfo.ComputerName & vbCRLF & _
"Domain Name is " & networkInfo.UserDomain
MsgBox infoStr
The simplest way might be to query the environment.
There are USERDOMAIN, USERNAME, USERPROFILE and COMPUTERNAME environment variables containing the obvious values.
Querying those would depend solely on WScript.Shell instead of on WScript.Network as in the accepted (and correct) answer. If you already have a reference to the shell, this might be a slightly more comfortable way.

Internet explorer grab internal ip address

I'm looking for a solution to grab people's internal ip addresses in IE (not using java or java applets). The equivalent in Java looks like that:
this.sock.bind(new java.net.InetSocketAddress('0.0.0.0', 0));
this.sock.connect(new java.net.InetSocketAddress(document.domain, (!document.location.port)?80:document.location.port));
return this.sock.getLocalAddress().getHostAddress();
Is that something possible in vbscript or jscript? Could you provide me with an example?
Thanks for your time.
You can not get internal IP with JavaScript.
This looks like something you'll need an ActiveX control for, if it is possible.
I think that depending on the security settings in IE you might be able to use WMI. If so you could just use the Win32_NetworkAdapterConfiguration and it's IPAddress property.
The following sample in vbscript:
strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select IPAddress from Win32_NetworkAdapterConfiguration ")
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) _
to UBound(IPConfig.IPAddress)
WScript.Echo IPConfig.IPAddress(i)
Next
End If
Next
Is taken from this MSDN page.

Resources