You can probably achieve the desired result with a bit of regex
Powershell V1 compatible
switch -regex (net share){
'^(\S+)\s+(\w:.+?)(?=\s{2,})' {
New-Object PSObject -Property @{
ShareName = $matches.1
Resource = $matches.2
}
}
}
Powershell 3.0+
switch -regex (net share){
'^(\S+)\s+(\w:.+?)(?=\s{2,})' {
[PSCustomObject]@{
ShareName = $matches.1
Resource = $matches.2
}
}
}
On powershell 5.1 you can use ConvertFrom-String
with "training" data. It can be real sample data or generic. It may take some adjusting for your specific environment but this worked well in testing.
$template = @'
{Share*:ADMIN$} {Path: C:\Windows} {Note:Remote Admin}
{Share*:Admin} {Path: E:\Admin} {Note:Default share}
{Share*:Apps} {Path: D:\Apps} {Note:test}
'@
net share | Select-Object -Skip 4 | Select-Object -SkipLast 2 |
ConvertFrom-String -TemplateContent $template -OutVariable ShareList
Any output shown should now be contained in the variable $ShareList
$Sharelist | Get-Member -MemberType NoteProperty
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Note NoteProperty string Note=Remote Admin
Path NoteProperty string Path=C:\Windows
Share NoteProperty string Share=ADMIN$
You could also use psexec to get the information remotely and apply either proposed solution.
switch -regex (.\PsExec.exe -nobanner \\$RemoteComputer cmd "/c net share"){
'^(\S+)\s+(\w:.+?)(?=\s{2,})' {
[PSCustomObject]@{
ShareName = $matches.1
Resource = $matches.2
}
}
}
or
$template = @'
{Share*:ADMIN$} {Path: C:\Windows} {Note:Remote Admin}
{Share*:Admin} {Path: E:\Admin} {Note:Default share}
{Share*:Apps} {Path: D:\Apps} {Note:Test}
'@
.\PsExec.exe -nobanner \\$RemoteComputer cmd "/c net share" |
Select-Object -Skip 4 | Select-Object -SkipLast 2 |
ConvertFrom-String -TemplateContent $template -OutVariable ShareList