Auditing File Shares on Clients
Here is a script I wrote that will list out all the file and printer shares on all the machines in your domain. The script uses the RootDSE object to locate the root of your domain, and then connects to the WMI service on all your machines remotely, so you'll need to be logged in as a Domain Administrator for this script to work properly.
Output: The script will output a set of comma-separated headers that have been statically entered (Computer Name, Share Name, Share Type), and then will echo out a line for each printer or file share it encounters that contains the previously mentioned variables. The script will also detect if any computers were not able to be contacted, or had errors when attempting a connection to the remote WMI service and will echo those out at the end. I encourage you to run the script and review the output. Then, modify the script as necessary to suit your needs! I would be welcome to any suggestions you have as to how to improve this to assist your network administration and monitoring!
set pcsDown = CreateObject("Scripting.Dictionary")
set pcsErr = CreateObject("Scripting.Dictionary")
main()
function main()
set rootdse = GetObject("LDAP://RootDSE")
domain = rootdse.Get("defaultNamingContext")
wscript.echo "Computer Name,Share Name,Share Type"
searchDir(domain)
printBadPCs()
end function
'***************************
'Declaration: function searchDir(ldappath)
'Purpose: Takes LDAP-formatted string as parameter. Performs an operation based on the directory object's class
'Return value: none
'***************************
function searchDir(ldappath)
set ou = GetObject("LDAP://" & ldappath)
for each dirObj in ou
select case dirObj.class
case "organizationalUnit"
searchDir(dirObj.distinguishedName)
case "container"
searchDir(dirObj.distinguishedName)
case "computer"
if isAlive(dirObj.cn) then
printShares(dirObj.cn)
else
pcsDown.Add dirObj.cn, ""
end if
end select
next
end function
'***************************
'Declaration: printShares(pcname)
'Purpose: Prints the name of any type 0 (disk) or type 1 (print queue) shares on the machine
'Return value: nothing
'***************************
function printShares(pcname)
on error resume next
numShares = 0
set shares = GetObject("winmgmts:\\" & pcname & "\root\cimv2:Win32_Share").Instances_
if Err.Number <> 0 then
pcsErr.Add pcname, ""
exit function
end if
for each share in shares
select case share.Type
case 0
wscript.echo pcname & "," & share.Name & ",disk"
numShares = numShares + 1
case 1
wscript.echo pcname & "," & share.Name & ",printer"
numShares = numShares + 1
end select
next
' if numShares <> 0 then wscript.echo "Total number of shares on " & pcname & ": " & numShares
end function
'***************************
'Declaration: function isAlive(pcname)
'Purpose: Sends ICMP packet to remote machine
'Return value: BOOL true if machine is alive, else BOOL false
'***************************
function isAlive(pcname)
isAlive = false
set ping = GetObject("winmgmts:").ExecQuery("select * from Win32_PingStatus where Address = '" & pcname & "'")
for each png in ping
if png.StatusCode = 0 then isAlive = true
next
end function
function printBadPCs()
wscript.echo vbcrlf & "<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>"
wscript.echo "The following computers could not be contacted"
wscript.echo "<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>"
for each pc in pcsDown
wscript.echo pc
next
wscript.echo vbcrlf & "<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>"
wscript.echo "The following computers generated an error msg"
wscript.echo "<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>"
for each pc in pcsDown
wscript.echo pc
next
end function