locked
parsing logon scripts RRS feed

  • Question

  • I have multiple logon scripts that I want to turn into Group Policy drive mappings.

    The batch files (logon1.bat, logon2.bat, etc) are all in this format. There are 423 batch files

    :R Drive
    Ifmember "domain\RUsers"
    Net use R: /d
    Net use R: \\fileserver1\sharename
    :End R Drive

    :S drive
    Ifmember "domain\Susers"
    Net Use S: /d
    Net use S: \\fileserver2\sharename
    End S Drive

    :T Drive
    ifmember "domain\Tusers"
    Net use T: /d
    Net use T: \\fileserver2\TShare
    :End T Drive

    Id like to run a powershell script and end up with a single csv file in this format

    DriveLetter   Path                                  Group
    R                  \\fileserver1\sharename    domain\RUsers
    S                  \\fileserver2\sharename    domain\Susers
    T                   \\fileserver2\TShare          domain\Tusers

    I havent used PS to parse files like this before.

    Any help appreciated.

    Thanks,

    Steve

    Tuesday, April 16, 2019 8:51 PM

Answers

  • You can try this.

    $h = @{}
    $a = @()
    Get-ChildItem c:\temp\logon*.bat | foreach {
        Get-Content $_ | foreach {
            if ($_ -match 'Ifmember\s+"(..+?)"') {
                $h.Group = $matches[1]
            }
            if ($_ -match '^Net\s+Use\s+([A-Za-z]):\s+(\\\\..*?\\..*)') {
                $h.DriveLetter = $matches[1].ToUpper()
                $h.Path = $matches[2]
            }
        }
        $a += New-Object PSCustomObject -Property $h
    }
    $a | Export-Csv c:\temp\Whatever.csv -Notypeinfo


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by speterson400 Wednesday, April 17, 2019 12:14 PM
    Wednesday, April 17, 2019 1:36 AM

All replies

  • Watch this:

    Sophisitcated Techniques of Plain Text Parsing


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by jrv Tuesday, April 16, 2019 9:30 PM
    Tuesday, April 16, 2019 9:25 PM
  • You can try this.

    $h = @{}
    $a = @()
    Get-ChildItem c:\temp\logon*.bat | foreach {
        Get-Content $_ | foreach {
            if ($_ -match 'Ifmember\s+"(..+?)"') {
                $h.Group = $matches[1]
            }
            if ($_ -match '^Net\s+Use\s+([A-Za-z]):\s+(\\\\..*?\\..*)') {
                $h.DriveLetter = $matches[1].ToUpper()
                $h.Path = $matches[2]
            }
        }
        $a += New-Object PSCustomObject -Property $h
    }
    $a | Export-Csv c:\temp\Whatever.csv -Notypeinfo


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by speterson400 Wednesday, April 17, 2019 12:14 PM
    Wednesday, April 17, 2019 1:36 AM
  • Wow ... and I'm used to tell people "We do NOT have a < Make a wish > forum here". ;-)

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Wednesday, April 17, 2019 5:48 AM
    Wednesday, April 17, 2019 5:47 AM
  • Thanks for the link. I had actually started watching that prior to posting but saw he started with a tab deliminated file. After your post, I watched most of the video. The end of the video using the  template file is very intriguing but, I couldn't get it to work. I kept getting an error on the {name\ *: } syntax. It didnt seem to like the *. Im going to keep trying with the template file and see if I can figure it out.

    I appreciate your reply and learned a lot from re-watching the video.

    Steve


    --------- SteveP

    Wednesday, April 17, 2019 12:14 PM
  • Rich,

    Wow! This works great. Thank you so much! 

    Ive been working on my RegEx skills but, learned a lot from your script. This will help me a lot for future projects

    Thank you! Thank you!

    sincerely,

    Steve


    --------- SteveP

    Wednesday, April 17, 2019 12:17 PM