Script per leggere il numero seriale del computer

Lo script in se non è nulla di che nel senso basta utilizzare WMI per leggere la proprietà SerialNumeber della classe Win32_BIOS, inrealtà lo scopo di questo script è quello di creare una template per simili esigenze ovvero:

  • Gestire script utilizzabili da riga di comando con possibilità di richiedere dati all’utente
  • Utilizzare WMI
  • Connettersi anche a computer remoti
  • Visualizzare in forma tabellare i risultati

Tutte queste esigenze di fatto sono semplicemente realizzabili se si utilizza l’oggetto Microsoft.CmdLib disponibile in Windows XP e Windows 2003 Server, per maggiori informazioni si veda il seguente Hey, Scripting Guy! Not-So-Hard Work Has Its Rewards, Too (purtroppo pare che su Vista non funzioni)

Per utilizzare l’oggetto occorre inserire il seguente codice nello script:

‘*** Include common module
Dim objCmdLib : Set objCmdLib = CreateObject( “Microsoft.CmdLib” )
‘*** Referring the script host to common module
Set objCmdLib.ScriptingHost = WScript.Application

Per controllare che lo script sia eseguito a riga di comando è possibile utilizzarre la funzione CheckScript:

‘*** Check whether the script is run using CScript
If CInt(objCmdLib.checkScript()) <> CONST_CSCRIPT Then
 WScript.Echo (UseCscriptErrorMessage)
        WScript.Quit(EXIT_UNEXPECTED)
End If

Per richiedere le credenziali all’utente da riga di comando non visualizzando la password è possibile utilizzare il seguente codice utilizzano l’oggetto ScriptPW disponibile in Windows XP e Windows 2003:

‘*** Require connection information to user
Dim strComputer, strDomain, strUser, strPassword, objPassword

Wscript.StdOut.Write “Please enter computer name or IP Address (. for local host):”
strComputer = Wscript.StdIn.ReadLine

If strComputer <> “.” Then
 Wscript.StdOut.Write “Please enter domain (null for log on to computer):”
 strDomain = Wscript.StdIn.ReadLine
        If strDomain = “” Then  strDomain = strComputer

 Wscript.StdOut.Write “Please enter your user name (null for Administrator):”
 strUser = Wscript.StdIn.ReadLine
        If strUser = “” Then strUser = “Administrator”
        strUser = strDomain & “\” & strUser

 Set objPassword = CreateObject(“ScriptPW.Password”)
 Wscript.StdOut.Write “Please enter your password:”
 strPassword = objPassword.GetPassword()
        Wscript.StdOut.WriteLine
End If

Per ottenere il servizio wmi è possibile utilizzare la funzione WMIConnect:

‘*** Connection
Dim blnLocalConnection : blnLocalConnection = False
Dim objService

If NOT objCmdLib.wmiConnect(“root\cimv2” , _
 strUser , strPassword , _
        strComputer , blnLocalConnection , _
        objService) Then
 Wscript.StdOut.WriteLine(ConnectionErrorMessage)        
        WScript.Quit(EXIT_METHOD_FAIL)
End If

Per visualizzare i dati in forma tabellare è possibile utilizzare la funzione ShowResults:

‘*** Set Show Results Format
Dim arrHeader : arrHeader = Array(“Element”, “Value”)
Dim arrMaxLength : arrMaxLength = Array(15, 25)
Dim strFormat : strFormat = “Table”
Dim blnPrintHeader : blnPrintHeader = True
Dim arrBlnHide : arrBlnHide = Array(False, False)
Dim arrResultsArray()
Dim index : index = 0

‘*** Read info
Dim objQuery, objQueryItem

‘*** Read info Computer System
Set objQuery = objService.ExecQuery(“Select Model from Win32_ComputerSystem”)

For Each objQueryItem in objQuery
  ReDim Preserve arrResultsArray(index)
  arrResultsArray(index) = Array(“Model”, objQueryItem.Model)
  index = index + 1
Next

‘*** Read info BIOS
Set objQuery = objService.ExecQuery(“Select SerialNumber from Win32_BIOS”)

For Each objQueryItem in objQuery
  ReDim Preserve arrResultsArray(index)
  arrResultsArray(index) = Array(“Serial Number”, objQueryItem.SerialNumber)
  index = index + 1
Next

‘*** Show System Information
Wscript.StdOut.WriteLine
objCmdLib.ShowResults arrHeader, arrResultsArray, arrMaxLength, _
 strFormat, blnPrintHeader, arrBlnHide

Per approfindimenti sulle condizioni necessarie per connettersi trmite WMI a computer remoti si veda WMI Isn’t Working! mentre per imparare ad utilizzare funzionalità interessanti il mio consiglio è quello di sbirciare gli scripti già presenti nel sistema, in Windows XP  \Windows\System32.

Lo script completo è disponible al seguente SysInfo.vbs (tx dx Salva oggetto se avete problemi a scaricarlo).