PoshBot.LithnetRMA.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
Import-Module LithnetRMA

# Slack text width with the formatting we use maxes out ~80 characters...
$Width = 80
$CommandsToExport = @()

function Find-Person {
    <#
    .SYNOPSIS
        Find MIM Resource (Person)
    .PARAMETER Identity <AccountName>|<DisplayName>
        MIM Service AccountName or DisplayName
    .USAGE
        !Find <AccountName>|<DisplayName>
    .EXAMPLE
        !Find 'Darren Robinson'
    .EXAMPLE
        !Search 'Darren Robinson'
    .Example
        !Find drobinson
    .Example
        !Search drobinson
    .DESCRIPTION !Find
        Search the MIM Service for Person objects using AccountName or DisplayName
    .LINK
        https://blog.darrenjrobinson.com
    #>

    [cmdletbinding()]
    [PoshBot.BotCommand(
        CommandName = 'Find',
        Aliases = ('Search', 'Search-Resources'),
        Permissions = 'read'
    )]
    param(
        # !Find <AccountName>|<DisplayName>
        [parameter(position = 1,
            parametersetname = 'id', mandatory)]
        [string]$Identity,

        [PoshBot.FromConfig('MIMServiceCreds')]
        [parameter(mandatory)]
        [PSCredential]$MIMServiceCreds,

        [PoshBot.FromConfig('MIMServiceAddress')]
        [parameter(mandatory)]
        [string]$MIMServiceAddress
    )

    # Person Output Attributes
    $MIMPersonTemplate = [pscustomobject][ordered]@{ 
        AccountName = $null 
        DisplayName = $null 
        JobTitle    = $null
        City        = $null              
    } 

    # Connect to MIM Service using LithnetRMA PSModule using configuration info from the PoshBot Config
    Set-ResourceManagementClient -BaseAddress $MIMServiceAddress -Credentials $MIMServiceCreds
    # Try to find the user using DisplayName
    $result = Search-Resources -Xpath "/Person[starts-with(DisplayName, `'$Identity`')]" -AttributesToGet @("AccountName", "DisplayName", "JobTitle", "City", "EmployeeType", "Email")

    if (!$result.AccountName) {        
        # Failed finding user by DisplayName, try to find the user using AccountName
        $result = Search-Resources -Xpath "/Person[starts-with(AccountName, `'$Identity`')]" -AttributesToGet @("AccountName", "DisplayName", "JobTitle", "City", "EmployeeType", "Email")
    }

    if ($result.AccountName) {
        $resultList = @()
        foreach ($person in $result) {
            $o = $MIMPersonTemplate.PsObject.Copy()
            $o.AccountName = $person.AccountName
            $o.DisplayName = $person.DisplayName
            $o.City = $person.City
            $o.JobTitle = $person.JobTitle 
            
            $resultList += $o 
        }      
        $o = $resultList | Format-Table -AutoSize | Out-String -Width $Width
    }
    else {
        $o = "User `'$($Identity)`' not found!"
    }

    New-PoshBotCardResponse -Type Normal -Text $o 
}
$CommandsToExport += 'Find-Person'

function Get-Person {
    <#
    .SYNOPSIS
        Get MIM Resource
    .PARAMETER Identity <AccountName>|<DisplayName>
        MIM Service AccountName or DisplayName
    .USAGE
        !Person <AccountName>|<DisplayName>
    .EXAMPLE
        !Person 'Darren Robinson'
    .EXAMPLE
        !User 'Darren Robinson'
    .Example
        !Person drobinson
    .DESCRIPTION !Person
        Get a Person Object from the MIM Service using AccountName or DisplayName
    .LINK
        https://blog.darrenjrobinson.com
    #>

    [cmdletbinding()]
    [PoshBot.BotCommand(
        CommandName = 'Person',
        Aliases = ('User', 'Get-Resource','GetUser', 'GetPerson'),
        Permissions = 'read'
    )]
    param(
        # !Person <AccountName>|<DisplayName>
        [parameter(position = 1,
            parametersetname = 'id')]
        [string]$Identity,

        [PoshBot.FromConfig('MIMServiceCreds')]
        [parameter(mandatory)]
        [PSCredential]$MIMServiceCreds,

        [PoshBot.FromConfig('MIMServiceAddress')]
        [parameter(mandatory)]
        [string]$MIMServiceAddress
    )

    # Person Output Attributes
    $MIMPersonTemplate = [pscustomobject][ordered]@{ 
        AccountName  = $null 
        DisplayName  = $null 
        EmployeeType = $null 
        JobTitle     = $null
        City         = $null   
        Email        = $null                 
    } 

    # Connect to MIM Service using LithnetRMA PSModule using configuration info form the PoshBot Config
    Set-ResourceManagementClient -BaseAddress $MIMServiceAddress -Credentials $MIMServiceCreds

    try {
        # Try to find the user using DisplayName
        $result = Get-Resource -AttributeName DisplayName -AttributeValue "$($Identity)" -ObjectType Person 
    }
    catch {
        $result = Get-Resource -AttributeName AccountName -AttributeValue "$($Identity)" -ObjectType Person 
    }
    
    if ($result.AccountName) {
        $o = $MIMPersonTemplate.PsObject.Copy()
        $o.AccountName = $result.AccountName
        $o.DisplayName = $result.DisplayName
        $o.City = $result.City
        $o.JobTitle = $result.JobTitle    
        $o.EmployeeType = $result.EmployeeType
        $o.Email = $result.Email                    
        $o = $o | Format-List | Out-String -width $Width       
    }
    else {
        $o = "`'$($Identity)`' not found. Try Searching for the user using !Find <accountname>|<fullname>"
    }
    
    New-PoshBotCardResponse -Type Normal -Text $o 
}
$CommandsToExport += 'Get-Person'

Export-ModuleMember -Function $CommandsToExport