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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: