'Minimum age of account we want to report (In Days)
iDays = 60

Sub ReportUnusedAccounts(sComputer)
'Get a connection to the remote computer
Set poComputer = GetObject("WinNT://" & sComputer)
'Used as a flag to see if we could see any accounts on the remote computer
bFoundAccount=FALSE
'Loop through each account and see how old it is
For Each poItem in poComputer
 if poItem.Class="User" then
  'Update our counter so we know we found at least one account
  bFoundAccount=TRUE
  'Get the number of days since the last login
  iDuration=DateDiff("D",poItem.LastLogin,Date)
  'If greater than our trigger, then report
  if (iDuration > iDays) then
   sOutput = sOutput & sComputer & ", '" & poItem.Name & "', '" & poItem.LastLogin & "','" & iDuration & "'" & vbLF
  end if
 end if
next
'If we saw no accounts, then we had a problem connecting
if (bFoundAccount=FALSE) then
 Wscript.Echo sComputer & ", 'Could not connect, or access denied'"
else
 WScript.Echo (sOutput)
end if
end sub

'Make sure we got our argument from the command line

if (WScript.Arguments.Count=0) then
 Wscript.Echo "***************************************"
 WScript.Echo "* IntelliAdmin, LLC                   *"
 Wscript.Echo "* http://www.intelliadmin.com         *"
 WScript.Echo "* (Unused Accounts Reporter)          *"
 WScript.Echo "***************************************"
 WSCript.Echo "Missing Arguments. Usage shown below: "
 Wscript.Echo "Unused.vbs <DOMAIN>"
else

 'Get domain object so we can query a list of computers
 Set DomObj = GetObject("WinNT://" & WScript.Arguments(0) )

 'Filter only computer objects
 DomObj.Filter = Array("computer")

 'Loop through all computers and execute our RemoteService.exe program

 For Each sComputer In DomObj
  ReportUnusedAccounts sComputer.Name
 Next
end if
