VBScript to take PC's name, remove anything thats not a letter or number (i.e. A-BC-123 -> ABC123) - vbscript

Me and a non-programmer would appreciate some help with VBScript to remove anything that's not a letter or number from the PC's name and store the modified name. Then join or rejoin the domain.
Example:
pc name: pc-home-543
rename: pchome543
join domain: homeworkplay
restart and it all works

You can rename a computer using the Rename method of the Win32_ComputerSystem WMI class. For replacing non-alphanumeric characters use a regular expression.
Set net = CreateObject("WScript.Network")
Set wmi = GetObject("winmgmts://./root/cimv2")
Set re = New RegExp
re.Pattern = "[^a-z0-9]*"
re.Global = True
re.IgnoreCase = True
For Each sys In wmi.ExecQuery("SELECT * FROM Win32_ComputerSystem")
sys.Rename re.Replace(net.ComputerName, "")
Next
You need to reboot the computer for the name change to become effective:
CreateObject("WScript.Shell").Run "shutdown -r -t 0"
I don't think you need to re-join the system to the domain afterwards, but if you do, you can do it with the JoinDomainOrWorkgroup method of the same WMI class.

Related

VBScript WMI PnPEntity obtaining HardwareID

I'm looking to extract the hardwareID from each device installed on a system using VBScript.
I can extract most properties from the PnPEntity class however the HardwareId or CompatibleId does seem to cause trouble - I'm presuming because it potentially returns an array.
My script is as follows:
Set TxtDriverOutput = objFSO.CreateTextFile("C:\Program Files\xxx\drivers.log", 8, True)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
txtDriverOutput.WriteLine Now() & Chr(32) & "Begin HardwareID WMI Query"
txtDriverOutput.WriteLine "----------------------------------------------------------------------------"
txtDriverOutput.WriteLine ""
Set colsHardwareID = objWmiService.ExecQuery("Select * from Win32_PnPEntity")
For Each objItem In colshardwareID
For Each StrHardwareID In objItem.HardwareID
txtdriveroutput.WriteLine StrHardwareID
Next
Next
When I run the script, I see one hardware ID populated into the text file - and then I get Error: Object is not a collection flagged on the line of my second for loop (sometimes I have seen it flag the line after it errors for some reason so maybe take that with a pinch of salt.
I have tried using ObjItem.HarwareID.count, UBound(ObjItem.HardwareID) in case there is a PnP Device that doesn't have a hardware ID (not sure if possible). Can anyone point me in the right direction? Thanks!
I managed to get to the bottom of this in the end.
I had to use the following:
For Each objItem In colshardwareID
If Not IsNull(objItem.HardwareID) Then
For Each StrHardwareID In objItem.HardwareID
ReDim Preserve ArrHardwareID(i)
ArrHardwareID(i) = strHardwareID
i = i + 1
Next
End If
Next
As expected - the collection was empty. A .count or .isempty was not working, it had to be IsNull.

vbScript that Checks NetworkAdapter Config and updates URL if it detects change

Ok... everyone here seems to be way more educated about this stuff than I. But I've been at this for 2 days straight and I've gone at it from all kinds directions.
Goal:
Have a script check the IP address of local pc. At some time (thinking 5 minutes) recheck the IP address again. Compare the two and if there is a change execute a command silently.
I realize that I can not just get the IP address as I'll get the entire state using winmgmts and I'm ok with that. Really it's just to keep an eye on whether or not the state changes in general. The line of code is to access a URL which will update my FreeDNS incase the IP changes. For whatever reason, none of the programs offered have been working. One even crashes .Net Framework.
Knowing that I'm an utter noob (and thank you for reading this far!) here's what my crazy broken and utterly bizarre code looks like. Any assistance would be divine and I apologize
Dim firstSet, secondSet
Set objWMIService = GetObject("winmgmts:")
Set FirstIP = objWMIService.ExecQuery("SELECT * FROM " & "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
Set firstSet = FirstIP
WScript.Sleep 3
Set objWMIService = GetObject("winmgmts:")
Set SecondIP = objWMIService.ExecQuery("SELECT * FROM " & "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
Set secondSet = SecondIP
results = StrComp(firstSet, secondSet, 1)
If Not results = True Then
updateURL = "MyURL"
Set req = CreateObject("MSXML2.XMLHTTP.6.0")
req.Open "GET", updateURL, False
req.Send
End If

vbscript Remove mapped drive if the drive letter and path matches

Here is the vbscript that I have which should remove the netwrok drive if it matches the letter and the path but it does not work properly and the array shows 13 mapped drives which I only have 5 on my computer. Doesnt it suppose to check all the available mapped drives on the user's mapped computer?
Set objShell = CreateObject("Wscript.Shell")
Set objNet = WScript.CreateObject("Wscript.Network")
Set objExec = objShell.Exec("net use ")
strMaps = LCase(objExec.StdOut.ReadAll)
MapArray=split(strMaps,CHR(10))
for x=1 to ubound(MapArray)
if instr(MapArray(x),"W:") AND instr(mapArray(x),"\\path\folder$") then
objNet.RemoveNetworkDrive "W:",true,true
end if
if instr(MapArray(x),"U:") AND instr(mapArray(x),"\\path\folder$") then
objNet.RemoveNetworkDrive "U:"
end if
next
To enumerate network drives.
Set NetDrives = WScript.CreateObject("WScript.Network").EnumNetworkDrives
For X = 0 to NetDrives.Count -1 Step 2
MsgBox NetDrives(x) & " " & NetDrives(x+1)
Next
To do what you want. We don't test then do usually. We do and test what happened. Testing most things usually takes up almost the same resources as doing.
On Error Resume Next
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemoveNetworkDrive "Y:"
Msgbox err.description
Again anything involving files and registry (because users delete) and networks or internet (because by nature are unreliable) should use error handling.

Get Administrotor Object in windows in different language

I've a vbscript to authenticate an user credentials, one part of my code is
Set objLocalGroup = GetObject("WinNT://./Administrators, group")
now this code fail in Windows(German and French Version), after debugging, I think the problem is that, in German Version, the Group corresponding to English "Administrators" was named "Administratoren"..
Is there any generic way to get the Object?
Thanks.
The administrators group has a well-known SID, so something like this should work:
Set wmi = GetObject("winmgmts://./root/cimv2")
Set admins = wmi.Get("Win32_SID.SID='S-1-5-32-544'")
Set objLocalGroup = GetObject("WinNT://./" & admins.AccountName & ",group")
Another way would be getting the name from the Win32_Group class:
Set wmi = GetObject("winmgmts://./root/cimv2")
qry = "SELECT * FROM Win32_Group WHERE SID = 'S-1-5-32-544'"
For Each group In wmi.ExecQuery(qry)
Set objLocalGroup = GetObject("WinNT://./" & group.Name & ",group")
Next

Two different names for Windows services (VB6)

I am having a minor problem automating the starting and stopping of services. When I open services.msc and look at the list of services, they all have names. However, when I run this code:
Dim objService As Object
Dim objSet As Object
IsServiceRunning = False
Set objSet = GetObject("winmgmts:").ExecQuery("SELECT * FROM Win32_Service")
For Each objService In objSet
If (UCase(strServiceName) = UCase(objService.Name)) And (UCase(objService.State) = UCase("Running")) Then
IsServiceRunning = True
End If
Next
The objService.Name value is not the same as the name in the list. For example, "Computer Browser" is just "browser", "Distributed File System" is "dfs", and "Net Logon" is "netlogon". Is there a way to pull the full, longer name for these services from this objService object? I can workaround this, but for the sake of clarity in the code I'd rather use the same value for determining if the service is running, making a NET START or NET STOP command line call, and logging.
Just use objService.Caption to access "long name" of service.
I discovered the name of the property like this:
For Each objService In objSet
For Each vElem In objService.Properties_
Debug.Print vElem.Name; "=";
Debug.Print vElem.Value
Next
Exit For
...
Next
Just put objService in watch window to find out Properties_ property. Put vElem in watch window too to find Name and the default property Value (besides IsArray, etc.) of SWbemProperty object.

Resources