Functions/Public/Get-DMIDepartment.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
61
62
63
64
65
66
67
68
69
70
71
72
73
using namespace System.Management.Automation
#These classes are a necessary workaround to use variables as validate sets. Hopefully Microsoft bakes this in someday.
class ValidBannerOrgGenerator : IValidateSetValuesGenerator {
    [string[]] GetValidValues() {
        return $Script:BannerOrgCodes
    }
}

class ValidDeptnameGenerator : IValidateSetValuesGenerator {
    [string[]] GetValidValues() {
        return $Script:Deptnames
    }
}
<#
.Synopsis
   Returns the information for all departments or a specified department.
.DESCRIPTION
   Returns the information for all departments or a specified department.
.PARAMETER BannerOrg
    Banner organization code. This is an alphanumeric code in 4 sections. Ex: '1A1-NA-NA0-283'
.PARAMETER Deptname
    Plaint text department name. Ex: 'Academic Outreach'
.EXAMPLE
   PS C:\> Get-DMIDepartment

   This will return all departments.
.EXAMPLE
   PS C:\> Get-DMIDepartment -BannerOrg '1A1-NA-NA0-283'

   This will return a specific department based on banner organization code.
.EXAMPLE
    PS C:\> Get-DMIDepartment -Deptname 'Academic Outreach'

    This will return a specific department based on department name.
#>

function Get-DMIDepartment {
    [CmdletBinding(DefaultParametersetname='BannerOrg')]
    param (    
        [parameter(ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = 'BannerOrg')]
        [ValidateSet([ValidBannerOrgGenerator],
            ErrorMessage = "'{0}' is not a valid banner org code.")]
        [Alias("owner_code")]    
        [String]$BannerOrg = '%',

        [parameter(ParameterSetName = 'Deptname')]
        [ValidateSet([ValidDeptnameGenerator],
            ErrorMessage = "'{0}' is not a valid department name.")]    
        [String]$Deptname = '%'
    )

    begin {
        
    }
    
    process {
        $Splat = @{
            Datasource = $Script:SQLiteDBPath
            Query = 'SELECT * FROM DMI WHERE Banner_Org like @BannerOrg AND Deptname like @Deptname'
            SqlParameters = @{
                BannerOrg = $BannerOrg.Replace('*','%')
                Deptname = $Deptname.Replace('*','%')
            }
        }
        
        Invoke-SqliteQuery @Splat
    }
    
    end {
        
    }
}