Link to home
Start Free TrialLog in
Avatar of WellingtonIS
WellingtonIS

asked on

Script to query PC

I need a script to query Computer names, Model Numbers and OS info.  I have WMIC but I was hoping for something that doesn't time out as much? Actually I can query WMIC for the compuer names and Models but not the OS. And WMIC gives a lot of RPC errors too.
Avatar of akalyan911
akalyan911
Flag of India image

Hi WellingtonIS,
No way to do this with the command line utilities dsget and dsquery. A VBScript solution follows:
Option Explicit

Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset
Dim dtmDate, lngSeconds, str64Bit, strDN, strOS
Dim objShell, lngBiasKey, lngBias, k, intDays

' Specify number of days. If the computer password has not been changed
' in this number of days, the account is considered stale.
intDays = 180

' Determine date the specified number of days in the past.
' would just now be expired.
dtmDate = DateAdd("d", - intDays, Now())

' Retrieve DNS name of the domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

' Obtain local Time Zone bias from machine registry.
' This bias changes with Daylight Savings Time.
Set objShell = CreateObject("Wscript.Shell")
lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _
  & "TimeZoneInformation\ActiveTimeBias")
If (UCase(TypeName(lngBiasKey)) = "LONG") Then
  lngBias = lngBiasKey
ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then
  lngBias = 0
  For k = 0 To UBound(lngBiasKey)
    lngBias = lngBias + (lngBiasKey(k) * 256^k)
  Next
End If

' Convert the datetime value to UTC.
dtmDate = DateAdd("n", lngBias, dtmDate)

' Find number of seconds since 1/1/1601 for this date.
lngSeconds = DateDiff("s", #1/1/1601#, dtmDate)

' Convert the number of seconds to a string
' and convert to 100-nanosecond intervals.
str64Bit = CStr(lngSeconds) & "0000000"

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on computer objects where the password has not been changed
' in the specified number of days and the account is not disabled.
strFilter = "(&(objectCategory=computer)" _
  & "(pwdLastSet<=" & str64Bit & ")" _
  & "(!userAccountControl:1.2.840.113556.1.4.803:=2))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedName,operatingSystem"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 200
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
  ' Retrieve values and display in comma delimited format.
  strDN = adoRecordset.Fields("distinguishedName").Value
  strOS = adoRecordset.Fields("operatingSystem").Value
  Wscript.Echo """" & strDN & """,""" & strOS & """"
  ' Move to the next record in the recordset.
  adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close
 
Similar can be done in PowerShell.

Try this, may it works..
ASKER CERTIFIED SOLUTION
Avatar of Dale Harris
Dale Harris
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WellingtonIS
WellingtonIS

ASKER

can this be modified to print to a csv?
I know you prefer no WMIC but,
WMIC OS
for /f "skip=1 delims=^|" %a in ('wmic os get Name') do echo %a

Open in new window

I don't think I have the quest tools installed.
For /f...  I need a list of PCs?  and wmic os get name is only going to give me the OS name I need the PC name and model number too
Just installed the quest tools.  Running now...
It outputs everything into the same directory as you run the script from.  All you have to do is open excel and click the Data tab.  Then click From Text and separate by semicolon (;) when using the wizard.  The headings will also be useful for you.
I'm just running.  What's the file name going to be??? Or will it just pop when it ends?
The file outputted is computerinfo.txt.  It will fill up as it's running so you can check on the data it's getting.
You can add the command
 notepad .\computerinfo.txt 

Open in new window

if you want it to open it up.
I was just giving a point in the script. This is crude but:
@echo off

for /f %%s in ('dsquery computer "OU=ComputerOU,DC=Domain,DC=COM" -limit 0 -o rdn ^|sort') do call :sysinfo %%s
pause
exit

:sysinfo
set sysname=%1
set sysname=%sysname:"=%
echo %sysname% >>sysinfo.csv
for /f "skip=1 delims=^|" %%a in ('wmic /node:%sysname% os get Name') do echo %%a >>sysinfo.csv
wmic /node:%sysname% computersystem get model >>sysinfo.csv
echo. >>sysinfo.csv

goto :eof

Open in new window

This worked great! Thanks.  To bad it's not a csv so I can seperate it out. But thanks!
You can easily separate it out in excel using the "Data" tab, and selecting "From Text".  When it takes you through the wizard, ensure you're separating by semicolon and each column will have it's own data.

I'm glad it worked for you!  You could use the Export-CSV option, but I prefer using text files and importing into Excel.
I did it's just a mess but I got the info  which is most important!  thanks again for your help.
You're welcome.  I'm glad I could help!