PublicFunctions/Get-ServerCollection.ps1

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
Function Get-ServerCollection
{
    <#
        .Synopsis
            Gets a (filtered) list of servers from a CSV File
        .DESCRIPTION
            This is the primary cmdlet for this module. It imports a .csv file
            and uses the resultant PS Custom Object as a starting point for adding
            new columns. Often the next cmdlet in the pipe line will be
            Test-ServerConnectionOnPipeline to verify the server (ComputerName)
            in on the network and can be contacted by various protocols

            The parameters are both optional.
            Leaving one blank applies no filter for that parameter.
            Parameter validation is used to provide tab completion for rapid Ad Hoc use

            If there is another collection of ComputerNames available more convenient
            than a .csv file such as Active Directory or a .txt file, use
            Get-ServerObjectCollection

            After that call any one of the cmdlets that return information about each server
        .EXAMPLE
            Get-MyServerCollection
            Returns everything
        .EXAMPLE
            Get-MyServerCollection -Role Web
            Returns all of the Web Servers
        .EXAMPLE
            Get-MyServerCollection -Role SQL -Location WA
            Returns the SQL Servers in Washington
        .EXAMPLE
            Get-MyServerCollection -Role SQL | Test-ServerConnectionOnPipeline
            Returns all of the SQL servers and their availability on the network
        .EXAMPLE
            Get-MyServerCollection | Test-ServerConnectionOnPipeline | Get-OSCaptionOnPipeline
            Gets OS Version of all servers
    #>


    [CmdletBinding()]

    Param (
        [ValidateSet("Web", "SQL", "DC")]
        [string]$Role,
        [ValidateSet("AZ", "WA")]
        [string]$Location
    )

    $ScriptPath = $PSScriptRoot
    $ComputerNames = '..\Servers.csv'

    If ($Role -ne "")  {$ModRole = $Role}
       Else {$ModRole = "*"}
    If ($Location -ne "")  {$ModLocation = $Location}
       Else {$ModLocation = "*"}

    $Return = Import-Csv -Path "$ScriptPath\$ComputerNames"  |
        Where-Object {($_.Role -like $ModRole) -and ($_.Location -like $ModLocation)}

    Return $Return
}