Skip to content

Instantly share code, notes, and snippets.

@jeffpatton1971
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffpatton1971/7ff32fa597690073eb0f to your computer and use it in GitHub Desktop.
Save jeffpatton1971/7ff32fa597690073eb0f to your computer and use it in GitHub Desktop.
Return a list of file or print shares using the net command.
Function Get-NetShare
{
<#
.SYNOPSIS
Return a list of shares without using WMI
.DESCRIPTION
This function returns a list of shares using the old net view command. This
works well in situations where a fierwall may be blocking access.
.PARAMETER ComputerName
The name of the server that has file or print shares
.PARAMETER Type
This will be either Print or Disk
Print returns printer shares
Disk returns file shares
.EXAMPLE
Get-NetShare -ComputerName server-01 -Type Print
Server Share Path
------ ----- ----
server-01 hp01 \\server-01\hp01
server-01 hp02 \\server-01\hp02
server-01 hp03 \\server-01\hp03
Description
-----------
This example shows the basic usage for this function
.NOTES
FunctionName : Get-NetShares
Created by : jspatton
Date Coded : 10/08/2014 11:08:30
.LINK
https://code.google.com/p/mod-posh/wiki/ComputerManagement#Get-NetShares
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory = $true)]
[string]$ComputerName,
[ValidateSet("Print", "Disk", IgnoreCase = $true)]
[parameter(Mandatory = $true)]
[string]$Type = "Print"
)
Begin
{
Write-Verbose "Getting share from server"
$List = net view "\\$($Server)" |Select-String $Type
Write-Verbose "$($List)"
}
Process
{
foreach ($Entry in $List)
{
Write-Verbose "Converting regex to string"
$Line = $Entry.ToString();
Write-Debug $Line
Write-Verbose "Building share property"
$Share = $Line.Substring(0,$Line.IndexOf($Type)).trim()
Write-Verbose "Building Description property"
$Description = $Line.Substring($Line.IndexOf($Type),$Line.Length-$Line.IndexOf($Type)).Replace($Type,"").Trim()
$Path = "\\$($Server)\$($Share)"
New-Object -TypeName psobject -Property @{
Server = $Server
Share = $Share
Description = $Description
Path = $Path
} |Select-Object -Property Server, Share, Description, Path
}
}
End
{
}
}
@jeffpatton1971
Copy link
Author

Added description to the mix

@jeffpatton1971
Copy link
Author

Decided to use variables inside my parsing ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment