Base
This commit is contained in:
53
functions/public/New-Session.ps1
Normal file
53
functions/public/New-Session.ps1
Normal file
@@ -0,0 +1,53 @@
|
||||
function New-Session {
|
||||
[CmdletBinding(DefaultParameterSetName="Credentials")]
|
||||
param (
|
||||
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[string]$URL,
|
||||
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[string]$AppID,
|
||||
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2,ParameterSetName="Credentials")]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[pscredential]$Credentials,
|
||||
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2,ParameterSetName="Token")]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[string]$Token,
|
||||
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=3)]
|
||||
[switch]$IgnoreSSL = $false
|
||||
)
|
||||
switch ($PSCmdlet.ParameterSetName) {
|
||||
"Credentials" {
|
||||
$_bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credentials.Password)
|
||||
$_password = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($_bstr)
|
||||
$_uri = "$URL/api/$AppID/user"
|
||||
$_auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($Credentials.UserName):$_password"))
|
||||
$_headers = @{
|
||||
"Accept" = "application/json"
|
||||
"Content-Type" = "application/json"
|
||||
"Authorization" = "Basic $_auth"
|
||||
}
|
||||
|
||||
$_response = Invoke-RestMethod -Method Post -Uri $_uri -Headers $_headers -ErrorAction SilentlyContinue
|
||||
|
||||
if ($_response.success -ne $true) { return $_response.error }
|
||||
|
||||
$script:ipamAuthType = "Credentials"
|
||||
$script:ipamAuth = $true
|
||||
$script:ipamToken = $_response.data.token
|
||||
$script:ipamAppID = $AppID
|
||||
$script:ipamURL = $URL
|
||||
$script:ipamCredentials = $Credentials
|
||||
$script:ipamExpires = Get-Date $_response.data.expires
|
||||
}
|
||||
"Token" {
|
||||
$script:ipamAuthType = "Token"
|
||||
$script:ipamAuth = $true
|
||||
$script:ipamToken = $Token
|
||||
$script:ipamAppID = $AppID
|
||||
$script:ipamURL = $URL
|
||||
$script:ipamExpires = "Never"
|
||||
}
|
||||
}
|
||||
}
|
||||
Export-ModuleMember -Function New-Session
|
||||
Reference in New Issue
Block a user