Link to home
Create AccountLog in
Avatar of Zorge
Zorge

asked on

URGENT! Need a Filesystem VBScript

Hello everyone,

I have quite an urgent issue.
I have a VB Script which contacts DFS and lists the Folders and targets.

So that scripts parses DFS and give out something like this:


\\RUM\BS                       '(1)
\\DFS-1\PUBS                '(2)
\\RUM\PUBS
\\DFS-2\PUBS
\\RUM\BS
\\DFS-4\PUBS
\\RUM\BS\UK
\\dfs-3\london
\\RUM\BS\IRELAND
\\Dfs-2\PUBS
\\RUM\DATA
\\DFS-1\DATA
\\RUM\DATA\AAA\DEPT\Finance\Holland
\\DFS-1\TELEKOM
\\RUM\DATA\AAA\DEPT\Finance\Holland
\\DFS-3\TELEKOM

This is a simple DFS structure. First line is DFS Namespace Server (1), second (2) is the share responsible

I have now the following task.

1) I need to extract from that that script output only the lines starting with "\\RUM\DATA\AAA\DEPT\Finance" and their apropriate shares. So in my case the out put would be something like:

\\RUM\DATA\AAA\DEPT\Finance\Holland
\\DFS-1\TELEKOM
\\RUM\DATA\AAA\DEPT\Finance\Holland
\\DFS-3\TELEKOM

2) Then I need to find out the physical location of the share for, in this case,

\\DFS-1\TELEKOM
\\DFS-3\TELEKOM

*hint one can find out the path for a share by using for example, but I don't get how to find it out in VBScript

C:\install>rmtshare \\dfs-3\telekom
Share name        \\dfs-3\telekom
Path              C:\Zimbabwe\TELEKOM
Remark
Maximum users     No limit
Users             0
Permissions:
        \Everyone  :  READ

The command completed successfully.


So that at the end I have something like results like (which are just pathe's of the two shares mentioned above":

DFS-1 C:\Zimbabwe\TELEKOM
DFS-3 C:\Directory\TELEKOM

Remarks:
the output from the script can be much much longer. But the order is always the same.DFS Namespace Server (1), second (2) is the share responsible

Help greatly appreaciated! Thanks!
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDfsTargets = objWMIService.ExecQuery _
    ("Select * from Win32_DFSTarget")
For each objDfsTarget in colDfsTargets
    Wscript.Echo objDfsTarget.LinkName       
    Wscript.Echo chr(92)&chr(92)& objDfsTarget.ServerName & chr(92)& objDfsTarget.ShareName

Next

Open in new window

Avatar of Fry
Fry
Flag of Australia image

G'Day,

Hope I'm not too late. Here is a script which will do what I think you're after. You just need to specify your server which has the DFS store and which share you want to look for:

' ============================================         User Changable Items

  strComputer = "RUM"                                                                  ' String for computer with DFS Shares
  strSearch = "\\RUM\DATA\AAA\DEPT\Finance\Holland"              ' String for Share
 
Let me know if you need any more help with this.

Option Explicit
  On Error Resume Next


  Dim intCount, intLoop
  Dim strComputer, strShareName, strSearch
  Dim objWMIService, objDFSTarget, objShare
  Dim colDFSTargets, colDrives, colShares
  Dim aryShares()
  ReDim aryShares(1,0)  

  intCount = 0

' ============================================         User Changable Items

  strComputer = "RUM"	                                         ' String for computer with DFS Shares
  strSearch = "\\RUM\DATA\AAA\DEPT\Finance\Holland"              ' String for Share
  


' =============================================        Populate Array With Share Details

  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
  Set colDfsTargets = objWMIService.ExecQuery ("Select * from Win32_DFSTarget") 
  For each objDfsTarget in colDfsTargets
    ReDim Preserve aryShares(1, intCount)
    aryShares(0,intCount) = objDfsTarget.LinkName
    aryShares(1, intCount) = chr(92)&chr(92)& objDfsTarget.ServerName & chr(92)& objDfsTarget.ShareName
    intCount = intCount + 1  
  Next

  
' ============================================         Cycle Through Shares Array

  For intLoop = 0 to uBound(aryShares,2)


' ============================================         Matches Shares with Search String

    If uCase(aryShares(1,intLoop)) = uCase(strSearch) Then
      strComputer = mid(aryShares(1,intLoop),3,Int(InStr(3,aryShares(1,intLoop),"\",1)-3))
      strShareName = mid(aryShares(1,intLoop),len(strComputer) + 4,Int(InStr(3,aryShares(1,intLoop),"\",1)))


      wscript.echo "Server: " & strComputer


' ============================================         Gets local path for Share

      Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Name = '" & strShareName & "'")
      For each objShare in colShares  
        Wscript.Echo "Path: " & vbTab &  objShare.Path   
      Next

      wscript.echo vbcrlf
  
    End IF
  Next

Open in new window

You don't need the array if you want the script to be smaller. It started off on another path and didn't think about not using it until now.
Avatar of Zorge
Zorge

ASKER

Hi Enigma,

thank you for your quick reply.
On the script provided by you I get this as response:

***********************************************************************************
C:\install>cscript all_1.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Server: DFS-3
Path:   C:\Zimbabwe\TELEKOM
***********************************************************************************

when entering in user data

  strComputer = "DFS-1"                                            ' String for computer with DFS Shares
  strSearch = "\\DFS-3\Telekom"              ' String for Share

But the task I have is to have in my case to enter

  strSearch = "\\RUM\DATA\AAA\DEPT\Finance\Holland"              ' String for Share

and end up filtering from the outcome that would look something like (because the lines contain the search string):

\\RUM\DATA\AAA\DEPT\Finance\Holland
\\DFS-1\TELEKOM
\\RUM\DATA\AAA\DEPT\Finance\Holland
\\DFS-3\TELEKOM

with the ONE line below that search string. Which is then stripped to (has to be stripped since we are first looking for who is namespace server for the above mentioned folder)

\\DFS-1\TELEKOM
\\DFS-3\TELEKOM

then both lines are resolved to local pathes like:

DFS-1 C:\Zimbabwe\TELEKOM
DFS-3 C:\Directory\TELEKOM

which I will later use for operation like
(DFS-1 becomes VARIABLE1 ; C:\Zimbabwe\TELEKOM VARIABLE2, I will process it line after line untill I'm finished with all servers)

psexec VARIABLE1 icacls VARIABLE2 /restore c:\script\permissions.txt /c

Thanks a lot for your help! Much appreciated!
ASKER CERTIFIED SOLUTION
Avatar of Fry
Fry
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Zorge

ASKER

Perfect! And so quick!
Thanks Mate.

For those playing at home here is the script again but tweaked. Remember kids always program on a good nights sleep.


Option Explicit
  On Error Resume Next


  Dim strComputer, strSearch, strVariable1, strVariable2
  Dim intAryLoop
  Dim objWMIService, objDFSTarget, objShare
  Dim colDFSTargets, colDrives, colShares
  Dim aryMatches()

  ReDim Preserve aryMatches(1,0) 

' ============================================         User Changable Items

  strComputer = "RUM"                                            ' String for computer with DFS Shares
  strSearch = "\\RUM\DATA\AAA\DEPT\Finance\Holland"              ' String for Share


' ============================================         Get List Of DFS Shares On Target Computer
  
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
  Set colDfsTargets = objWMIService.ExecQuery ("Select * from Win32_DFSTarget")


 ' ============================================        Matches Shares with Search String

  For each objDfsTarget in colDfsTargets
    If uCase(objDfsTarget.LinkName) = uCase(strSearch) Then


' ============================================         Gets local path for Share

      Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & objDfsTarget.ServerName & "\root\cimv2")
      Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Name = '" & objDfsTarget.ShareName & "'")
      For each objShare in colShares
          strVariable2 = objShare.Path
      Next
      strVariable1 = objDfsTarget.ServerName
    End IF


' ============================================         Store Information In Match Array

    IF strVariable1 <> "" or strVariable2 <> "" THEN
      IF aryMatches(0,uBound(aryMatches,2)) = "" THEN
        aryMatches(0,0) = strVariable1
        aryMatches(1,0) = strVariable2
      Else
        ReDim Preserve aryMatches(1,uBound(AryMatches,2) + 1)
        aryMatches(0,uBound(aryMatches,2)) = strVariable1
        aryMatches(1,uBound(aryMatches,2)) = strVariable2
      End If
    End IF
  Next


' ============================================         Display Results In Match Array

  wscript.echo "Results:"
  For intAryLoop = 0 to uBound(aryMatches,2)
    wscript.echo aryMatches(0,intAryLoop) & " " & aryMatches(1, intAryLoop)
  Next

Open in new window