How would I verify that unused NIC's in a Windows Server are disabled? - vbscript

I need to check a Windows Server (many of them a day) and just verify that Unused NIC's are disabled. This is just one of many checks I'm doing.
I'm trying to figure out how I can do this and this is my following code.
It gives me an error with objNetwork.PhysicalAdapter saying
Object doesn't support this property or method:
'objNetwork.PhysicalAdapter'
Sub CheckUnusedNICs()
WScript.Echo("Check for unused NICs")
WScript.Echo("------------------------------------")
Set colNetwork = objWMISrvc.ExecQuery("SELECT * from Win32_NetworkAdapter")
For each objNetwork in colNetwork
WScript.Echo objNetwork.AdapterTypeID & vbCrLf
WScript.Echo objNetwork.PhysicalAdapter
'If (objNetwork.AdapterTypeID = 0 AND objNetwork.PhysicalAdapter = True) Then
' WScript.Echo("Placeholder")
' End If
Next
End Sub
I'm by no means proficient in VBScript but I'm learning it as I go.

The WMI Win32_NetworkAdapter class doesn't have a PhysicalAdapter property.
Use NetConnectionStatus and ConfigManagerErrorCode properties instead.
A device would be:
Enabled and connected if:
NetConnectionStatus = 2
Enabled and no cable is plugged in if:
NetConnectionStatus = 7
Disconnected due to disabled device if:
(NetConnectionStatus = 0) and (ConfigManagerErrorCode = 22)
More details about Win32_NetworkAdapter class including the full list of the above codes can be found at:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394216(v=vs.85).aspx

Related

How to get the full computer name using Test Complete

How to get the full computer name using Test Complete
For example,
Computer Name : RAMAKRISHNA
Full Computer Name : RAMAKRISHNA.XYZ.COM
Domain Name : XYZ.COM
Using TestComplete, I have tried the following:
log.Message sys.HostName 'Gives "RAMAKRISHNA"
log.Message sys.DomainName 'Gives "XYZ"
Here, I am missing to get ".COM" using TestComplete
Please help me to get the full computer like RAMAKRISHNA.XYZ.COM
You can do this using WMI.
Sub Test
Log.Message getFullPCName
End Sub
Function getFullPCName
Set cSystem = WMI.Service.InstancesOf("Win32_ComputerSystem").ItemIndex(0)
getFullPCName = cSystem.Name & "." & cSystem.Domain
End Function
Windows Management Instrumentation technology provides ways to manage Windows settings and operations. You can use the Win32_ComputerSystem WMI class to get information on the system. TestComplete provides an easy way to work with WMI using the corresponding object. Find more information in the WMI Object help topic.
Try this if it helps:
Set wshShell = CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
WScript.Echo "Computer Name: " & strComputerName & "." & GetDomainName
Function GetDomainName()
Dim Info
Set Info = CreateObject("AdSystemInfo")
GetDomainName = Info.DomainDNSName
End Function

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 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.

Unlock bitlocker drive with VBScript

I'm trying to script out the unlocking of a bitlocker drive using a DRA certificate. I'm attempting to use the WMI Method UnlockWithCertificateFile and I can't for the life of me figure out what i'm doing wrong or even find an example.
I know the certificate and pin work because i can manually unlock the drive using manage-bde -unlock....
when i run my script i get a return value of -2146885623 wich i've looked up to be -2146885623, "Cannot find the requested object."
i'm not sure what object its talking about.
here is the code i'm using (minus the pin)
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\Security\MicrosoftVolumeEncryption")
' Obtain an instance of the the class
' using a key property value.
Set objShare = objWMIService.Get("Win32_EncryptableVolume.DeviceID='\\?\Volume{a2965903-4af0-11e2-be65-806e6f6e6963}\'")
' Obtain an InParameters object specific
' to the method.
Set objInParam = objShare.Methods_("UnlockWithCertificateFile"). _
inParameters.SpawnInstance_()
' Add the input parameters.
objInParam.Properties_.Item("PathWithFileName") = "D:\BitLocker.pfx"
objInParam.Properties_.Item("Pin") = "PinCode
' Execute the method and obtain the return status.
' The OutParameters object in objOutParams
' is created by the provider.
Set objOutParams = objWMIService.ExecMethod("Win32_EncryptableVolume.DeviceID='\\?\Volume{a2965903-4af0-11e2-be65-806e6f6e6963}\'", "UnlockWithCertificateFile", objInParam)
' List OutParams
Wscript.Echo "Out Parameters: "
Wscript.echo "ReturnValue: " & objOutParams.ReturnValue
has anyone ever used this method or can anyone see something i may be doing wrong? also i'm using the windows 8 commandline recovery option, i'm not sure if that makes a difference because i have tested calling other methods and wmi and scripting seem to be fully implemented.

It keeps saying ActiveX component can't create object: 'Shell.LocalMachine'

When I run the code, I get an error saying
ActiveX component can't create object: 'Shell.LocalMachine'
Class MachineName
Private internal_ComputerName
Public Function SetMachineName
Set objComputer = CreateObject("Shell.LocalMachine")
internal_ComputerName = objComputer.MachineName
End Function
Public Property Get GetMachineName
GetMachineName = internal_ComputerName
End Property
End Class
Dim objMachine
Set objMachine = New MachineName
objMachine.SetMachineName
thanks for this. I am having the same problems when using this Shell.Localmachine on my windows 7 64 bit machine when i try to run a simple vbscript code. I had to default to WScript.Network instead:
'just a test script
'set objComputer = CreateObject("Shell.LocalMachine")
'wscript.echo "computer name" & objcomputer.machinename
Set objWshNet = CreateObject("WScript.Network")
wscript.echo "computer name : " & objwshnet.computername
Morbo said "Must admit I hadn't come across that object before. I'd normally create a "WScript.Network" object and get the ComputerName property. If you're diagnosing "Shell.LocalMachine" I can tell you that on my copy of XP it is provided by system32\shgina.dll"

Resources