Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pink PCs, Execute VBScript on live hosts 1

Status
Not open for further replies.

bence8810

IS-IT--Management
Jul 20, 2005
241
AE
Hi,

I am trying to find out live shares on our PCs, and I am using a VBScript to accomplish this. There are two problems I am facing.

I would like to create a batch file that would execute this VBScript with a parameter that would be read from a text file.

This is how I run the script now:
Code:
cscript CheckShare-Local.vbs	/computer:COMPUTERNAME

And I would like a text file with all the COMPUTERNAMEs and the batch would execute the VBScript on those Computers.

Secondly, this VBScript runs rather slow, as some hosts are not live currently. Can I do something to first ping the hosts and only if they respond run the rest of the script?

Thanks, any help, idea would be appreciated.

Ben

p.s.

This is the VBScript I am calling:

Code:
'*************************************************************
'        Script Witten by Larry Heintz               
'        March 2006 [URL unfurl="true"]www.windowsadminscripts.com[/URL]
' This script will reveal all user and hidden shares on a
' computer. It will show you the share name, share comment
' and share path. You will also have the option to run the 
' script against a AD Domain or Work Group name. The script
' also saves the shares information into a comma delimited
' file.
'
' Script Usages:
' List Shares on Single PC: cscript shares.vbs /computer:[computername]
' List Shares on AD Domain/Work Group: cscript shares.vbs /adwg:[ad domain or workgroup name]
'*************************************************************
Dim objStdOut,args
Dim computername,adwg
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
adwg = args.Item("adwg")
logpath = getLogPath()

if wscript.arguments.count = 0 then
    wscript.echo "Script Usages:"
    wscript.echo "List Shares on Single PC: cscript shares.vbs /computer:[computername]"
    wscript.echo "List Shares on AD Domain/Work Group: cscript shares.vbs /adwg:[ad domain or workgroup name]"
elseif args.exists("computer") then
    Call listShares(computername)
elseif args.exists("adwg") then
    Call listSharesADWG(adwg)
end if

Function listShares(computername)
On Error Resume Next
Dim objshares,share
set objshares = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery("SELECT * FROM Win32_Share")
wscript.echo "Shares for " & ucase(computername) & vbcrlf
wscript.echo "Name" & space(11) & "Comment" & space(13) & "Path"
wscript.echo "=====" & space(10) & "========" & space(12) & "====="
for each share in objshares
    Call writeLog(computername,share.name,share.caption,share.path)
    wscript.echo share.name & space(15-(len(share.name))) & share.caption & space(20-len(share.caption)) & share.path
next
set objshares = nothing
wscript.echo ""
End Function

Function listSharesADWG(adwg)
set container = getobject("WinNT://" & adwg)
container.filter = array("computer")
for each computer in container
    listShares(computer.name)
next
set container = nothing
End Function

Function getLogPath()
Dim temp,temp2
temp = split(wscript.scriptfullname,"\")
for i = 0 to ubound(temp) - 1
    temp2 = temp2 & temp(i) & "\"
next
getLogPath = temp2
End Function

Function writeLog(computername,sharename,sharecomment,sharepath)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFSOwriteline = FSO.OpenTextFile(getLogPath() & "\shares.csv", 8,True)
    objFSOwriteline.WriteLine(computername & "," & sharename & "," & sharecomment & "," & sharepath)
    objFSOwriteline.close
Set objFSOwriteline = nothing
Set FSO = nothing
End Function
 
Hi
Thanks for the reply. Unfortunately I am not skilled enough to do that in VB. I know little about scripting, I just thought with a batch file it wouldnt be too difficult to call a name and call the VB script.

I can do it if I create a line for each PC, and then change the computername. If I execute the script it runs fine.

It would be nicer though to just have one batch file, one VB script, and a text file with all the PC names.

And the other thing would be equally important, to ping the host before doing the actual check, as it takes a lot of time when the script tries to inventory a host that is not live.

Thanks for any help,

Ben
 
Try this, Ben
Code:
if wscript.arguments.count = 0 then
    wscript.echo "Script Usages:"
    wscript.echo "List Shares on Single PC: cscript shares.vbs /computer:[computername]"
    wscript.echo "List Shares on AD Domain/Work Group: cscript shares.vbs /adwg:[ad domain or workgroup name]"
elseif args.exists("computer") then
    if Ping(computer) = False then
        wscript.echo "PC " & computer & " is not online. Can't check it." & vbCrLf &_
                     "Skipping..."
    Else
        Call listShares(computername)
    End If
elseif args.exists("adwg") then
    Call listSharesADWG(adwg)
end if

'-------------------------------------------------------------------------------
Function Ping(strHost)
    dim objPing, objRetStatus

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
            Ping = False
        else
            Ping = True
        end if
    next
End Function

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Hi,

Thanks JJ, I tried your script. Although it works, I think its not pinging, as it still take a load of time when a host is down.

My feeling is that I applied your changes wrong. Could you look at my script?

Thanks

Ben

Code:
'*************************************************************
'        Script Witten by Larry Heintz               
'        March 2006 [URL unfurl="true"]www.windowsadminscripts.com[/URL]
' This script will reveal all user and hidden shares on a
' computer. It will show you the share name, share comment
' and share path. You will also have the option to run the 
' script against a AD Domain or Work Group name. The script
' also saves the shares information into a comma delimited
' file.
'
' Script Usages:
' List Shares on Single PC: cscript shares.vbs /computer:[computername]
' List Shares on AD Domain/Work Group: cscript shares.vbs /adwg:[ad domain or workgroup name]
'*************************************************************
Dim objStdOut,args
Dim computername,adwg
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
adwg = args.Item("adwg")
logpath = getLogPath()

if wscript.arguments.count = 0 then
    wscript.echo "Script Usages:"
    wscript.echo "List Shares on Single PC: cscript shares.vbs /computer:[computername]"
    wscript.echo "List Shares on AD Domain/Work Group: cscript shares.vbs /adwg:[ad domain or workgroup name]"
elseif args.exists("computer") then
    if Ping(computer) = False then
        wscript.echo "PC " & computer & " is not online. Can't check it." & vbCrLf &_
                     "Skipping..."
    Else
        Call listShares(computername)
    End If
elseif args.exists("adwg") then
    Call listSharesADWG(adwg)
end if

Function Ping(strHost)
    dim objPing, objRetStatus

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
            Ping = False
        else
            Ping = True
        end if
    next
End Function

Function listShares(computername)
On Error Resume Next
Dim objshares,share
set objshares = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery("SELECT * FROM Win32_Share")
wscript.echo "Shares for " & ucase(computername) & vbcrlf
wscript.echo "Name" & space(11) & "Comment" & space(13) & "Path"
wscript.echo "=====" & space(10) & "========" & space(12) & "====="
for each share in objshares
    Call writeLog(computername,share.name,share.caption,share.path)
    wscript.echo share.name & space(15-(len(share.name))) & share.caption & space(20-len(share.caption)) & share.path
next
set objshares = nothing
wscript.echo ""
End Function

Function listSharesADWG(adwg)
set container = getobject("WinNT://" & adwg)
container.filter = array("computer")
for each computer in container
    listShares(computer.name)
next
set container = nothing
End Function

Function getLogPath()
Dim temp,temp2
temp = split(wscript.scriptfullname,"\")
for i = 0 to ubound(temp) - 1
    temp2 = temp2 & temp(i) & "\"
next
getLogPath = temp2
End Function

Function writeLog(computername,sharename,sharecomment,sharepath)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFSOwriteline = FSO.OpenTextFile(getLogPath() & "\shares.csv", 8,True)
    objFSOwriteline.WriteLine(computername & "," & sharename & "," & sharecomment & "," & sharepath)
    objFSOwriteline.close
Set objFSOwriteline = nothing
Set FSO = nothing
End Function
 
Well, my guess is the ListSharesADWG Function is where the apparent delay is.

So, to test this, I've added in some wscript.echo lines to identify where the delays occurs.
Code:
'*************************************************************
'        Script Witten by Larry Heintz
'        March 2006 [URL unfurl="true"]www.windowsadminscripts.com[/URL]
' This script will reveal all user and hidden shares on a
' computer. It will show you the share name, share comment
' and share path. You will also have the option to run the
' script against a AD Domain or Work Group name. The script
' also saves the shares information into a comma delimited
' file.
'
' Script Usages:
' List Shares on Single PC: cscript shares.vbs /computer:[computername]
' List Shares on AD Domain/Work Group: cscript shares.vbs /adwg:[ad domain or workgroup name]
'*************************************************************
Dim objStdOut,args
Dim computername,adwg
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
adwg = args.Item("adwg")
logpath = getLogPath()

if wscript.arguments.count = 0 then
    wscript.echo "Script Usages:"
    wscript.echo "List Shares on Single PC: cscript shares.vbs /computer:[computername]"
    wscript.echo "List Shares on AD Domain/Work Group: cscript shares.vbs /adwg:[ad domain or workgroup name]"
elseif args.exists("computer") then
    if Ping(computer) = False then
        wscript.echo "PC " & computer & " is not online. Can't check it." & vbCrLf &_
                     "Skipping..."
    Else
        wscript.echo computer & " is online." & vbCrLf &_
                     "Analysing the shares..."
        Call listShares(computername)
    End If
elseif args.exists("adwg") then
    wscript.echo "Listing shares on AD/Workgroup"
    Call listSharesADWG(adwg)
end if

Function Ping(strHost)
    dim objPing, objRetStatus

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
            Ping = False
        else
            Ping = True
        end if
    next
End Function

Function listShares(computername)
    On Error Resume Next
    Dim objshares,share
    set objshares = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery("SELECT * FROM Win32_Share")
    wscript.echo "Shares for " & ucase(computername) & vbcrlf
    wscript.echo "Name" & space(11) & "Comment" & space(13) & "Path"
    wscript.echo "=====" & space(10) & "========" & space(12) & "====="
    for each share in objshares
        Call writeLog(computername,share.name,share.caption,share.path)
        wscript.echo share.name & space(15-(len(share.name))) & share.caption & space(20-len(share.caption)) & share.path
    next
    set objshares = nothing
    wscript.echo ""
End Function

Function listSharesADWG(adwg)
    set container = getobject("WinNT://" & adwg)
    container.filter = array("computer")
    for each computer in container
        wscript.echo "Listing shares on " & computer
        listShares(computer.name)
    next
    set container = nothing
End Function

Function getLogPath()
    Dim temp,temp2
    temp = split(wscript.scriptfullname,"\")
    for i = 0 to ubound(temp) - 1
        temp2 = temp2 & temp(i) & "\"
    next
    getLogPath = temp2
End Function

Function writeLog(computername,sharename,sharecomment,sharepath)
    Dim FSO,objFSOwriteline
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set objFSOwriteline = FSO.OpenTextFile(getLogPath() & "\shares.csv", 8,True)
        objFSOwriteline.WriteLine(computername & "," & sharename & "," & sharecomment & "," & sharepath)
        objFSOwriteline.close
    Set objFSOwriteline = nothing
    Set FSO = nothing
End Function
As that function checks computers you should Ping each one first as well:
Code:
Function listSharesADWG(adwg)
    set container = getobject("WinNT://" & adwg)
    container.filter = array("computer")
    for each computer in container
        if ping(computer) = true then
            wscript.echo "Listing shares on " & computer
            listShares(computer.name)
        else
            wscript.echo "No reply from " & computer & "!" & vbCrLf & "Skipping..."
        end if
    next
    set container = nothing
End Function

Let us know how you get on.

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Hi

Good call on the Echo lines.

It seems even if I try it on a non existing name, it thinks its online.

Code:
cscript CheckShare-Local-Ping.vbs /computer:675675ytyfrhgf

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

 is online.
Analysing the shares...
Shares for 675675YTYFRHGF

Name           Comment             Path
=====          ========            =====

As for your second [ code ] entry, I dont know what you mean, sorry. Where should I place that piece of code?

Thanks

Ben
 
Use the second code snippet as a replacement for the existing listSharesADWG(adwg) Function.

In the meantime, I'm having a look as to why it's incorrectly reporting each computer as being online...

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Ah, it's a classic typo. There were places in the script where we had 'computer' instead of 'computername'.

e.g.
Code:
if Ping(computer) = False then
should be
Code:
if Ping(computername) = False then

Anyway, this version now seems to work:
Code:
'*************************************************************
'        Script Witten by Larry Heintz
'        March 2006 [URL unfurl="true"]www.windowsadminscripts.com[/URL]
' This script will reveal all user and hidden shares on a
' computer. It will show you the share name, share comment
' and share path. You will also have the option to run the
' script against a AD Domain or Work Group name. The script
' also saves the shares information into a comma delimited
' file.
'
' Script Usages:
' List Shares on Single PC: cscript shares.vbs /computer:[computername]
' List Shares on AD Domain/Work Group: cscript shares.vbs /adwg:[ad domain or workgroup name]
'*************************************************************
Dim objStdOut,args
Dim computername,adwg
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
adwg = args.Item("adwg")
logpath = getLogPath()

if wscript.arguments.count = 0 then
    wscript.echo "Script Usages:" & vbCrLf &_
                 "List Shares on Single PC: cscript shares.vbs /computer:[computername]" & vbCrLf &_
                 "List Shares on AD Domain/Work Group: cscript shares.vbs /adwg:[ad domain or workgroup name]"
elseif args.exists("computer") then
    wscript.echo "Pinging computer name '" & computername & "'"
    if Ping(computername) = False then
        wscript.echo "PC " & computername & " is not online. Can't check it." & vbCrLf &_
                     "Skipping..."
    Else
        wscript.echo computername & " is online." & vbCrLf &_
                     "Analysing the shares..."
        Call listShares(computername)
    End If
elseif args.exists("adwg") then
    wscript.echo "Listing shares on AD/Workgroup"
    Call listSharesADWG(adwg)
end if

Function Ping(strHost)
    dim objPing, objRetStatus

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
            Ping = False
        else
            Ping = True
        end if
    next
End Function

Function listShares(computername)
    On Error Resume Next
    Dim objshares,share
    set objshares = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery("SELECT * FROM Win32_Share")
    wscript.echo "Shares for " & ucase(computername) & vbcrlf
    wscript.echo "Name" & space(11) & "Comment" & space(13) & "Path"
    wscript.echo "=====" & space(10) & "========" & space(12) & "====="
    for each share in objshares
        Call writeLog(computername,share.name,share.caption,share.path)
        wscript.echo share.name & space(15-(len(share.name))) & share.caption & space(20-len(share.caption)) & share.path
    next
    set objshares = nothing
    wscript.echo ""
End Function

Function listSharesADWG(adwg)
    set container = getobject("WinNT://" & adwg)
    container.filter = array("computer")
    for each computer in container
        wscript.echo "Listing shares on " & computer
        listShares(computer.name)
    next
    set container = nothing
End Function

Function getLogPath()
    Dim temp,temp2
    temp = split(wscript.scriptfullname,"\")
    for i = 0 to ubound(temp) - 1
        temp2 = temp2 & temp(i) & "\"
    next
    getLogPath = temp2
End Function

Function writeLog(computername,sharename,sharecomment,sharepath)
    Dim FSO,objFSOwriteline
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set objFSOwriteline = FSO.OpenTextFile(getLogPath() & "\shares.csv", 8,True)
        objFSOwriteline.WriteLine(computername & "," & sharename & "," & sharecomment & "," & sharepath)
        objFSOwriteline.close
    Set objFSOwriteline = nothing
    Set FSO = nothing
End Function

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
we had 'computer' instead of 'computername'
Tip: use the Option Explicit instruction.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Usually do and for the reasons you're tacitly suggesting, but even if I had both variables were in use correctly in different places but additionally incorrect in a couple of other places. So, Option Explicit wouldn't have thrown up a error.

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Hi

Thanks JJ, the script works like a charm, it does skip the computers that are offline.

The only thing missing now is to read the computernames from a text file. Is it possible somehow?

My workaround now is to have an excel sheet with 2 columns

Code:
 vbsfile /computer:computer1

And I just multiply the 2nd column with numbers increasing, as our names are in an ascending order with a 2 digi number at the end. Once I am done, I copy these cells to a TEXT file that I name .bat and execute.

If I could just have the VBS somehow read the computer names froma text file that would be luxurious, but even like this its outstanding

Thanks for all the help,

Ben
 
Well, to do this from the command prompt you could put the target computernames in a .txt file (which, for the sake of argument we'll call c:\targets.txt) like this:
Code:
pc01
pc02
pc03
fileserver01
fileserver02
then run the script like this from the command prompt:
Code:
for /f %a in (c:\targets.txt) do @cscript CheckShare-Local.vbs /computer:%a
Or, if you want to run the same command from a batch file, replace [tt]%a[/tt] with [tt]%%a[/tt] like this:
Code:
@echo off
color 9f
for /f %%a in (c:\targets.txt) do @cscript CheckShare-Local.vbs /computer:%%a
(the @echo off and color 9f lines are entirely optional).

Still, if you want to do it in the main .vbs script (and there's many good reasons why you should) then here's a code snippet that shows how to read a text file line by line. It'll work by itself, to show you how it works and prove it.
Code:
option explicit
Dim objFSO
Dim Msg, i
Dim strTargetsFile, objTargetsFile
Dim strComputername
Const ForReading = 1
Const conDefaultTargetsList = "c:\targets.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Msg = "Enter the path and name of the text file containing the list of computers to check:"
strTargetsFile = InputBox(msg,"Enter a list",conDefaultTargetsList)

Set objTargetsFile = objFSO.OpenTextFile(strTargetsFile, ForReading)

While not objTargetsFile.AtEndOfStream
    i = i + 1
    strComputername = objTargetsFile.ReadLine
    wscript.echo "Line number " & i & " is " & strComputername
Wend
On the grounds that writing VB scripts is fun, try adding this to your script so that it behaves how you want it to.

And might I recommend you download and install Syn Text Editor for all you scripting needs (no, I didn't write it, nor do I have shares in Syn as it's freeware) :)

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Dear JJ,

The Batch version works like a dream, thank you. I also tried the new VBS script you wrote, and it does produce a pop up with all the computernames in the script, so in theory it works. However, for my lack of scripting knowledge, I cannot implement it back to the original script, and I am more than happy with the batch version, although the VBS might be better when running as a logon script, if the Anti Virus is set agains batches.

Anyways, I am really glad, and satisfied with the current working one, so all I can say is Thanks a lot.

Best regards,

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top