HornbillAPI.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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
############################## # Hornbill XMLMC API Azure Powershell Module # #.DESCRIPTION # This module includes functions to allow your Powershell scripts to make and send API calls # to your Hornbill instance, and process responses accordingly. # # Requires Powershell 3.0 or above. # #.NOTES # See example scripts and function documentation for guidance on usage. ############################## # Initialise the module-level variables [string]$script:InstanceName = "" [string]$script:InstanceZone = "" [string]$script:APIKey = "" [string]$script:XMLMCParams = "" [string]$script:InstanceURL = "" ############################## # .SYNOPSIS # Allows you to define the Hornbill instance # #.DESCRIPTION # MANDATORY - Allows your Powershell script to define the Hornbill instance to connect # to, the zone in which it resides, and the API key to use for session generation. # #.PARAMETER Instance # The (case-sensitive) instance name that you wish to connect to. # #.PARAMETER Zone # The (case-sensitive) zone in which the Hornbill instance resides. # If not supplied, defaults to: eur # #.PARAMETER Key # The API key to use to generate authenticate against the Hornbill instance with. # #.EXAMPLE # Set-HB-Instance "yourinstancename" "eur" "yourapikeygoeshere" ############################## function Set-HB-Instance { Param( [Parameter(Mandatory=$True, HelpMessage="Specify the name of the Instance to connect to (case sensitive)")] [ValidateNotNullOrEmpty()] [string]$Instance, [Parameter(Mandatory=$False, HelpMessage="Specify the Zone in which the Instance is run. Defaults to 'eur' (case sensitive)")] [string]$Zone="eur", [Parameter(Mandatory=$True, HelpMessage="Specify the API Key to authenticate the session against")] [ValidateNotNullOrEmpty()] [string]$Key ) $script:InstanceName = $Instance $script:InstanceZone = $Zone $script:APIKey = $Key $script:InstanceURL = "https://"+$script:InstanceZone+"api.hornbill.com/"+$script:InstanceName+"/xmlmc/" } ############################## # .SYNOPSIS # Define proxy details to connect through # #.DESCRIPTION # If you are using a proxy to connect to the internet, then this function allows you to define # the proxy address and authentication details (where applicable) # #.PARAMETER ProxyAddress # Mandatory - URI of the Proxy server to connect through # #.PARAMETER ProxyCredentials # Either: # - String containing the username to authenticate against the proxy with (will prompt for password) # - A PSCredential object, such as one generated by the Get-Credential cmdlet. # # By not providing this parameter, the module will use the credentials of the current Windows user # #.EXAMPLE # Set-HB-Proxy "http://yourproxyaddress:80/" # The above will route the requests through your proxy, and will authenticate using the current user details # # Set-HB-Proxy "http://yourproxyaddress:80/" "DOMAIN01\User01" # The above will route the requests through your proxy, and will authenticate using "DOMAIN01\User01" account ############################## function Set-HB-Proxy { Param( [Parameter(Mandatory=$True, HelpMessage="Specify the address of your Proxy")] [ValidateNotNullOrEmpty()] [string]$ProxyAddress, [Parameter(Mandatory=$False, HelpMessage="Specify the credentials to authenticate against your Proxy")] [System.Management.Automation.PSCredential] [System.Management.Automation.CredentialAttribute()] $ProxyCredentials ) $script:ProxyURI = $ProxyAddress if($ProxyCredentials) { $script:ProxyCreds = $ProxyCredentials } } ############################## # .SYNOPSIS # Add a parameter to the XMLMC request # #.DESCRIPTION # Add a parameter to the XMLMC request # #.PARAMETER ParamName # Mandatory - the name of the parameter # #.PARAMETER ParamValue # Mandatory - the [string] value of the parameter # #.PARAMETER ParamAllowEmpty # Boolean, allow empty string to be passed as a parameter value # #.PARAMETER ParamAttribs # Any attributes to add to the XMLMC request # #.EXAMPLE # Add-HB-Param "application" "com.hornbill.servicemanager" # Add-HB-Param "h_class" "computer" "onError=""omitElement"" " # # Note the escaped double-quotes in the ParamAttribs string. ############################## function Add-HB-Param { Param( [Parameter(Mandatory=$True, HelpMessage="Specify the name of the Parameter to add")] [ValidateNotNullOrEmpty()] [string]$ParamName, [Parameter(Mandatory=$False, HelpMessage="Specify the Value of the Parameter")] [string]$ParamValue = "", [Parameter(Mandatory=$False, HelpMessage="Specify attributes to add to the Parameter XML node")] [boolean]$ParamAllowEmpty = $False, [Parameter(Mandatory=$False, HelpMessage="Specify attributes to add to the Parameter XML node")] [string]$ParamAttribs = "" ) if($ParamName.length -eq 0){ return "Parameter name length needs to be greater than zero" } if(-not $ParamAllowEmpty -And $ParamValue -eq ""){ return } $script:EncodedParamVal = "" if($ParamValue -ne ""){ $script:EncodedParamVal = [System.Security.SecurityElement]::Escape($ParamValue) } $CurrentParam = "<"+$ParamName if($ParamAttribs -and $ParamAttribs.length -gt 0){ $CurrentParam = $CurrentParam + " " + $ParamAttribs } $CurrentParam = $CurrentParam + ">" + $EncodedParamVal + "</"+$ParamName+">" $script:XMLMCParams = $script:XMLMCParams + $CurrentParam } ############################## # .SYNOPSIS # Open a new XML element # #.DESCRIPTION # Allows for the building of complex XML # #.PARAMETER Element # The name of the complex element to open # #.EXAMPLE # Open-HB-Element "primaryEntityData" ############################## function Open-HB-Element { Param( [Parameter(Mandatory=$True, HelpMessage="Specify the name of the Parameter to add")] [ValidateNotNullOrEmpty()] [string]$Element ) $script:XMLMCParams = $script:XMLMCParams + "<"+$Element+">" } ############################## # .SYNOPSIS # Close an already open XML element # #.DESCRIPTION # Allows for the building of complex XML # #.PARAMETER Element # The name of the complex element to close # #.EXAMPLE # Close-HB-Element "primaryEntityData" ############################## function Close-HB-Element { Param( [Parameter(Mandatory=$True, HelpMessage="Specify the name of the Parameter to add")] [ValidateNotNullOrEmpty()] [string]$Element ) $script:XMLMCParams = $script:XMLMCParams + "</"+$Element+">" } ############################## # .SYNOPSIS # Return XML parameters # #.DESCRIPTION # Returns XML string of parameters that have been added by Add-HB-Params, Open-HB-Element or Close-HB-Element # #.EXAMPLE # Get-HB-Params ############################## function Get-HB-Params { if($script:XMLMCParams.length -gt 0) { return "<params>"+$script:XMLMCParams+"</params>" } return "" } ############################## # .SYNOPSIS # Clear existing XML parameters # #.DESCRIPTION # Clears any existing XMLMC parameters that have been added # #.EXAMPLE # Clear-HB-Params ############################## function Clear-HB-Params { $script:XMLMCParams = "" } ############################## # .SYNOPSIS # Base64 encode a string # #.DESCRIPTION # Returns a Base64 encoded string from a given UTF8 string # #.PARAMETER StringVal # The string to encode # #.EXAMPLE # ConvertTo-HB-B64Encode "encode this please" ############################## function ConvertTo-HB-B64Encode { Param( [Parameter(Mandatory=$True, HelpMessage="Specify the string to Base-64 encode")] [ValidateNotNullOrEmpty()] [string]$StringVal ) $UnencodedBytes = [System.Text.Encoding]::UTF8.GetBytes($StringVal) $EncodedText =[Convert]::ToBase64String($UnencodedBytes) return $EncodedText } ############################## # .SYNOPSIS # Decode a Base64 encoded string # #.DESCRIPTION # Returns a UTF8 string from a given Base64 endcoded string # #.PARAMETER StringVal # The string to decode # #.EXAMPLE # ConvertTo-HB-B64Decode "ZW5jb2RlIHRoaXMgcGxlYXNl" ############################## function ConvertTo-HB-B64Decode { Param( [Parameter(Mandatory=$True, HelpMessage="Specify the Base-64 string to decode")] [ValidateNotNullOrEmpty()] [string]$B64Val ) $DecodedString = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($B64Val)) return $DecodedString } ############################## # .SYNOPSIS # Invokes the XMLMC API call # #.DESCRIPTION # Takes the API Service and Method as inputs to this function, and any parameters # added with Add-HB-Param, Open-HB-Element or Close-HB-Element, and invokes an API call using # the instance details defined with the Set-Instance function. # # Returns a Powershell Object containing: # .status - the status of the API call or HTTP response # .params - any returned parameters from the API # .error - any returned errors if the HTTP request or API call fails # #.PARAMETER XMLMCService # The Service that contains the API on the Hornbill instance # #.PARAMETER XMLMCMethod # The API Method # #.EXAMPLE # $xmlmcCall = Invoke-HB-XMLMC "session" "getSessionInfo" # # If successful This would return: # $xmlmcCall.status = "ok" # $xmlmcCall.params = A PSObject containing all output parameters returned by the # session::getSessionInfo API ############################## function Invoke-HB-XMLMC { Param( [Parameter(Mandatory=$True, HelpMessage="Specify the XMLMC Service")] [ValidateNotNullOrEmpty()] [string]$XMLMCService, [Parameter(Mandatory=$True, HelpMessage="Specify the XMLMC Method")] [ValidateNotNullOrEmpty()] [string]$XMLMCMethod ) $script:responseStatus = "" $script:responseParams = "" $script:responseError = "" try { # Build XMLMC call $script:mcParamsXML = Get-HB-Params $script:bodyString = '<methodCall service="'+$XMLMCService+'" method="'+$XMLMCMethod+'">'+$script:mcParamsXML+'</methodCall>' $script:body = [XML]$script:bodyString # Build HTTP request headers $script:headers = @{} $script:headers["Content-Type"] ="text/xmlmc" $script:headers["Cache-control"]="no-cache" $script:headers["Authorization"]="ESP-APIKEY "+$script:APIKey $script:headers["Accept"]="text/xml" # Build URI for HTTP request $script:URI = $script:InstanceURL + $XMLMCService+"/?method="+$XMLMCMethod # Invoke HTTP request if($script:ProxyURI -and $script:ProxyURI -ne ""){ if($script:ProxyCreds) { $script:ProxyCreds $r = Invoke-WebRequest -Uri $script:URI -UseBasicParsing -Method Post -Headers $script:headers -ContentType "text/xmlmc" -Body $script:body -ErrorAction:Stop -Proxy $script:ProxyURI -ProxyCredential $script:ProxyCreds } else { $r = Invoke-WebRequest -Uri $script:URI -UseBasicParsing -Method Post -Headers $script:headers -ContentType "text/xmlmc" -Body $script:body -ErrorAction:Stop -Proxy $script:ProxyURI -ProxyUseDefaultCredentials } } else { $r = Invoke-WebRequest -Uri $script:URI -UseBasicParsing -Method Post -Headers $script:headers -ContentType "text/xmlmc" -Body $script:body -ErrorAction:Stop } # Read and process response [XML]$script:xmlResponse = $r.Content $script:responseStatus = $script:xmlResponse.methodCallResult.status if(($script:responseStatus -eq "fail") -or ($script:responseStatus -eq "false")){ $script:responseError = $script:xmlResponse.methodCallResult.state.error } else { $script:responseParams = $script:xmlResponse.methodCallResult.params } } catch { # HTTP request failed - return exception in response $script:responseError = $_.Exception $script:responseStatus = "fail" } # Clear the XMLMC parameters now ready for the next API call Clear-HB-Params # Return an object of the results. $script:resultObject = New-Object PSObject -Property @{ Status = $script:responseStatus Params = $script:responseParams Error = $script:responseError } # Return result object return $script:resultObject } # Export the functions available to the script importing this module Export-ModuleMember -Function '*' |