Find-SMBShare.psm1

Function Find-SMBShare 
{
    #FrankTucker@lan-sol.com
    
    <#
    .SYNOPSIS
         Function searchs for shared items on a computer/s and reports
    .EXAMPLE
        PS> Find-SMBShare -Computername Workstation1 -Credential domain\username | fl
 
        ComputerName : Workstation1
        Status : Connected
        TotalShare : 8
        AdminShare : {ADMIN$, C$, E$...}
        PrinterShare : Brother7840BW, CannonColor
        NotAdminFound : True
        NotAdminShare : DDrive
     
        This example finds shared items on \\Workstation1.
    .EXAMPLE
        PS> $Credential = (Get-Credential)
        PS> Find-SMBShare -Computername Server,Workstation1 -Credential $Credential
         
        This example finds shared items on \\Server and \\Workstation1 with a saved PSCredential
    .EXAMPLE
        PS> Find-SMBShare
         
        This example finds shared items on localhost with the current logon user credential.
    .PARAMETER computername
        The computer/s to search
    .PARAMETER credential
        Administrative account on computer/s
        Can be a [string]username or [object]pscredential
    #>


    [cmdletBinding()]
    param(
        [Parameter(Mandatory=$False,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True,
                   HelpMessage="The. Computer. Name.")]
        [Alias('Hostname','cn')]
        [object[]]$ComputerName,

        [parameter(Mandatory=$False,
                    ValueFromPipeLine=$False,
                    ValueFromPipelineByPropertyName=$False,
                    HelpMessage="Admin. Username. Or. PSCredential")]
        [Alias('user','username','pscredential')]
        [object]$Credential      
    )   

    Write-Verbose "Checking Credential Param"
    if ($Credential.Count -ne 0)
    {       
        if ($Credential.ToString() -contains 'System.Management.Automation.PSCredential')
        {
            $PSCredential = $Credential           
        }
            else
            {
                Write-Verbose "Getting Credential"
                $PSCredential = (Get-Credential -Credential $Credential)       
            } 
    }

    Write-Verbose "Checking ComputerName Param"
    if ($ComputerName.count -eq 0)
    {
        $ComputerName = $env:COMPUTERNAME
    }

    Write-Verbose "Connecting to $computer" 
    foreach ($computer in $computername)
    {
        try
        { 
        if ($PSCredential.count -ne 0)
        {
            $Session = New-CimSession -ComputerName $Computer -Credential $PSCredential -ErrorAction Stop
        }
            else
            {
                $Session = New-CimSession -ComputerName $computer -ErrorAction stop
            }

        Write-Verbose "Getting Data from $computer"           
        $Share = ( Get-CimSession -InstanceId $Session.InstanceId | Get-CimInstance -ClassName Win32_Share)
        $PrinterShare = ($Share | Where-Object {$_.name -notlike '*$*'} | Where-Object {$_.path -notlike '*:\*'})
        $AdminShare = ($share | Where-Object {$_.type -ne 0 } | Where-Object {$_.name -like '*$' } )
        $NotAdminShare = ($share | Where-Object {$_.type -eq 0} |  Where-Object {$_.name -notlike '*$'} )                       
                    
        $Properties = @{ComputerName = $computer
                     Status = 'Connected'
                     Total = $share.count
                     AdminShare = $adminShare.name
                     PrinterShare = $printershare.name
                     NotAdminFound = [boolean]$NotAdminShare
                     NotAdminShare = $notadminshare.name}
        
        Remove-CimSession -instanceID $Session.InstanceId

        } 
        catch
        {
            Write-Verbose "Couldn't connect to $computer"
            Write-Verbose "Check Credential"
            $properties = @{ComputerName = $computer
                          Status = 'Disconnected'
                          Total = $null
                          AdminShare = $null
                          PrinterShare = $null
                          NotAdminFound = [boolean]$null
                          NotAdminShare = $null}
        } 
        finally
        {
            $Obj = New-Object -TypeName PSObject -Property $properties 
            $Obj.PSObject.typenames.insert(0,'find.smbshare.object')
            Write-Output $Obj
        }
    }
}