Thursday, January 26, 2012

MS: Script Get List .NET Framework version for list of computers

One of my colleague in the WIndows Operations team asked me to create a VBscript to get all .NET Framework Version installed on a list of servers (provided in a .txt file). I created the script using blocks so the team can reuse it for multiples tasks.

To use the script: Cscript NetFramework.vbs , where is the text file with a list of servers or machines.


'--- script start here--
On Error Resume Next

Const HKEY_LOCAL_MACHINE = &H80000002
RegistryRoot= HKEY_LOCAL_MACHINE
Dim NetF(10)
Dim Version(10)

NetF(1) = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322"
Version(1) = "1.1"
NetF(2) = "Software\Microsoft\NET Framework Setup\NDP\v2.0.50727"
Version(2) = "2.0"
NetF(3) = "Software\Microsoft\NET Framework Setup\NDP\v3.0"
Version(3) = "3.0"
NetF(4) = "Software\Microsoft\NET Framework Setup\NDP\v3.5"
Version(4) = "3.5"
NetF(5) = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client"
Version(5) = "4.0"
NetCount = 5

Set objArgs = WScript.Arguments
if objArgs.Count = 0 then
WScript.Echo "Command line parameters not specified"
WScript.Echo "Usage: Cscript NetFramework.vbs "
WScript.quit
end if
ServerList = trim(objArgs(0))
OutputFile = "NetF-Report.csv"

'Create Header
Header="ServerName"
For I=1 to NetCount
Header=Header & "," & Version(I)
Next

'Record loop for servers
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFSO.OpenTextFile(ServerList, 1)
Info=Header & vbcrlf & CSVLine
Do Until objInFile.AtEndOfStream
strLine = objInFile.ReadLine
strComputer = strLine
wscript.echo "Checking machine " & Ucase(strComputer)
Info = Info & GetServerInfo(strComputer) & vbcrlf & CSVLine
Loop

'delete existing report file
objFSO.DeleteFile(OutputFile)

'create new report file
Set objFile = objFSO.CreateTextFile(OutputFile)
objFile.Write Info
objFile.Close

'*********************
'** Get Server Info **
'*********************
function GetServerInfo(ServerName)
Result=ServerName
if Ping(ServerName)=True then
For I=1 to NetCount
if ReadReg(ServerName, RegistryRoot, NetF(I), "Install")=True then
SPVersion=ReadRegValue(ServerName, RegistryRoot, NetF(I), "SP")
if SPVersion>0 then
Result=Result & ",SP" & SPVersion
else
Result=Result & ",NO SP"
end if
else
Result=Result & ",No"
end if
Next
else
Result = Result & ",N/A"
end if
GetServerInfo = Result
end function

'*******************
'** Read Registry **
'*******************
Function ReadReg(strComputer, RegistryRoot, strKeyPath, strValueName)
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objRegistry.GetDWordValue RegistryRoot,strKeyPath,strValueName,strValue
If IsNull(strValue) Then
ReadReg = False
Else
ReadReg = True
End If
End Function

'*************************
'** Read Registry Value **
'*************************
Function ReadRegValue(strComputer, RegistryRoot, strKeyPath, strValueName)
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objRegistry.GetDWordValue RegistryRoot,strKeyPath,strValueName,strValue
ReadRegValue = strValue
End Function

'******************
'** Ping Machine **
'******************
Function Ping(strHost)
Dim objPing, objRetStatus
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & strHost & "'")

for each objRetStatus in objPing
if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
Ping = False
else
Ping = True
end if
next
End Function