Find-SMBShare.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
        }
    }
}