Groups.psm1





<#
.SYNOPSIS
    The get groups operation will retrieve a list of groups from your company.
.DESCRIPTION
    The get groups operation will retrieve a list of groups from your company. This can be either all groups, or groups filtered by groupmember.
.PARAMETER Name
    Specifies the name of an existing variable to retrieve, can be specified as an array of strings to retrieve multiple variables
.PARAMETER LinkedItemGuid
    When specified gets the automation variables belonging to a specific item.
.PARAMETER CompanyName
    The companyname that's used in the helloId URL to know which HelloID tenant to talk to. Required if not connected with Connect-HelloId.
.PARAMETER ApiKey
    The Apikey to use for the api call. Required if not connected with Connect-HelloId.
.PARAMETER ApiSecret
    The Apisecret belonging to the apikey, has to be a securestring. Required if not connected with Connect-HelloId.
.EXAMPLE
    
.EXAMPLE
    
.INPUTS
    
.OUTPUTS
    
#>

function Get-HidGroup {
    [CmdletBinding(DefaultParameterSetName = 'guid',PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param ( 
        # the name of an existing variable
        [Parameter(Mandatory = $false,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "guid")]
        [ValidateNotNullOrEmpty()]
        [guid[]]$GroupGuid, 
    
        # the name of an existing variable
        [Parameter(Mandatory = $false,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "Name")]
        [ValidateNotNullOrEmpty()]
        [string[]]$Name,        

        # Company name used in the URL
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$CompanyName,
        
        # Api key
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$ApiKey,

        # Api secret
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [securestring]$ApiSecret
    )
    
    begin {

        if ($PSBoundParameters.ContainsKey("CompanyName") -AND $PSBoundParameters.ContainsKey("ApiKey") -AND $PSBoundParameters.ContainsKey("ApiSecret") ){
            Write-Verbose -Message "Using connectioninfo and credentials from parameter"
            #Create credential object for authentication
            $Cred = New-Object System.Management.Automation.PSCredential ($ApiKey, $ApiSecret)
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }
        
    } #End begin
    
    process {        

        if ($PSBoundParameters.ContainsKey("GroupGuid")){            
            foreach ($guid in $GroupGuid){
                $URI = "https://$CompanyName.helloid.com/api/v1/groups/$guid"
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing               
                $output
            }

        }
        elseif ($PSBoundParameters.ContainsKey("Name")) {
            foreach ($item in $Name){
                $URI = "https://$CompanyName.helloid.com/api/v1/groups/$item"
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                $output
            }
        }        
        else {
            $URI = "https://$CompanyName.helloid.com/api/v1/groups"
            $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
            $output            
        }
    } #End process
    
    end {
        
    } #End end
} #End function








<#
.SYNOPSIS
    The get groups operation will retrieve a list of groups from your company.
.DESCRIPTION
    The get groups operation will retrieve a list of groups from your company. This can be either all groups, or groups filtered by groupmember.
.PARAMETER Name
    Specifies the name of an existing variable to retrieve, can be specified as an array of strings to retrieve multiple variables
.PARAMETER UserNames
    Usernames to add to the group
.PARAMETER UserGuids
    UserGuids to add to the group
.PARAMETER Enabled
    Is the new group enabled or disabled.
.PARAMETER Default
    Is the new group a default group
.PARAMETER QrEnabled
    Is QR-code generation for group members enabled
.PARAMETER ManagedByUserGuid
    The user Guid that will be considered the manager
.PARAMETER CompanyName
    The companyname that's used in the helloId URL to know which HelloID tenant to talk to. Required if not connected with Connect-HelloId.
.PARAMETER ApiKey
    The Apikey to use for the api call. Required if not connected with Connect-HelloId.
.PARAMETER ApiSecret
    The Apisecret belonging to the apikey, has to be a securestring. Required if not connected with Connect-HelloId.
.EXAMPLE
    
.EXAMPLE
    
.INPUTS
    
.OUTPUTS
    
#>

function New-HidGroup {
    [CmdletBinding(PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (    
        # the name of the group to create
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
        
        # Usernames to add to the group
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string[]]$UserNames = @(),
        
        # UserGuids to add to the group
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [guid[]]$UserGuids = @(),
        
        # Is the new group enabled or disabled.
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [bool]$Enabled = $true,
        
        # is the new group a default group
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [bool]$Default = $false,
        
        # is QR-code generation for group members enabled
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [bool]$QrEnabled = $false,
        
        # UserGuids to add to the group
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [guid]$ManagedByUserGuid,

        # Company name used in the URL
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$CompanyName,
        
        # Api key
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$ApiKey,

        # Api secret
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [securestring]$ApiSecret
    )
    
    begin {

        if ($PSBoundParameters.ContainsKey("CompanyName") -AND $PSBoundParameters.ContainsKey("ApiKey") -AND $PSBoundParameters.ContainsKey("ApiSecret") ){
            Write-Verbose -Message "Using connectioninfo and credentials from parameter"
            #Create credential object for authentication
            $Cred = New-Object System.Management.Automation.PSCredential ($ApiKey, $ApiSecret)
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }


        #Variables
        $JsonUserNames = ConvertTo-Json $UserNames -Depth 15
        $JsonUserGuids = ConvertTo-Json $UserGuids -Depth 15
        
    } #End begin
    
    process {
        
        $sbBody = [System.Text.StringBuilder]::new()
        $null = $SbBody.AppendLine("{") 
        $null = $SbBody.AppendLine("`"name`": `"$Name`",")
        $null = $SbBody.AppendLine("`"IsDefault`": $(($Default).ToString().ToLower()),")
        $null = $SbBody.AppendLine("`"IsQrEnabled`": $(($QrEnabled).ToString().ToLower()),")
        $null = $SbBody.AppendLine("`"UserNames`": `"$JsonUserNames`",")
        $null = $SbBody.AppendLine("`"UserGUIDs`": `"$JsonUserGuids`",")
        $null = $SbBody.AppendLine("`"ManagedByUserGuid`": `"$UserNames`",")
        if ($PSBoundParameters.ContainsKey("ManagedByUserGuid")) { $null = $SbBody.AppendLine("`"ManagedByUserGuid`": `"$ManagedByUserGuid`",") }
        $null = $SbBody.AppendLine("`"IsEnabled`": $(($Enabled).ToString().ToLower())")
        $null = $SbBody.AppendLine("}") 
        $URI = "https://$CompanyName.helloid.com/api/v1/groups"
        $output = Invoke-RestMethod -Uri $URI -Method "POST" -Headers $headers -Credential $Cred -UseBasicParsing -Body $sbBody.ToString()
        $output            
        
    } #End process
    
    end {
        
    } #End end
} #End function