DevAdmin Blog

Blog di Ermanno Goletto (Microsoft MVP Directory Services - MCITP - MCTS - MCSA - MCP)
posts - 887, comments - 447, trackbacks - 13

My Links

News

Avatar

Curriculum Vitae

Visualizza il profilo di Ermanno Goletto su LinkedIn


Il contenuto di questo blog e di ciascun post viene fornito “così come é”, senza garanzie, e non conferisce alcun diritto. Questo blog riporta il mio personale pensiero che non riflette necessariamente il pensiero del mio datore di lavoro.

Logo Creative Commons Deed


Logo SysAdmin.it SysAdmin.it Staff


Logo TechNet Forum TechNet Italia @ForumTechNetIt Follow TechNet Forum on Twitter


Logo MVP


Ermanno Goletto Follow ermannog on Twitter

Article Categories

Archives

Post Categories

Blogs

Friends

Knowledge Base

MVP Sites

Resources

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

Print | posted on Thursday, May 15, 2008 1:42 PM | Filed Under [ Links Code, Snippets & Scripts IT ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 6 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET