Category Archives: Windows

WMI Script – Generating Physical Host Name for Virtual Machines running on Hyper-v

‘——————————————————————————————————
‘——————————————————————————————————
‘   Author         : Satyendra Singh
‘   Last Modified  : 12 Sep 2015
‘   Description    : This script will generate the physicalhostname for virtual machines ( hyper-v host)
‘   Prerequisite   : servernames.txt needs to be populated with targeted Windows server host name
‘   Version        : 1.0
‘   Outout    : This will generate ‘hypervhostout.csv’ for output
‘——————————————————————————————————
‘——————————————————————————————————

Option Explicit
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForWriting = 2

Const PATH_TO_INPUT = “servernames.txt”

Dim fso
Set fso = WScript.CreateObject(“Scripting.FileSystemObject”)

Dim shl
Set shl = WScript.CreateObject(“WScript.Shell”)

Dim input1
Set input1 = fso.OpenTextFile(PATH_TO_INPUT)
‘——————————————————————————————————————————

Dim oReg
Dim input
Dim strKeyPath
Dim strValueName
Dim strValue
Dim arrComputers
Dim output
DIm wmiService

Dim hostname
Dim line
Dim exec
Dim pingResults
Set output = fso.OpenTextFile(“hypervhostoutput.csv”,ForWriting,True)

arrComputers = Split(fso.OpenTextFile(“servernames.txt”).ReadAll, vbNewLine)
output.WriteLine “Server(P/V):” & “Hyper-v Host(Y/N):” & “Server Comment(if Applicable):”

On Error Resume Next

for each input in arrComputers

Set exec = shl.Exec(“ping -n 2 -w 1000 ” & input)
pingResults = LCase(exec.StdOut.ReadAll)

If InStr(pingResults, “reply from”) Then

WScript.Echo “Reply From: ” & input
On Error Resume Next

Set wmiService = GetObject(“winmgmts:\\” & input & “\root\CIMV2”)

If Not Err.Number = 0 Then
output.WriteLine input & “:Error: ” & Err.Description
‘output.WriteLine input & “:Error: ”
WScript.Echo input & “:Error: ” & Err.Description
On Error GoTo 0
Else
On Error GoTo 0
hostname = input
Set oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & input & “\root\default:StdRegProv”)
strKeyPath = “SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters”
strValueName = “PhysicalHostName”
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

If IsNull(strValue) Then

output.writeline input & “:” & “Physical Host”
else
output.WriteLine input & “:” & strValue
end if

End If

Else
output.WriteLine input & “:No Response”
WScript.Echo input & “:No Response”

End If

next
output.writeline vbCrLf
‘output.Close
input1.Close
Set wmiService = Nothing

Advertisements

WMI Script to Capture Windows Server Details

‘——————————————————————————————————
‘——————————————————————————————————
‘   Author         : Satyendra Singh
‘   Last Modified  : 27 August 2015
‘   Description    : This script will generate the Inventory
‘   Prerequisite   : servernames.txt needs to be populated with targeted Windows server host name
‘   Version        : 1.0
‘—————————————————————————————————————————
‘—————————————————————————————————————————

Option Explicit

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Const PATH_TO_INPUT = “servernames.txt”

Dim fso
Set fso = WScript.CreateObject(“Scripting.FileSystemObject”)

Dim shl
Set shl = WScript.CreateObject(“WScript.Shell”)

Dim input
Set input = fso.OpenTextFile(PATH_TO_INPUT)

Dim output
‘—————————————————————————————————————————————-`

wscript.echo ” ”
wscript.echo ” ”

Dim dtmLocalTime, dtmMonth, dtmDay, dtmYear,dtmNewDate
Dim OutputFS
Dim strComputer
Dim objItem

strComputer = “.”

Set wmiService = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set wmiResults = wmiService.ExecQuery(“Select * from Win32_OperatingSystem”)

For Each objItem in wmiResults
dtmLocalTime = objItem.LocalDateTime
dtmMonth = Mid(dtmLocalTime, 5, 2)
dtmDay = Mid(dtmLocalTime, 7, 2)
dtmYear = Left(dtmLocalTime, 4)

Next

dtmNewDate = dtmDay & “-” & dtmMonth & “-” & dtmYear

OutputFS = “WinDatainventory-” & dtmNewDate & “.xls”
wscript.echo “===========================================================================”
wscript.echo ” Data Validation Script has been initiated on ” & dtmDay & “/” & dtmMonth & “/” & dtmYear
wscript.echo “Script Output File Location: ” & OutputFS
wscript.echo “===========================================================================”
Set output = fso.CreateTextFile(OutputFS, True)

‘——————————————————————————————————————————————`

output.WriteLine “ServerName;Server Role;HardwareType;Model;Serial Number;SystemType;Operating System;CPUModel;CPU-Socket;CPU-Core;CPULogicalCore;Memoryr(GB); Network Connection Name (IP Enabled) ;Network Device Name (IP enabled); Network Speed(Mbps);IPAddress;Subnet;IPGateway;Total Disk-Size(GB);Free Space(GB);UsedSpace(GB);DNSservers;DNSDomain;WINS;PageFileSize(GB-Blank field if automatic manage);CD-ROM;Time-Zone;Total Physical/Virtual NICs; Total Physical/Virtual NICs(Count);Total Physical NiCs(Count);HBAModel;HBADescription;HBADriveVersion;HBADriveName;HBAFirmwareVerion;HBA(Count)”

Dim wmiService
Dim wmiResults

Dim hostname
Dim ostype
Dim model
Dim systemt
Dim biosversion
Dim operatingSystem
Dim serialNumber
Dim cpumodel
Dim cpusocket
Dim cpucore
DIm cpulogicalprocessor
Dim memory
Dim cdromdrive
Dim ipaddress
Dim macaddress
Dim dhcps
Dim dnss
Dim ipsubnet
Dim product
Dim ipaddress1
Dim dummy
Dim disksize
Dim freespace
Dim dummydata
Dim strUsedSpace
Dim Newspaceallocation
Dim servicepack
Dim DNSservers
Dim DNSDomain
Dim WINS
Dim Pagefilemaxsize
Dim CurrentTimeZone
Dim speed
Dim Names
Dim ConnectionID
Dim ipgw
Dim Atype

Dim line
Dim exec
Dim pingResults
While Not input.AtEndOfStream
line = input.ReadLine
hostname = “”
ostype = “”
model = “”
systemt = “”
biosversion = “”
operatingSystem = “”
serialNumber = “”
cpumodel = “”
cpusocket=””
cpucore = “”
cpulogicalprocessor = “”
memory = “”
disksize = “”
ipaddress = “”
ipaddress1 = “”
macaddress = “”
dhcps = “”
dnss = “”
ipsubnet = “”
product = “”
dummy = “”
freespace=””
dummydata=”null”
strUsedSpace = “”
Newspaceallocation = “”
servicepack = “”
DNSservers=””
DNSDomain=””
WINS=””
Pagefilemaxsize=””
cdromdrive=””
CurrentTimeZone=””
ipgw=””
ipsubnet=””

Names=””
speed=””
ConnectionID=””
Atype=””
Set exec = shl.Exec(“ping -n 2 -w 1000 ” & line)
pingResults = LCase(exec.StdOut.ReadAll)

If InStr(pingResults, “reply from”) Then
WScript.Echo “Reply From: ” & line
On Error Resume Next

Set wmiService = GetObject(“winmgmts:\\” & line & “\root\CIMV2”)

If Not Err.Number = 0 Then
output.WriteLine line & “;Error: ” & Err.Description
WScript.Echo line & “;Error: ” & Err.Description
On Error GoTo 0
Else
On Error GoTo 0
hostname = line

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_Bios”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

On Error Resume Next

Dim item
On Error Resume Next

For Each item In wmiResults
serialNumber = Trim(item.SerialNumber)
‘product = Trim(item.product)

Next

Dim z
Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_NetworkAdapter  WHERE NetEnabled=TRUE”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults

for each z in item.NetConnectionID

ConnectionID = ConnectionID & ” | ” & Trim(item.NetConnectionID)
Next
for each z in item.Name
Names = Names & ” | ” & Trim(item.Name)
Next

for each z in item.Speed
speed = speed  & “|” & Int((item.Speed)/1000000) & “Mbps”
Next
Next

Dim i
Dim arrLines
Dim alines
Dim j
Dim filter
Dim k
Dim pnc
Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_NetworkAdapter Where PhysicalAdapter=True”)

For Each item In wmiResults
Atype = Atype & ” | ” & item.Description
‘WScript.Echo Atype
arrLines = Split(Atype, “|”)
i = Ubound(arrLines)

k = 0
filter = Split(Atype, ” “)
for each j in filter
‘if Instr(j, “Virtual”) Then
if Instr(j, “Virtual”) or Instr(j, “Microsoft”) or Instr(j, “vpn”) or Instr(j, “Bluetooth”) then
k = k + 1
end if
Next

pnc = Int(i-k)

Next

‘HBA Details
Dim hbamodel
Dim hbadescription
Dim hbadriverversion
Dim hbafirmwareversion
DIm hbadrivename
Dim h
Dim hbacount
Dim hi
Dim hbaarray

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM MSFC_FCAdapterHBAAttributes”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)
for each item in wmiResults

for each h in item.Model
hbamodel = hbamodel & ” | ” & Trim(item.Model)
Next

for each h in item.ModelDescription
hbadescription = hbadescription & ” | ” & Trim(item.ModelDescription)
Next

for each h in item.DriveVersion
hbadriverversion = hbadriverversion & ” | ” & Trim(item.DriveVersion)
Next

for each h in item.DriveName
hbadrivename = hbadrivename & ” | ” & Trim(item.DriveName)
Next

for each h in item.Firmwareversion
hbafirmwareversion = hbafirmwareversion & ” | ” & Trim(item.FirmwareVersion)
Next

hbacount = hbacount & ” | ” & item.ModelDescription
‘WScript.Echo  hbacount
hbaarray = Split(hbacount, “|”)
hi = Ubound(hbaarray)

Next
Dim serverrole

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_ServerFeature”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
serverrole = Trim(item.Name)
Next
Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_ComputerSystem”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults
ostype = Trim(item.Manufacturer)
model = Trim(item.model)
systemt = Trim(item.systemtype)
On Error Resume Next
cpusocket = trim(item.NumberOfProcessors)
cpulogicalprocessor = Trim(item.NumberOfLogicalProcessors)
On Error Resume Next
DNSDomain = Trim(item.Domain)

Next

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_OperatingSystem”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults
servicepack = “SP” & Trim(item.servicepackmajorversion)
operatingSystem = Trim(item.Name)
operatingSystem = Split(operatingSystem, “|”)(0)
operatingSystem = operatingSystem & ” ” & servicepack
memory = Round(Trim(item.TotalVisibleMemorySize) / 1048576, 2)

dtmLocalTime = objItem.LocalDateTime
dtmMonth = Mid(dtmLocalTime, 5, 2)
dtmDay = Mid(dtmLocalTime, 7, 2)
dtmYear = Left(dtmLocalTime, 4)

Next

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_Processor”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults
cpumodel = Trim(item.Name)
On Error Resume Next
cpucore = Trim(item.numberofcores)
On Error Resume Next
Next
‘Dim manojjee

‘Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_DiskDrive where DeviceID !=””, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)
‘For Each item In wmiResults

‘manojjee =  Int(item.size/1073741824)
‘Next
Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_LogicalDisk WHERE DriveType=3”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults
disksize = disksize & Trim(item.DeviceID) & ” ” & Round(Trim(item.Size) / (1073741824), 2) & “GB ”
freespace = freespace & Trim(item.DeviceID) & ” ” & Round(Trim(item.freespace) / (1073741824), 2) & “GB ”
strUsedSpace = strUsedSpace & Trim(item.DeviceID) & ” ” &  Int((item.Size-item.FreeSpace)/1073741824) & “GB ”
Next
Dim x

Set wmiResults = wmiService.ExecQuery(“Select * FROM Win32_NetworkAdapterConfiguration   WHERE IPEnabled=TRUE”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

n = 1
For Each item In wmiResults
for each x in item.IPAddress
ipaddress = ipaddress & ” | ” & Trim(x)
Next
If Not IsNull(item.DefaultIPGateway) Then
for each x in item.DefaultIPgateway
ipgw = ipgw & “GWIP ” & Trim(x)
Next
End if

If Not IsNull(item.IPSubnet) Then
for each x in item.IPSubnet
ipsubnet = ipsubnet & “|” & Trim(x)
Next
End if
If Not IsNull(item.DNSServerSearchOrder) Then
For each x in item.DNSServerSearchOrder
DNSservers = DNSservers & ” DNS:  ” & trim(x)
Next
End If

WINS = item.WINSPrimaryServer & ” ” & item.WINSSecondaryServer

Next
Set wmiResults = wmiService.ExecQuery(“Select * FROM Win32_PageFile”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)
On Error Resume Next
For Each item In wmiResults

Pagefilemaxsize = pagefilemaxsize & Trim(item.Drive) & ” ” & Round(Trim(item.MaximumSize) / 1024, 2) & ” GB ,”
Next

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_CDROMDrive”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults
diskcaps = Trim(item.Description)
cdromdrive = Trim(item.Drive)
Next

Set wmiResults = wmiService.ExecQuery(“SELECT * FROM Win32_timezone”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each item In wmiResults
CurrentTimeZone = Trim(item.Caption)
Next
‘output.WriteLine hostname & “;” & ostype  & “;”& model & “;”& SerialNumber & “;” & systemt &  “;” & operatingSystem & “;” & cpumodel & “;” & cpusocket & “;” & cpucore & “;” & cpulogicalprocessor & “;” & memory & “;” & ConnectionID & “;” & Names & “;” & speed & “;” & ipaddress & “;” & ipsubnet & “;” & ipgw & “;” & disksize & “;” & freespace & “;” & strUsedSpace & “;” & DNSservers & “;” & DNSDomain & “;” & WINS & “;” & Pagefilemaxsize & “;” & cdromdrive & “;” & CurrentTimeZone & “;” & Atype & “;” & i  & “;” & k & “;” & pnc
‘WScript.Echo hostname & “;” & ostype & “;”& model & “;” & SerialNumber &  “;” & systemt & “;” & operatingSystem & “;” & cpumodel & “;” & cpusocket & “;” & cpucore & “;” & cpulogicalprocessor & “;” & memory & “;” & ConnectionID & “;” & Names & “;” & speed & “;” & ipaddress & “;” & ipsubnet & “;” & ipgw & “;” & disksize & “;” & freespace & “;” & strUsedSpace & “;” & DNSservers  & “;” & DNSDomain & “;” & WINS & “;” & Pagefilemaxsize & “;” & cdromdrive & “;” & CurrentTimeZone & “;” & Atype & “;” & i  & “;” & k & “;” & pnc
output.WriteLine hostname & “;” & serverrole & “;” & ostype  & “;”& model & “;”& SerialNumber & “;” & systemt &  “;” & operatingSystem & “;” & cpumodel & “;” & cpusocket & “;” & cpucore & “;” & cpulogicalprocessor & “;” & memory & “;” & ConnectionID & “;” & Names & “;” & speed & “;” & ipaddress & “;” & ipsubnet & “;” & ipgw & “;” & disksize & “;” & freespace & “;” & strUsedSpace & “;” & DNSservers & “;” & DNSDomain & “;” & WINS & “;” & Pagefilemaxsize & “;” & cdromdrive & “;” & CurrentTimeZone & “;” & Atype & “;” & i  & “;” &  pnc & “;” & hbamodel & “;” & hbadescription & “;” & hbadriverversion & “;” & hbadrivename & “;” & hbafirmwareversion & “;” & hi
WScript.Echo hostname & “;” & serverrole & “;” & ostype & “;”& model & “;” & SerialNumber &  “;” & systemt & “;” & operatingSystem & “;” & cpumodel & “;” & cpusocket & “;” & cpucore & “;” & cpulogicalprocessor & “;” & memory & “;” & ConnectionID & “;” & Names & “;” & speed & “;” & ipaddress & “;” & ipsubnet & “;” & ipgw & “;” & disksize & “;” & freespace & “;” & strUsedSpace & “;” & DNSservers  & “;” & DNSDomain & “;” & WINS & “;” & Pagefilemaxsize & “;” & cdromdrive & “;” & CurrentTimeZone & “;” & Atype & “;” & i  & “;” &  pnc & “;” & hbamodel & “;” & hbadescription & “;” & hbadriverversion & “;” & hbadrivename & “;” & hbafirmwareversion & “;” & hi

 

End If
Else
output.WriteLine line & “;No Response”
WScript.Echo line & “;No Response”
End If
Wend

output.Close
input.Close

Set wmiService = Nothing
Set wmiresults = Nothing