Automation_Anywhere_RPA.psm1

#region v1 Authentication
function Get-AAToken {
    <#
        .Synopsis
            Returns a JWT token from AA Control Room in a Header format.
 
        .DESCRIPTION
            Returns a JWT token from AA Control Room in a Header format.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Credential
            Credential object saved with Get-Credential. If domain user, must use DOMAIN\\username with \\ to escape special characters.
 
         .PARAMETER ApyKey
            API key to be used instead of a password.
         
 
        .EXAMPLE
           $header=Get-AAToken -CR https://mycr.domain.tld -Credential MyCredentialAsset
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [pscredential]$Credential=$MyCred,

           [Parameter(Mandatory=$false)]
           [string]$APIKey=$APIKey
    )

    [uri]$uri="$($CR)/v1/authentication"

    if ($APIKey){$body = @{"username"="$($Credential.username)";"apiKey"="$APIKey"}} 
    else {
    $pwd=[PSCredential]::new(0, $Credential.password).GetNetworkCredential().Password
    $body = @{"username"="$($Credential.username)";"password"="$pwd"}
        }
    Try{
        $token=Invoke-RestMethod -Method Post -Uri $uri -Body ($body|ConvertTo-Json)  -ContentType "application/json"
        $header=@{"X-Authorization"=$token.token}
        $pwd=$null
        $body=$null
        return $header
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }

}

function Remove-AAToken {
    <#
        .Synopsis
            Logout from AA Control Room.
 
        .DESCRIPTION
            Logout from AA Control Room.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
         .PARAMETER HeaderToken
            JWT token acquired with Get-AAToken to perform the logout.
         
 
        .EXAMPLE
           $header=Remove-AAToken -CR https://mycr.domain.tld -Token $HeaderToken
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$HeaderToken=$HeaderToken
    )

    [uri]$uri="$($CR)/v1/authentication/logout"

    Try{
        $logout=Invoke-RestMethod -Method Post -Uri $uri -Header $HeaderToken -ContentType "application/json" -Body "{}"
        return $true
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }

}

#endregion
#region v2 License
function Get-AALicense {
    <#
        .Synopsis
            Returns AA License details.
 
        .DESCRIPTION
           Returns AA License details.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
         
        .EXAMPLE
           Get-AALicense -CR https://mycr.domain.tld -Header (Get-AAToken)
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header
    )
    Try{
        [uri]$uri="$($CR)/v2/license/details"
        $Licenses=Invoke-RestMethod -Method Get -Uri $uri -ContentType "application/json" -Headers $header
        return $Licenses
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}
function Get-AALicenseDetails {
    <#
        .Synopsis
            Returns AA License details.
 
        .DESCRIPTION
           Returns AA License details.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
         
        .EXAMPLE
           Get-AALicenseDetails -CR https://mycr.domain.tld -Header (Get-AAToken)
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header
    )
    Try{
        [uri]$uri="$($CR)/v2/license/product/list"
        $LicenseDetails=Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -Headers $header  -Body "{}"
        return $LicenseDetails
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}
#endregion
#region v2 BotInsight
function Get-AABotInsightRunData {
    <#
        .Synopsis
            Returns AA BotInsight Run Data.
 
        .DESCRIPTION
            Returns AA BotInsight Run Data.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
         
        .EXAMPLE
           Get-AABotInsightRunData -CR https://mycr.domain.tld -Header (Get-AAToken)
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header
    )
    Try{
        [uri]$uri="$($CR)/v2/botinsight/data/api/getbotrundata"
        $Insights=Invoke-RestMethod -Method Get -Uri $uri -ContentType "application/json" -Headers $header
        return $Insights.botRunDataList
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}
function Get-AABusinessTaskLogData {
    <#
        .Synopsis
            Returns AA Business Task Log Data. MUST BE member of the "AAE_Bot Insight Admin" group.
 
        .DESCRIPTION
            Returns AA Business Task Log Data. MUST BE member of the "AAE_Bot Insight Admin" group.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
 
        .PARAMETER botName
            Bot name to search.
                 
        .PARAMETER fromDate
            From Date to search - as a string in (yyyy-mm-ddThh:mm:ss) format.
 
        .PARAMETER toDate
            To Date to search - as a string in (yyyy-mm-ddThh:mm:ss) format.
 
        .PARAMETER Limit
            Limit how many records to return.
 
        .PARAMETER PageNo
            page Numbers to return.
         
        .EXAMPLE
           Get-AABusinessTaskLogData -CR https://mycr.domain.tld -Header (Get-AAToken) -botName 'MyBot'
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header,

           [Parameter(Mandatory=$false)]
           [String]$botName,

           [Parameter(Mandatory=$false)]
           [String]$fromDate,

           [Parameter(Mandatory=$false)]
           [String]$toDate,

           [Parameter(Mandatory=$false)]
           [String]$Limit,

           [Parameter(Mandatory=$false)]
           [String]$pageNo
    )

    [uri]$uri="$($CR)/v2/botinsight/data/api/gettasklogdata"
    if ($botName) {$uri="$($uri)?botname=$($botname)"}
    if ($fromDate) {$uri="$($uri)&fromdate=$($fromDate)"}
    if ($toDate) {$uri="$($uri)&todate=$($toDate)"}
    if ($Limit) {$uri="$($uri)&limit=$($Limit)"}
    if ($pageNo) {$uri="$($uri)&pageno=$($pageNo)"}
    Try{
        $TaskLogData=Invoke-RestMethod -Method Get -Uri $uri -ContentType "application/json" -Headers $header  -Body "{}"
        return $TaskLogData
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}
#endregion

#region v1 Devices
function Get-AARunAsUsers {
    <#
        .Synopsis
            Returns AA RunAsUsers Data.
 
        .DESCRIPTION
            Returns AA RunAsUsers Data.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
         
        .EXAMPLE
           Get-AARunAsUsers -CR https://mycr.domain.tld -Header (Get-AAToken)
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header
    )

    Try{
        [uri]$uri="$($CR)/v1/devices/runasusers/list"
        $RunList=Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -Headers $header  -Body "{}"
        return $RunList
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}

#endregion

#region v3 Workload Management
function Get-AAWorkItemModels {
    <#
        .Synopsis
            Returns AA Work Item models.
 
        .DESCRIPTION
           Returns AA Work Item models.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
         
        .EXAMPLE
           Get-AAWorkItemModels -CR https://mycr.domain.tld -Header (Get-AAToken)
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header
    )
    Try{
        [uri]$uri="$($CR)/v3/wlm/workitemmodels/list"
        $WorkItemModels=Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -Headers $header  -Body "{}" 
        return $WorkItemModels
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}

function Get-AAAutomations {
    <#
        .Synopsis
            Returns AA Work Item models.
 
        .DESCRIPTION
           Returns AA Work Item models.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
         
        .EXAMPLE
           Get-AAAutomations -CR https://mycr.domain.tld -Header (Get-AAToken)
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header
    )
    Try{
        [uri]$uri="$($CR)/v3/wlm/automations/list"
        $WorkItemModels=Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -Headers $header  -Body "{}" 
        return $WorkItemModels
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}
#endregion

#region v2 Repository
function Get-AAFileList {
    <#
        .Synopsis
            Returns AA Files.
 
        .DESCRIPTION
           Returns AA Files.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
         
        .EXAMPLE
           Get-AAAutomations -CR https://mycr.domain.tld -Header (Get-AAToken)
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header
    )
    Try{
        [uri]$uri="$($CR)/v2/repository/file/list"
        $FileList=Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -Headers $header  -Body "{}" 
        return $FileList
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}

#endregion
#region v1 User Management
function Get-AAUser {
    <#
        .Synopsis
            Returns AA User details.
 
        .DESCRIPTION
           Returns AA User details.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
         
        .EXAMPLE
           Get-User -CR https://mycr.domain.tld -Header (Get-AAToken)
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header
    )
    Try{
        [uri]$uri="$($CR)/v1/usermanagement/users/list"
        $Users=Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -Headers $header -Body "{}" 
        return $Users.list
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}

function Get-AAUserRole {
    <#
        .Synopsis
            Returns AA User role details.
 
        .DESCRIPTION
           Returns AA User role details.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
         
        .EXAMPLE
           Get-UserRole-CR https://mycr.domain.tld -Header (Get-AAToken)
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header
    )
    Try{
        [uri]$uri="$($CR)/v1/usermanagement/roles/list"
        $UserRoles=Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -Headers $header -Body "{}" 
        return $UserRoles.list
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}

function New-AAUser {
    <#
        .Synopsis
            Creates a user in AA.
 
        .DESCRIPTION
           Creates a user in AA.
 
        .PARAMETER CR
            URL for Control Room including http/https as needed. If https, valid cert chain must exist.
         
        .PARAMETER Header
            Header retrieved with Get-AAToken with proper token.
 
        .PARAMETER Roles
            Role ID to add the new user to.
 
        .PARAMETER Domain
            Active Directory Domain name.
 
        .PARAMETER Email
            Users e-mail address.
 
        .PARAMETER enableAutoLogin
            Enable auto login.
 
        .PARAMETER Username
            Username.
 
        .PARAMETER firstName
            First Name.
 
        .PARAMETER lastName
            Last Name.
 
        .PARAMETER Description
            Description.
 
        .PARAMETER Disabled
            User is disabled? True/False switch.
 
        .PARAMETER Password
            Initial Password (only for local users).
 
        .PARAMETER licenseFeatures
            License Features.
         
        .EXAMPLE
           New-AAUser -CR https://mycr.domain.tld -Header (Get-AAToken)
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$CR=$CR,

           [Parameter(Mandatory=$false)]
           [Hashtable]$Header=$Header,

           [Parameter(Mandatory=$true)]
           [Integer]$Roles,

           [Parameter(Mandatory=$false)]
           [String]$Domain,
        
           [Parameter(Mandatory=$true)]
           [String]$Email,

           [Parameter(Mandatory=$false)]
           [Switch]$enableAutoLogin=$false,

           [Parameter(Mandatory=$true)]
           [Hashtable]$username,

           [Parameter(Mandatory=$true)]
           [String]$firstName,

           [Parameter(Mandatory=$true)]
           [String]$lastName,

           [Parameter(Mandatory=$true)]
           [String]$description,

           [Parameter(Mandatory=$false)]
           [Switch]$disabled=$false,

           [Parameter(Mandatory=$false)]
           [SecureString]$password,

           [Parameter(Mandatory=$true)]
           [String]$licenseFeatures

           )

    $body = @{"roles"="$($roles)"
            "domain"="$($Domain)"
            "email"="$($Email)"
            "enableAutoLogin"="$($enableAutoLogin)"
            "username"="$($username)"
            "firstName"="$($firstName)"
            "lastName"="$($lastName)"
            "description"="$($description)"
            "disabled"="$($disabled)"
            "licenseFeatures"="$($licenseFeatures)"}
## Still need to add password to the hash above. ##

    Try{
        [uri]$uri="$($CR)/v1/usermanagement/users"
        $Users=Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -Headers $header -Body  ($body|ConvertTo-Json) 
        return $true
    } Catch{
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Warning -Message "StatusCode: $($_.Exception.Response.StatusCode.value__) "
        Write-Warning -Message "StatusDescription: $($_.Exception.Response.StatusDescription)"
        Write-Warning -Message "Details: $_"
    }
}

#endregion