regex - Parse list of network shares with PowerShell -


i want parse "netuse" file , write network connections in single line/file padding "#" between them

like this:

r:\\server\share1#s:\\server\share2#y:\\server\share3\folder

my netuse file like:

new connections remembered.  status       local     remote                    network  ------------------------------------------------------------------------------- ok           r:        \\server\share1     microsoft windows network ok           s:        \\server\share2             microsoft windows network ok           y:        \\server\share3\folder  microsoft windows network  command completed successfully. 

my test code:

$netusefile= get-content "c:\temp\netuse.txt" $networsharestr = $netusefile | select-string -pattern "^.*\s+([a-za-z]):\s+(\\\\[a-za-z0-9_-]+\\.+)\s+microsoft windows network$"; 

trimstart ("ok") ?

i tried couple methods no luck. can helpme this?

edit:

@themadtechnician know.. need parse file... i've add 2 more fields

hostname:desktop1 username:user1 

i output:

user1,desktop1,r:\\server\share1#m:\\server\share2#x:\\server\share3\folder  $netusefile= get-content "c:\temp\netuse.txt"  $hostnamestr = $netusefile | select-string -pattern "hostname:" $usernamestr = $netusefile | select-string -pattern "username:" $test = new-object -typename psobject $test | add-member –membertype noteproperty –name 'hostname' –value         $hostnamestr.tostring().split(":")[1] $test | add-member –membertype noteproperty –name 'username' –value           $usernamestr.tostring().split(":")[1] 

how add members in object? (too inline me :)

gc c:\temp\meilehj.txt |?{$_ -match "^.*\s+([a-za-z]:)\s+(.+?)\s"}  | foreach {  add-member -inputobject $test –membertype noteproperty -name 'share' -value    $matches[1] add-member -inputobject $test –membertype noteproperty -name 'path' -value    $matches[2] ?? } 

and how join share , path '#'?

edit:

@themadtechnician lot!!

you're capturing more want. try this:

$netusefile= get-content "c:\temp\netuse.txt" $hostnamestr = ($netusefile | select-string -pattern "hostname:")|%{$_.tostring().substring(9,$_.tostring().length-9)} $usernamestr = ($netusefile | select-string -pattern "username:")|%{$_.tostring().substring(9,$_.tostring().length-9)} $networksharestr = (gc c:\temp\meilehj.txt|?{$_ -match "ok\s+([a-za-z]:)\s+(.+?)\s+?microsoft windows network.*$"}|%{     $matches[1].tostring()+$matches[2].tostring()     }) -join("#") write-output "$usernamestr,$hostnamestr,$networksharestr"|out-file c:\temp\output.txt 

edit: alroc's answer better route go, mine helps regex asked originally. edit2: updated script better answer updated question. functions thus: read input file variable.
find line hostname: string in it, select substring excludes first 9 characters ("hostname:), assign variable
find line username: string in it, select substring excludes first 9 characters ("username:), assign variable
find lines matching pattern network share, select drive letter , path, , join results hashtag separating them in single string. assign variable.
output variables, comma separated, single line in file c:\temp\output.txt
edit3: updated regex accommodates shares spaces in names. regex explained:

ok\s+([a-za-z]:)\s+(.+?)\s+?microsoft windows network.*$

ok matches characters ok literally (case sensitive)

\s+ match white space character [\r\n\t\f ]
quantifier: between 1 , unlimited times, many times possible, giving needed [greedy]

1st capturing group ([a-za-z]:)

[a-za-z] match single character present in list below

a-z single character in range between , z (case sensitive)
a-z single character in range between , z (case sensitive)

: matches character : literally

\s+ match white space character [\r\n\t\f ]
quantifier: between 1 , unlimited times, many times possible, giving needed [greedy]

2nd capturing group (.+?)

.+? matches character (except newline)
quantifier: between 1 , unlimited times, few times possible, expanding needed [lazy]

\s+? match white space character [\r\n\t\f ]
quantifier: between 1 , unlimited times, few times possible, expanding needed [lazy]

microsoft windows network matches characters microsoft windows network literally (case sensitive)

.* matches character (except newline)
quantifier: between 0 , unlimited times, many times possible, giving needed [greedy]

$ assert position @ end of line test (with pretty color coding , all) @ link!


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -