Read Content from Text File VBS - vbscript

Hi Guys this script requires you to manually enter computer name to pull up Inventory.
If I can have all the computer's name in a text file that will be great.
I tried something like
strComputer = fso1.OpenTextFile("c:\Computers.txt",1)
But doesn't seems to work any idea?
Dim strIP, strSubnet, strDescription, lnX, strcomputer, objwmiservice, colitems, objitems
strComputer = InputBox ("Enter the Computer name to get its Inventory:-")
lnX = 1
' ********************************************************************************************
'Section to change a filename using timestamps
strPath = "C:\"
strMonth = DatePart("m", Now())
strDay = DatePart("d",Now())
if Len(strMonth) = 1 then
strMonth = "0" & strMonth
else
strMonth = strMonth
end if
if Len(strDay) = 1 then
strDay = "0" & strDay
else
strDay = strDay
end if
strFileName = DatePart("yyyy",Now()) & strMonth & strDay
strFileName = Replace(strFileName,":","")
' ********************************************************************************************
'Variable Declarations
Const ForAppending = 8
'Get CompName
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel = impersonate}!\\" & strComputer & "\root\cimv2")
' ********************************************************************************************
'Get Operation System & Processor Information
' ********************************************************************************************
Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor")
For Each objItem in colItems
CompName = objItem.SystemName
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
if objFSO.FileExists(strPath & CompName & "_" & "_Inventory.txt") then
WScript.Quit
end if
'Set the file location
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strPath & CompName & "_" & "Inventory.txt", ForAppending, True)
' ********************************************************************************************
'Print HEADER
' ********************************************************************************************
objTextFile.Write "********************************************************************************************" & VBCRLF & VBCRLF
objTextFile.Write " COMPUTER INVENTORY " & VBCRLF
objTextFile.Write " DATE: " & FormatDateTime(Now(),1) & " " & VBCRLF
objTextFile.Write " TIME: " & FormatDateTime(Now(),3) & " " & VBCRLF & VBCRLF
objTextFile.Write "******************************************************************************************** " & VBCRLF & VBCRLF
' ********************************************************************************************
'Get Processor Information
' ********************************************************************************************
objTextFile.Write "COMPUTER" & VBCRLF
Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor")
For Each objItem in colItems
If lnX = 1 Then
objTextFile.Write " COMPUTER NAME: " & objItem.SystemName & VBCRLF
objTextFile.Write " PROCESSOR: " & objItem.Name & vbCrLf
lnX = lnX + 1
End If
Next
' ********************************************************************************************
'Get Computer Manufacturer and RAM details
' ********************************************************************************************
Set colSystems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objSystems In colSystems
objTextFile.WriteLine " MANUFACTURER: " & objSystems.Manufacturer
objTextFile.WriteLine " MODEL: " & objSystems.Model
objTextFile.WriteLine " DOMAIN: " & objsystems.Domain
objTextFile.Write " RAM: " & Round (objSystems.TotalPhysicalMemory / 1048576, 0) & " MB "& VBCRLF
Next
' ********************************************************************************************
' Get the Serial number/ Service Tag of the system
' ********************************************************************************************
Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
objTextFile.Write " SERVICE TAG: " & objSMBIOS.SerialNumber & VBCRLF
Next
' ********************************************************************************************
'Get BIOS Information
' ********************************************************************************************
objTextFile.Write VBCRLF & "BIOS INFO" & vbCrLf
Set colBIOS = objWMIService.ExecQuery("Select * from Win32_BIOS")
For each objItem in colBIOS
objTextFile.WriteLine " BIOS MANUFACTURER: " & objItem.Manufacturer
objTextFile.WriteLine " BIOS VERSION: " & objItem.Name
Next
' ********************************************************************************************
'Get OS Information
' ********************************************************************************************
objTextFile.Write VBCRLF & "OS INFO" & vbCrLf
Set colSettings = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colSettings
objTextFile.Write " OPERATING SYSTEM: " & objOperatingSystem.Caption & " {Enter the product key}" & VBCRLF
objTextFile.Write " SERVICE PACK: " & objOperatingSystem.Caption & " Service Pack "& objOperatingSystem.ServicePackMajorVersion & "." & objOperatingSystem.ServicePackMinorVersion & VBCRLF
Next
' ********************************************************************************************
'Get Logical Disk Size and Partition Information
' ********************************************************************************************
objTextFile.Write VBCRLF & "MEMORY" & VBCRLF
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 3")
For Each objDisk in colDisks
intFreeSpace = objDisk.FreeSpace
intTotalSpace = objDisk.Size
pctFreeSpace = intFreeSpace / intTotalSpace
objTextFile.Write objDisk.Name & "\ (" & objDisk.FileSystem & ") " & Round((objDisk.Size/1000000000),4) & " GB ("& Round((intFreeSpace/1000000000)*1.024,4) & " GB Free Space)" & VBCRLF
Next
' ********************************************************************************************
'Get NETWORK ADAPTERS information
' ********************************************************************************************
objTextFile.Write VBCRLF & "NETWORK" & VBCRLF
Set colNicConfigs = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objNicConfig In colNicConfigs
strDescription = objNicConfig.Caption
strMACAddress = objNicConfig.MACAddress
strDHCP = objNicConfig.DHCPEnabled
For Each strIPAddress In objNicConfig.IPAddress
strIP = strIPAddress 'Assign IP Address to variable
For Each strIPSubnet In objNicConfig.IPSubnet
strSubnet = strIPSubnet 'Assign Subnet to variable
Next
objTextFile.Write " NETWORK ADAPTER: " & strDescription & VBCRLF
objTextFile.Write " IP ADDRESS: " & strIP & VBCRLF
objTextFile.Write " MAC ADDRESS: " & strMACAddress & vbCrLf
objTextFile.Write " DHCP ENABLED: " & strDHCP & vbCrLf & vbCrLf
Next
Next
Set colNicConfigs = NOTHING
' ********************************************************************************************
'Get GRAPHICS ADAPTERS information
' ********************************************************************************************
objTextFile.Write VBCRLF & "GRAPHICS CARD" & VBCRLF
Set colGraphics = objWMIService.ExecQuery ("Select * from Win32_DisplayControllerConfiguration")
For Each objGraphics in colGraphics
objTextFile.WriteLine " GRAPHICS CARD: " & objGraphics.Name & VBCRLF
Next
' ********************************************************************************************
' Get the list of Installed software
' ********************************************************************************************
objTextFile.Write VBCRLF & "SOFTWARE INSTALLED" & VBCRLF
Set colSoftware = objWMIService.ExecQuery("Select * from Win32_Product")
For Each objSoftware in colSoftware
objTextFile.WriteLine objSoftware.Description
Next
'Close text file after writing logs
objTextFile.Write VbCrLf
objTextFile.Close
'Clean Up
WScript.Echo "Inventory Complete "

The .OpenTextFile() call in your
strComputer = fso1.OpenTextFile("c:\Computers.txt",1)
just opens the file, but does not read the file's content that should be assigned to strComputer. So use
strComputer = fso1.OpenTextFile("c:\Computers.txt").ReadLine()

Related

vbscript not accessing outlook 2013/2016 subfolder off the inbox using redemption

I am running the below vbscript on outlook 2013/2016 and having issues trying to read emails in sub folders off the Inbox. I can read the Inbox emails. . Can anyone point me in the right directions?
thanks in advance.
Function CheckMail(strMailBox,strFolder,strFolderAbbr,strDetails)
'
olFolderInbox = 6
set Session = CreateObject("Redemption.RDOSession")
'
Set objOutlook = CreateObject("Outlook.Application")
Session.MAPIOBJECT = objOutlook.Session.MAPIOBJECT
set Store = Session.Stores.GetSharedMailbox(strMailBox)
set Inbox = Store.GetDefaultFolder(olFolderInbox)
'
Wscript.Echo "MailBox: " & Store.Name & " - " & Inbox.Name
If strFolder = "" then
set SubFolder = Inbox
strFolderAbbr = strMailBox & " Inbox"
Else
'set SubFolder = Inbox.Folders(strFolder)
'
set SubFolder = Inbox.Folders.Item(strFolder)
'
strFolderAbbr = strMailBox & " Inbox\" & strFolder
Wscript.Echo " Sub Folder: " & SubFolder
End If
'
nItems = SubFolder.Items.Count
If nHowlong > 1 Then
nHowlong = Round((nItems/110)/60,0)
strTime = " Hour(s)!!"
Else
nHowlong = Round(nItems/110,0)
strTime = " Minute(s)!!"
End If
Wscript.Echo nItems & " - Emails in folder " & strFolderAbbr & " About " & nHowlong & strTime
'" - " & nItems
'
for each Msg in SubFolder.Items
nCounter = nCounter + 1
'Wscript.Echo "Item " & nCounter & "/" & nItems & vbCRLF & "EID: " & Msg.EntryID & vbCRLF & "ABOUT: " & Msg.Subject & vbCRLF & "FROM: " & Msg.SenderName & vbCRLF & "LEVEL: " & IIf(Msg.Importance=2,"High",IIf(Msg.Importance=1,"Normal","Low")) & vbCRLF & "Status: " & IIf(Msg.UnRead, "Not Read", "Read") & vbCRLF & "Received: " & Msg.ReceivedTime & vbCRLF & "Body: " & Msg.Body
'& nCounter & "/" & nItems & vbCRLF & "EID: " & Msg.EntryID & vbCRLF & "ABOUT: " & Msg.Subject & vbCRLF & "FROM: " & Msg.SenderName & vbCRLF & "LEVEL: " & IIf(Msg.Importance=2,"High",IIf(Msg.Importance=1,"Normal","Low")) & vbCRLF & "Status: " & IIf(Msg.UnRead, "Not Read", "Read") & vbCRLF & "Received: " & Msg.ReceivedTime & vbCRLF & "Body: " & Msg.Body
'
' process all emails in the box
strRecords = strRecords & "REG-" &strFolderAbbr & "~" & Msg.Subject & "~" & Msg.ReceivedTime & "~" & IIf(Msg.UnRead, "Not Read", "Read") & "~" & Msg.SenderName & "~" &IIf(Msg.Importance=2,"High",IIf(Msg.Importance=1,"Normal","Low")) & "*"
On Error Resume Next
err.clear
if Err Then
'WScript.Echo "ReceivedTime was null"
End If
On Error GoTo 0
next
CheckMail = strRecords
End Function
Function IIf(bClause, sTrue, sFalse)
If CBool(bClause) Then
IIf = sTrue
Else
IIf = sFalse
End If
End Function
I finally figured out the code that works. I would first like to thank Dmitry for all the help he gave me. But this was a stuburn issue. the following code solved the problem. Please dont ask me to explain it just plain ole luck and trail and error.
set Session = CreateObject("Redemption.RDOSession")
Set objOutlook = CreateObject("Outlook.Application")
Session.MAPIOBJECT = objOutlook.Session.MAPIOBJECT
'Set Root foldedr of the mail box of the stores
set IPMRoot = Session.Stores.Item(strMailBox).IPMRootFolder
'Set the subfolder to the inbox
If strFolder = "" then
set subFolder = IPMRoot.Folders("InBox")
strFolderAbbr = strMailBox & " Inbox"
Else
'Set subfolder to subfolder chosen
set subFolder = IPMRoot.Folders("InBox").Folders(strFolder)
strFolderAbbr = strMailBox & " Inbox\" & strFolder
End If
'

Windows Event Log VBS script no result

We have powershell scripts setup for preforming offline backups. The executing of these scripts was failing due to an incorrect script execution causing powershell to prompt what user wants to do. Due to this the scripts timed out.
To detect script execution timeouts i'm trying to query the event log with a VBS script, but this allways returns 0 results.
I tried with this code:
LOG_FILE = "C:\jsco\Scripts\output.txt"
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Microsoft-Windows-TaskScheduler/Operational' AND EventCode = '332'")
For Each objEvent in colItems
writeLog "Category: " & objEvent.Category
writeLog "Category String: " & objEvent.CategoryString
writeLog "Computer Name: " & objEvent.ComputerName
writeLog "Data: " & objEvent.Data
writeLog "Event Code: " & objEvent.EventCode
writeLog "Event Identifier: " & objEvent.EventIdentifier
writeLog "Insertion Strings: " & objEvent.InsertionStrings
writeLog "Logfile: " & objEvent.Logfile
writeLog "Message: " & objEvent.Message
writeLog "Record Number: " & objEvent.RecordNumber
writeLog "Source Name: " & objEvent.SourceName
writeLog "Time Generated: " & objEvent.TimeGenerated
writeLog "Time Written: " & objEvent.TimeWritten
writeLog "Type: " & objEvent.Type
writeLog "User: " & objEvent.User
Next
Sub writeLog(strText)
Dim objFSO, objLogFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile(LOG_FILE, 8, True)
objLogFile.WriteLine strText
objLogFile.Close
End Sub
I allso tried this with:
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Security)}!\\" & strComputer & "\root\cimv2")
And:
Set objWMIService = GetObject("winmgmts:{(Security)}\\" & strComputer & "\root\cimv2")
As shown on the screencap, the event is defenatly in the the Event Log.
(source: jef at 149.210.139.155)
Does anyone have any idea what causes this and on how to fix this. Thanks for your time.

VBscript to target specific IP of NIC and change its DNS settings

I have currently been tasked to put together a script that will change the DNS settings of 15,000 ish servers. However, there is no common unique identifer of these NIC's other than their current DNS IP. My Question, Is it possible to somehow have my script do an ipfonfig /all and then if one of the NIC's reports back with the current DNS settings target that NIC for the new updated settings?
I was currently using the below script until i was made aware that some of the NIC will not be called "Production". Any suggestions are welcome! (powershell was not an option as we may be targeting some very old servers)
Dim strDns1
Dim strDns2
strDns1 = "10.10.10.10"
strDns2 = "10.10.10.10"
Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set dns name=""Production"" static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Production"" addr="& strDns2, 0, True
Set objShell = Nothing
WScript.Quit
You can do the ipconfig query with a script like this:
Set wso = CreateObject("WScript.Shell")
Set execo = wso.Exec("ipconfig /all")
Set stdout = execo.StdOut
While Not stdout.AtEndOfStream
cmdOutput = cmdOutput & VbCrLf & stdout.ReadLine
Wend
wscript.echo cmdOutput
The main problem is this is just a string, so you have to parse it yourself, which is annoying and probably error prone.
I would rather suggest you take a look at the wmi class
Win32_NetworkAdapterConfiguration
which has a lot of information on the network connections. Only caveat with this WMI class is that it stores many values in arrays instead of strings so you can not just query for everything easily. Either pick something to query against that is a string like DNSDomain or just handle the logic within the script.
I'm agree with Syberdoor , You should use the Wmi Class
Win32_NetworkAdapterConfiguration
This code give you some informations :
Call ListDNSInfo()
'********************************************************************
Sub ListDNSInfo()
Dim ComputerName,IPConfigSet,IPConfig,BailObtenu,BailExpirant
ComputerName="."
On error resume next
set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & ComputerName).ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE")
If Err.Number <> 0 Then
wscript.echo " - non accessible -"
Else
for each IPConfig in IPConfigSet
BailObtenu = IPConfig.DHCPLeaseObtained
BailExpirant = IPConfig.DHCPLeaseExpires
'---- Convertion des date et heure d'obtention et d'expiration des baux DHCP en un format lisible par l'utilisateur. ----
BailObtenu = mid(BailObtenu, 7, 2) & "/" & mid(BailObtenu, 5, 2) & "/" & mid(BailObtenu, 1, 4) & " - " & mid(BailObtenu, 9, 2)& ":" & mid(BailObtenu, 11, 2)& ":" & mid(BailObtenu, 13, 2)
BailExpirant = mid(BailExpirant, 7, 2) & "/" & mid(BailExpirant, 5, 2) & "/" & mid(BailExpirant, 1, 4) & " - " & mid(BailExpirant, 9, 2)& ":" & mid(BailExpirant, 11, 2)& ":" & mid(BailExpirant, 13, 2)
MsgBox " Configuration réseau de l'ordinateur " & ComputerName & vbcrlf & vbcrlf & _
"Nom Machine " & vbtab & " : " & IPConfig.DNSHostName & vbcrlf & _
"Carte active" & vbtab & " : " & IPConfig.Description & vbcrlf & _
"Adresse MAC " & vbtab & " : " & IPConfig.MACAddress & vbcrlf & _
"DHCP Activé" & vbtab & " : " & IPConfig.DHCPEnabled & vbcrlf & _
"Adresse IP " & vbtab & " : " & IPConfig.IPAddress(0) & vbcrlf & _
"Masque " & vbtab & vbtab & " : " & IPConfig.IPSubnet(0) & vbcrlf & _
"Passerelle " & vbtab & " : " & IPConfig.DefaultIPGateway(0) & vbcrlf & _
"Serveur DHCP " & vbtab & " : " & IPConfig.DHCPServer & vbcrlf & vbcrlf & _
"Serveur DNS " & vbtab & " : " & IPConfig.DNSServerSearchOrder(0) & vbcrlf & _
" " & vbtab & vbtab & " : " & IPConfig.DNSServerSearchOrder(1) & vbcrlf & _
"Serveur WINS " & vbtab & " : " & IPConfig.WINSPrimaryServer(0) & vbcrlf & _
" " & vbtab & vbtab & " : " & IPConfig.WINSSecondaryServer(0) & vbcrlf & vbcrlf & _
" Bail obtenu " & vbtab & " : " & BailObtenu & vbcrlf & _
" Bail expirant " & vbtab & " : " & BailExpirant _
,VbInformation,"Configuration réseau de l'ordinateur "
Next
End If
End Sub

Monitoring service with WMI

I would like to monitor some services with WMI:
1. Test if the service is running
2. If not running restart it
3. If I can't restart it send an email
Could someone help me deal with this please?
strComputer = "."
srv= " WSearch, wuauserv "
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Service where Name='srv'")
For Each objService in colItems
Wscript.Echo "Service Name: " & objService.Name & VBNewLine _
& "State: " & objService.State
if objService.State="Stopped" then
objService.StartService()
Wscript.Echo "Started service "
else
REM here the code for email if the service didn't start
end if
Next

Getting & Managing/updating local built-in accounts for some Windows domain & standalone servers (vbscript)

**
Hi!
I was wondering if someone tried something similar, I have some code merged with a lot of glue...but as i'm a newby in vbs I can be sure that most of it is wrong. Basically I wanted to save a lot time during built-in admin accounts review /update with an automatic vbscript for this task.
I have like 6 account names and each one with an specific passwords.
I'm not 100% sure of which local account name is being used into each server but that might be something that i will need to verify manually or try to see if I can use another file where this script will read the possible accounts names and passwords and use some kind of brute force
Here is what I have :**
**update 8-29-12 a (deleted)
**update 8-29-12 b "THIS ONE IS WORKING..but i need to test & use cpau for NDC's"
Option Explicit
Dim strExcelPath, objExcel, objSheet, intRow, strUserDN, strPassword, comp
Dim objUser
' Spreadsheet file.
strExcelPath = "c:\List.xls"
' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Excel application not found."
Wscript.Quit
End If
On Error GoTo 0
' Open spreadsheet.
On Error Resume Next
objExcel.Workbooks.Open strExcelPath
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Spreadsheet cannot be opened: " & strExcelPath
Wscript.Quit
End If
On Error GoTo 0
' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
intRow = 2
Do While objSheet.Cells(intRow, 1).Value <> ""
comp = objSheet.Cells(intRow, 1).Value
strUserDN = objSheet.Cells(intRow, 2).Value
strPassword = objSheet.Cells(intRow, 3).Value
On Error Resume Next
Set objuser = GetObject ("WinNT://" & comp & "/" & strUserDN & ",user")
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Data NOT found: "
Else
objUser.SetPassword strPassword
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Password NOT set for: " & strUserDN
Else
End If
End If
intRow = intRow + 1
Loop
' Close the workbook.
objExcel.ActiveWorkbook.Close
' Quit Excel.
objExcel.Application.Quit
Wscript.Echo "Done"
My option "B" could be start over using something like this:
#echo off
for /F "delims=" %%i in (servers.txt) do (
psexec \%%i NET USER > %%i.txt
)
**
There might hundreds of ways to solve this and the my idea it's avoid having someone manually modifying the admin passwords for the servers listed and not listed in the AD after one month.
Any help would be appreciated.
Regards
This code will list the local users for one computer, specified by the variable server, and then print the user id of each user.
server = "YourServerName"
Set oComputer = GetObject("WinNT://" & server & "")
oComputer.Filter = Array("User")
For Each oUser in oComputer
WScript.Echo oUser.Name
Next
'Objective: check multiple servers for admin accounts status and report to html file
Set iFSO = CreateObject("Scripting.FilesyStemObject")
Set oFSO = CreateObject("Scripting.FilesyStemObject")
InputFile = WScript.Arguments.Named("servers")
if len(InputFile) < 1 then
wscript.echo "Error: Servers Parameter not found" & vbCrLf
show_usage
wscript.quit
end if
Outputfile= InputFile & "_guest_admins_" + cstr(Month(now()))+"_"+cstr(day(now()))+".htm"
if not ofso.FileExists(inputfile) then
wscript.echo "Error: Server list file not Found."
wscript.quit
end if
Set ofile = ofso.createTextFile(OutputFile, True)
Set ifile = iFSO.OpenTextFile(inputfile)
ofile.writeline "<html>" & html_head & "<body>"
ofile.writeline "<table border=1 cellpadding=1 cellspacing=0>"
ofile.writeline o
ofile.writeline "<tr><td>Hostname</td><td>User</td><td>Disabled</td><td>Locked</td><td>Expiration Date</td><td>Flags</td><td>BuiltIn</td></tr>"
Do until ifile.AtEndOfLine
Computer = ifile.ReadLine
if ping(Computer) then
Builtin = ""
if Check_WMI(Computer) then
Builtin = GetBuiltInAccount(Computer)
else
Builtin = "WMI Fail"
end if
strt = now
wscript.echo "Checking Users for server: " & Computer
on error resume next
Set objGroup = GetObject("WinNT://" & Computer & "/Administrators,group")
if err.number <> 0 then
wscript.echo "GetObject WinNT Failed"
ofile.writeline "<tr><td>" & computer & "</td><td colspan=6 align=center>GetObject WinNT Fail: "& err.number &"</td></tr>"
else
on error goto 0
For Each objUser in objGroup.Members
ofile.writeline GetUserNT(computer, objUser.Name, Builtin)
Next
wscript.echo "Elapsed Time: " & datediff("s", strt, now) & " seconds"
end if
else
wscript.echo computer & " does not reply ping"
ofile.writeline "<tr><td>" & computer & "</td><td colspan=6 align=center>No Ping Reply</td></tr>"
end if
Loop
ofile.writeline "</table>"
ofile.writeline "</body></html>"
function ping(target)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPingedComputers = objWMIService.ExecQuery("Select * from Win32_PingStatus Where Address = '"& target & "'")
For each objComputer in colPingedComputers
' If the status code is Null or Not 0 then the ping failed
If IsNull( objComputer.StatusCode ) Or objComputer.StatusCode <> 0 Then
' Set the function to return Boolean FALSE
Ping = False
Else
' Set the function to return Boolean TRUE
Ping = True
End If
Next
end function
sub show_usage
wscript.echo "Usage: cscript chkusers /servers:list.txt" & vbcrlf
wscript.echo vbtab & "/servers Parameter is a Text File one Servername per Line" & vbcrlf
wscript.echo "Notes: This script generates an html report of server admin accounts."
wscript.echo " Results are saved in a file named + date + htm extension."
wscript.echo " Output example filename: list_guest_admins_" + cstr(Month(now()))+"_"+cstr(day(now()))+".htm"
end sub
Function Check_WMI(strServer)
On Error Resume Next ' error handling off
' create object reference, connect to namespace root\default
Set oCimOmId = GetObject("winmgmts:"& strServer & "\root\default:__cimomidentification=#")
' Test whether WMI is present or not.
If Err <> 0 then
Check_WMI= true
else
Check_WMI= false
end if
on error goto 0
end function
Sub EnumNameSpaces(strNameSpace)
'call enumnamespaces("root")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\" & strNameSpace)
Set colNameSpaces = objWMIService.InstancesOf("__NAMESPACE")
For Each objNameSpace In colNameSpaces
Call EnumNameSpaces(strNameSpace & "\" & objNameSpace.Name)
Next
End Sub
function CSS
tt = "<style type=""text/css"">" & vbcrlf
tt=tt & " body {font-family:Verdana;font-size: 10px;color: #49403B;background: #EFEFEF;}" & vbcrlf
tt=tt & "table {font-family:Verdana;font-size: 12px; empty-cells:show; }" & vbcrlf
tt=tt & "</style>" & vbcrlf
CSS = tt
end function
function html_head
tt="<head>" & vbcrlf
tt=tt & CSS
html_head = tt & "</head>" & vbcrlf
end function
function GetUserNT(strComputer, usr, bltin)
Const ADS_UF_DONT_EXPIRE_PASSWD = &H10000
o=""
On Error Resume Next
Set objUser = GetObject("WinNT:// " & strComputer & "/" & usr & " ")
o=o& "<tr><td>"& strcomputer &"</td><td>"& usr &"</td>"
if len(objUser.AccountDisabled) = 0 then exit function
o=o& "<td> "& StrDisabled(objUser.AccountDisabled) &"</td>"
o=o& "<td> "& StrLocked(objUser.IsAccountLocked) &"</td>"
o=o& "<td> "
o=o& objUser.Get("UserFlags") AND ADS_UF_DONT_EXPIRE_PASSWD
o=o& "</td>"
o=o& "<td> "
o=o& objUser.AccountExpirationDate
o=o& "</td>"
if lcase(bltin) = lcase(usr) then
o=o & "<td> Built-In</td>"
elseif instr(bltin, "[[Fail]]") > 0 then
o=o & "<td> "& bltin &"</td>"
else
o=o & "<td> </td>"
end if
o = o & "</tr>"
GetUserNT = o
end function
function StrLocked(str)
if str = "True" then
StrLocked = "Locked"
else
StrLocked = "Unlocked"
end if
end function
function StrDisabled(str)
if str = "True" then
StrDisabled = "Disabled"
else
StrDisabled = "Enabled"
end if
end function
function GetUsers(strComputer,grp,usr)
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount Where name = '"& usr &"'")
o=""
For Each objItem in colItems
o=o& "<tr>"
o=o& "<td>" & strComputer & "</td>"
o=o& "<td>" & grp & "</td>"
o=o& "<td>" & objItem.AccountType & "</td>"
o=o& "<td>" & objItem.Caption & "</td>"
o=o& "<td>" & objItem.Description & "</td>"
o=o& "<td>" & objItem.Disabled & "</td>"
o=o& "<td>" & objItem.Domain & "</td>"
o=o& "<td>" & objItem.FullName & "</td>"
o=o& "<td>" & objItem.LocalAccount & "</td>"
o=o& "<td>" & objItem.Lockout & "</td>"
o=o& "<td>" & objItem.Name & "</td>"
o=o& "<td>" & objItem.PasswordChangeable & "</td>"
o=o& "<td>" & objItem.PasswordExpires & "</td>"
o=o& "<td>" & objItem.PasswordRequired & "</td>"
o=o& "<td>" & objItem.SID & "</td>"
o=o& "<td>" & objItem.SIDType & "</td>"
o=o& "<td>" & objItem.Status & "</td>"
o=o& "</tr>"
Next
on error goto 0
GetUsers = o
end function
Function getlcl(srvname)
Set objComputer = GetObject("WinNT://" & srvname & "/Administrators,group")
wscript.echo "Local Accounts on " & srvname
wscript.echo "-------------------------------------------------"
For Each objUser in objComputer.Members
Wscript.Echo vbTab & objUser.Name
Next
wscript.echo "-------------------------------------------------"
end Function
function GetBuiltInAccount(strComputer)
on error resume next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
if err.number <> 0 then
GetBuiltInAccout = "Get Object WMI [[Fail]]: " & err.number & ":: " & err.description
err.clear
exit function
end if
on error goto 0
on error resume next
Set colAccounts = objWMIService.ExecQuery("Select * From Win32_UserAccount Where Domain = '" & strComputer & "'")
if err.number <> 0 then
GetBuiltInAccout = "WMI_ExecQuery [[Fail]]: " & err.number & ":: " & err.description
err.clear
exit function
end if
on error goto 0
on error resume next
For Each objAccount in colAccounts
if err.number <> 0 then
GetBuiltInAccout = "WMI_ExecQuery_ForEachAccount [[Fail]]: " & err.number & ":: " & err.description
wscript.echo "WMI_ExecQuery_ForEachAccount [[Fail]]: " & err.number & ":: " & err.description
err.clear
exit function
end if
on error goto 0
If Left (objAccount.SID, 6) = "S-1-5-" and Right(objAccount.SID, 4) = "-500" Then
GetBuiltInAccount = objAccount.Name
exit function
End If
Next
end function
Special instructions
For Domain servers:
Create a batch/cmd file (something like admins.cmd) containing the line below:
cscript admin.vbs /servers:list.txt
For Standalone server:
-Download CPAU.exe
Create a batch/cmd file (something like adminsNDC.cmd) conteninig the line below:
CPAU -u %COMPUTERNAME%\administrator -p mypassword -ex "cscript.exe Admins.vbs /servers:list.txt" -nowarn
-Local Admin report v1-
Instructions
For Domain servers:
1-Add the servers to be scaned into: "list.txt"
2-Run/create a scheduled task for "one" of the following files :
admins.cmd <-------> (you will get: |Hostname|User| Disabled |Locked |Expiration Date| Flags |BuiltIn |)
3-Check the html report(you can sent it to excel through IE)
For a Standalone server:
1-Add the server to be scaned into: "list.txt"
2-Edit the account name and password info into adminsNDC.cmd
3-Run adminsNDC.cmd <-------> (you will get: |Hostname|User| Disabled |Locked |Expiration Date| Flags |BuiltIn |)
3-Check the html report
After getting a report of the servers and accounts, this script could be used to perform password's update.
Option Explicit
Dim strExcelPath, objExcel, objSheet, intRow, strUserDN, strPassword, comp
Dim objUser
' Spreadsheet file.
strExcelPath = "c:\List.xls"
' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Excel application not found."
Wscript.Quit
End If
On Error GoTo 0
' Open spreadsheet.
On Error Resume Next
objExcel.Workbooks.Open strExcelPath
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Spreadsheet cannot be opened: " & strExcelPath
Wscript.Quit
End If
On Error GoTo 0
' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
intRow = 2
Do While objSheet.Cells(intRow, 1).Value <> ""
comp = objSheet.Cells(intRow, 1).Value
strUserDN = objSheet.Cells(intRow, 2).Value
strPassword = objSheet.Cells(intRow, 3).Value
On Error Resume Next
Set objuser = GetObject ("WinNT://" & comp & "/" & strUserDN & ",user")
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Data NOT found: "
Else
objUser.SetPassword strPassword
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Password NOT set for: " & strUserDN
Else
End If
End If
intRow = intRow + 1
Loop
' Close the workbook.
objExcel.ActiveWorkbook.Close
' Quit Excel.
objExcel.Application.Quit
Wscript.Echo "Done"

Resources