PowerSNOW.psm1

function Get-SNOWRestAPITask {
    <#
    .SYNOPSIS
        This script retrieves Task information from ServiceNow using the REST API.
 
    .NOTES
        Name: Get-SNOWRestAPITask
        Author: Bruce Stump
 
    .DESCRIPTION
        This script retrieves Task information from ServiceNow using the REST API. There are three
        parameters; $Credential (Credential that has access to ServiceNow Tables), $InstanceUrl
        (ServiceNow Instance URL), and $Query (REST Query that goes into the sysparm_query parameter)
        If you are unfamiliar with creating REST Queries, it is not a mandatory option. The default will
        get everything in the sys_task table for tha last 90 days. You can then filter in PowerShell.
        $Credential and $Instance are mandatory parameters.
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
        $query = "short_descriptionSTARTSWITHTermination"
 
        Get-SNOWRestAPITask -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
         
        $daysAgo = (Get-Date).AddDays(-90).ToString('yyyy-MM-dd')
        $query = "sys_created_on>=$daysAgo"
 
        Get-SNOWRestAPITask -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        Get-SNOWRestAPITask -Credential $cred -instanceUrl $InstanceUrl
    #>

    
    [CmdletBinding()]
    
    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential,
    
        [Parameter(Mandatory=$true)]
        [string]$InstanceUrl,
    
        [Parameter(Mandatory=$false)]
        [string]$Query
        )
    
    Begin {
        # Initialize Variables
        if ($Query -eq $null) {
            $daysAgo = (Get-Date).AddDays(-90).ToString('yyyy-MM-dd')
            $Query = "sys_created_on>=$daysAgo"
        }
      
        $SnowUser = $Credential.username
        $SnowPassword = $Credential.password
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SnowPassword)
        $PlainTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    
        $Table = "sc_task"
        $limit = 10000
        $offset = 0
    }
    
    Process {
        # Url to SNOW Task Table
        $ServiceNowUrl = "$InstanceUrl/api/now/table/$Table"
    
        # Encode Credentials
        $encodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$SnowUser`:$PlainTextPassword"))
    
        # Create the headers
        $headers = @{
            "Authorization" = "Basic $encodedCredentials"
            "Content-Type" = "application/json"
            "Accept" = "application/json"
        }
    
        # Construct final url
        $url = $ServiceNowUrl + '?sysparm_limit=' + $limit + '&sysparm_offset=' + $offset + '&sysparm_query=' + $Query
    
        # Invoke REST
        $response = Invoke-RestMethod -Uri "$url" -Headers $headers -Method Get

        $response.result
    }
    
    End {}
}

function Get-SNOWRestAPIIncident {
    <#
    .SYNOPSIS
        This script retrieves Incident information from ServiceNow using the REST API.
 
    .NOTES
        Name: Get-SNOWRestAPIIncident
        Author: Bruce Stump
 
    .DESCRIPTION
        This script retrieves Task information from ServiceNow using the REST API. There are three
        parameters; $Credential (Credential that has access to ServiceNow Tables), $InstanceUrl
        (ServiceNow Instance URL), and $Query (REST Query that goes into the sysparm_query parameter)
        If you are unfamiliar with creating REST Queries, it is not a mandatory option. The default will
        get everything in the incident table for tha last 90 days. You can then filter in PowerShell.
        $Credential and $Instance are mandatory parameters.
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
        $query = "short_descriptionSTARTSWITHTermination"
 
        Get-SNOWRestAPIIncident -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        $daysAgo = (Get-Date).AddDays(-90).ToString('yyyy-MM-dd')
        $query = "sys_created_on>=$daysAgo"
 
        Get-SNOWRestAPIIncident -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        Get-SNOWRestAPIIncident -Credential $cred -instanceUrl $InstanceUrl
    #>

    
    [CmdletBinding()]
    
    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential,
    
        [Parameter(Mandatory=$true)]
        [string]$InstanceUrl,
    
        [Parameter(Mandatory=$false)]
        [string]$Query
        )
    
    Begin {
        # Initialize Variables
        if ($Query -eq $null) {
            $daysAgo = (Get-Date).AddDays(-90).ToString('yyyy-MM-dd')
            $Query = "sys_created_on>=$daysAgo"
        }
      
        $SnowUser = $Credential.username
        $SnowPassword = $Credential.password
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SnowPassword)
        $PlainTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    
        $Table = "incident"
        $limit = 10000
        $offset = 0
    }
    
    Process {
        # Url to SNOW Task Table
        $ServiceNowUrl = "$InstanceUrl/api/now/table/$Table"
    
        # Encode Credentials
        $encodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$SnowUser`:$PlainTextPassword"))
    
        # Create the headers
        $headers = @{
            "Authorization" = "Basic $encodedCredentials"
            "Content-Type" = "application/json"
            "Accept" = "application/json"
        }
    
        # Construct final url
        $url = $ServiceNowUrl + '?sysparm_limit=' + $limit + '&sysparm_offset=' + $offset + '&sysparm_query=' + $Query
    
        # Invoke REST
        $response = Invoke-RestMethod -Uri "$url" -Headers $headers -Method Get

        $response.result
    }
    
    End {}
}

function Get-SNOWRestAPIChangeRequest {
    <#
    .SYNOPSIS
        This script retrieves Change Request information from ServiceNow using the REST API.
 
    .NOTES
        Name: Get-SNOWRestAPIChangeRequest
        Author: Bruce Stump
 
    .DESCRIPTION
        This script retrieves Change Request information from ServiceNow using the REST API. There are three
        parameters; $Credential (Credential that has access to ServiceNow Tables), $InstanceUrl
        (ServiceNow Instance URL), and $Query (REST Query that goes into the sysparm_query parameter)
        If you are unfamiliar with creating REST Queries, it is not a mandatory option. The default will
        get everything in the change request table for tha last 90 days. You can then filter in PowerShell.
        $Credential and $Instance are mandatory parameters.
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
        $query = "short_descriptionSTARTSWITHTermination"
 
        Get-SNOWRestAPIChangeRequest -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        $daysAgo = (Get-Date).AddDays(-90).ToString('yyyy-MM-dd')
        $query = "sys_created_on>=$daysAgo"
 
        Get-SNOWRestAPIChangeRequest -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        Get-SNOWRestAPIChangeRequest -Credential $cred -instanceUrl $InstanceUrl
    #>

    
    [CmdletBinding()]
    
    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential,
    
        [Parameter(Mandatory=$true)]
        [string]$InstanceUrl,
    
        [Parameter(Mandatory=$false)]
        [string]$Query
        )
    
    Begin {
        # Initialize Variables
        if ($Query -eq $null) {
            $daysAgo = (Get-Date).AddDays(-90).ToString('yyyy-MM-dd')
            $Query = "sys_created_on>=$daysAgo"
        }
      
        $SnowUser = $Credential.username
        $SnowPassword = $Credential.password
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SnowPassword)
        $PlainTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    
        $Table = "change_request"
        $limit = 10000
        $offset = 0
    }
    
    Process {
        # Url to SNOW Task Table
        $ServiceNowUrl = "$InstanceUrl/api/now/table/$Table"
    
        # Encode Credentials
        $encodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$SnowUser`:$PlainTextPassword"))
    
        # Create the headers
        $headers = @{
            "Authorization" = "Basic $encodedCredentials"
            "Content-Type" = "application/json"
            "Accept" = "application/json"
        }
    
        # Construct final url
        $url = $ServiceNowUrl + '?sysparm_limit=' + $limit + '&sysparm_offset=' + $offset + '&sysparm_query=' + $Query
    
        # Invoke REST
        $response = Invoke-RestMethod -Uri "$url" -Headers $headers -Method Get

        $response.result
    }
    
    End {}
}

function Get-SNOWRestAPIAttachment {
    <#
    .SYNOPSIS
        This script retrieves Attachments from ServiceNow Tickets using the REST API.
 
    .NOTES
        Name: Get-SNOWRestAPIAttachment
        Author: Bruce Stump
 
    .DESCRIPTION
        This script retrieves Attachments from ServiceNow Tickets using the REST API. There are four
        parameters; $Credential (Credential that has access to ServiceNow Tables), $InstanceUrl
        (ServiceNow Instance URL), $Ticket (Ticket object which you retrieve from ServiceNow using the
        PowerSNOW modules for retrieving ticket information) and $Path (Location that you want to save
        the attachments) All parameters are mandatory, see examples for more information. This module
        only accepts one ticket object at a time (-Ticket), if you want to streamline multiple tickets,
        you will need to put this module in a loop.
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
        $query = "short_descriptionSTARTSWITHTermination"
 
        $ticketObject = Get-SNOWRestAPITask -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
        Get-SNOWRestAPIAttachment -Credential $cred -InstanceUrl $InstanceUrl -Ticket $ticketObject -Path C:\Admin
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        $daysAgo = (Get-Date).AddDays(-90).ToString('yyyy-MM-dd')
        $query = "sys_created_on>=$daysAgo"
 
        $ticketObject = Get-SNOWRestAPIChangeRequest -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
        Get-SNOWRestAPIAttachment -Credential $cred -InstanceUrl $InstanceUrl -Ticket $ticketObject -Path C:\Admin
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        $daysAgo = (Get-Date).AddDays(-90).ToString('yyyy-MM-dd')
        $query = "sys_created_on>=$daysAgo"
 
        $ticketObject = Get-SNOWRestAPIIncident -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
        Get-SNOWRestAPIAttachment -Credential $cred -InstanceUrl $InstanceUrl -Ticket $ticketObject -Path C:\Admin
    #>

    
    [CmdletBinding()]
    
    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential,
    
        [Parameter(Mandatory=$true)]
        [string]$InstanceUrl,

        [Parameter(Mandatory=$true)]
        [PSCustomObject]$Ticket,

        [Parameter(Mandatory=$true)]
        [ValidateScript({
            if(Test-Path $_ -PathType Container) {
                return $true
            }
            elseif(Test-Path $_ -PathType Leaf) {
                throw 'The Path parameter must be a folder. File paths are not allowed.'
            }
            throw 'Invalid File Path'
        })]
        [System.IO.FileInfo]$Path
        )
    
    Begin {

        $SnowUser = $Credential.username
        $SnowPassword = $Credential.password
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SnowPassword)
        $PlainTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    
        $Table = "sys_attachment"
    }
    
    Process {

        if ($Ticket.count -ne 1) {
            Throw "Ticket parameter needs to be a ticket of 1, Number Tickets is " + $Ticket.count
        }

        # Url to SNOW Attacment Table
        $ServiceNowUrl = "$InstanceUrl/api/now/table/$Table"
    
        #Url to SNOW Attachment File
        $ServiceNowAttachmentFile = "$InstanceUrl/api/now/attachment"

        # Encode Credentials
        $encodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$SnowUser`:$PlainTextPassword"))
    
        # Create the headers
        $headers = @{
            "Authorization" = "Basic $encodedCredentials"
            "Content-Type" = "application/json"
            "Accept" = "application/json"
        }
    
        # Construct final url
        $attachmentUrl = $ServiceNowUrl + '?sysparm_query=table_sys_id=' + $Ticket.sys_id
        $attachment = Invoke-RestMethod -Uri "$attachmentUrl" -Headers $headers -Method Get

        $attachmentSysID = @()
        $attachmentFileName = @()

        if (($attachment.result).count -eq 0) {
            Throw "There are no attachments for this ticket"
        }
        
        ($attachment.result).foreach({
            $attachmentSysID += $_.sys_id
            $attachmentFileName += $_.file_name
        }) 

        for ($i = 0;$i -le $attachmentSysID.count - 1;$i++){
            $attachmentUrl = $ServiceNowAttachmentFile + '/' + $attachmentSysID[$i] + '/file'
            $attachmentFile = $attachmentFileName[$i]
            Invoke-RestMethod -Uri "$attachmentUrl" -Headers $headers -Method Get -OutFile "$Path\$attachmentFile"
        }
    }
    
    End {}
}

function Get-SNOWRestAPIGroup {
    <#
    .SYNOPSIS
        This script retrieves User Group Info from ServiceNow using the REST API.
 
    .NOTES
        Name: Get-SNOWRestAPIGroup
        Author: Bruce Stump
 
    .DESCRIPTION
        This script retrieves Group info from ServiceNow using the REST API. There are three
        parameters; $Credential (Credential that has access to ServiceNow Tables), $InstanceUrl
        (ServiceNow Instance URL), and $Query (REST Query that goes into the sysparm_query parameter)
        If you are unfamiliar with creating REST Queries, it is not a mandatory option. The default will
        get everything. You can then filter in PowerShell. Only $Credential and $InstanceUrl parameters
        are mandatory.
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
        $query = "nameSTARTSWITHProject"
 
        Get-SNOWRestAPIGroup -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        Get-SNOWRestAPIGroup -Credential $cred -InstanceUrl $InstanceUrl
    #>

    
    [CmdletBinding()]
    
    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential,
    
        [Parameter(Mandatory=$true)]
        [string]$InstanceUrl,

        [Parameter(Mandatory=$false)]
        [string]$Query = ""
    )

    Begin {

        $SnowUser = $Credential.username
        $SnowPassword = $Credential.password
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SnowPassword)
        $PlainTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    
        $Table = "sys_user_group"
        $limit = 10000
        $offset = 0
    }
    
    Process {

        # Url to SNOW Task Table
        $ServiceNowUrl = "$InstanceUrl/api/now/table/$Table"
    
        # Encode Credentials
        $encodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$SnowUser`:$PlainTextPassword"))
    
        # Create the headers
        $headers = @{
            "Authorization" = "Basic $encodedCredentials"
            "Content-Type" = "application/json"
            "Accept" = "application/json"
        }
    
        # Construct final url
        $url = $ServiceNowUrl + '?sysparm_limit=' + $limit + '&sysparm_offset=' + $offset + '&sysparm_query=' + $Query
    
        # Invoke REST
        $response = Invoke-RestMethod -Uri "$url" -Headers $headers -Method Get

        $response.result
    }
    
    End {}
}

function Get-SNOWRestAPIUser {
    <#
    .SYNOPSIS
        This script retrieves User Info from ServiceNow using the REST API.
 
    .NOTES
        Name: Get-SNOWRestAPIUser
        Author: Bruce Stump
 
    .DESCRIPTION
        This script retrieves User info from ServiceNow using the REST API. There are three
        parameters; $Credential (Credential that has access to ServiceNow Tables), $InstanceUrl
        (ServiceNow Instance URL), and $Query (REST Query that goes into the sysparm_query parameter)
        If you are unfamiliar with creating REST Queries, it is not a mandatory option. The default will
        get everything. You can then filter in PowerShell. Only $Credential and $InstanceUrl parameters
        are mandatory.
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
        $query = "nameSTARTSWITHProject"
 
        Get-SNOWRestAPIUser -Credential $cred -InstanceUrl $InstanceUrl -Query $query
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        Get-SNOWRestAPIUser -Credential $cred -InstanceUrl $InstanceUrl
    #>

    
    [CmdletBinding()]
    
    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential,
    
        [Parameter(Mandatory=$true)]
        [string]$InstanceUrl,

        [Parameter(Mandatory=$false)]
        [string]$Query = ""
    )

    Begin {

        $SnowUser = $Credential.username
        $SnowPassword = $Credential.password
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SnowPassword)
        $PlainTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    
        $Table = "sys_user"
        $limit = 10000
        $offset = 0
    }
    
    Process {

        # Url to SNOW Task Table
        $ServiceNowUrl = "$InstanceUrl/api/now/table/$Table"
    
        # Encode Credentials
        $encodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$SnowUser`:$PlainTextPassword"))
    
        # Create the headers
        $headers = @{
            "Authorization" = "Basic $encodedCredentials"
            "Content-Type" = "application/json"
            "Accept" = "application/json"
        }
    
        # Construct final url
        $url = $ServiceNowUrl + '?sysparm_limit=' + $limit + '&sysparm_offset=' + $offset + '&sysparm_query=' + $Query
    
        # Invoke REST
        $response = Invoke-RestMethod -Uri "$url" -Headers $headers -Method Get

        $response.result
    }
    
    End {}
}

function New-SNOWRestAPIIncident {
    <#
    .SYNOPSIS
        This script creates new Incidents in ServiceNow using the REST API.
 
    .NOTES
        Name: New-SNOWRestAPIIncident
        Author: Bruce Stump
 
    .DESCRIPTION
        This script creates Incidents in ServiceNow using the REST API. There are three
        parameters; $Credential (Credential that has access to ServiceNow Tables), $InstanceUrl
        (ServiceNow Instance URL), and various parameters that match the fields of the Incident
        table. The only parameter of the Incident table that is required in the short_description
        field, which makes a total of three mandatory parameters; -Credential, -InstanceUrl, and
        -short_description. See parameters for which ServiceNow fields this module will accept.
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        New-SNOWRestAPIIncident -Credential $cred -InstanceUrl $InstanceUrl -short_description "Test"
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        $sysID = Get-SNOWRestAPIGroup -Credential $cred -InstanceUrl $InstanceUrl | ? {$_.Name -eq "Test Name"} | Select -ExpandProperty sys_id
         
        New-SNOWRestAPIIncident -Credential $cred -InstanceUrl $InstanceUrl -short_description "Test" -assignment_group $sysID
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
     
        $sysID = Get-SNOWRestAPIGroup -Credential $cred -InstanceUrl $InstanceUrl | ? {$_.Name -eq "Test Name"} | Select -ExpandProperty sys_id
 
        $Arguments = @{
            Credential = $cred
            InstanceUrl = $InstanceUrl
            short_description = "Testing"
            assignment_group = $sysID
        }
 
        New-SNOWRestAPIIncident @Arguments
    #>

    
    [CmdletBinding()]
    
    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential,
    
        [Parameter(Mandatory=$true)]
        [string]$InstanceUrl,

        [Parameter(Mandatory=$true)]
        [string]$short_description,

        [Parameter(Mandatory=$false)]
        [PSCustomObject]$assignment_group
        )
    
    Begin {
        # Initialize Variables
        $SnowUser = $Credential.username
        $SnowPassword = $Credential.password
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SnowPassword)
        $PlainTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    
        $Table = "incident"
    }
    
    Process {
        # Url to SNOW Task Table
        $ServiceNowUrl = "$InstanceUrl/api/now/table/$Table"
    
        # Encode Credentials
        $encodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$SnowUser`:$PlainTextPassword"))
    
        # Create the headers
        $headers = @{
            "Authorization" = "Basic $encodedCredentials"
            "Content-Type" = "application/json"
            "Accept" = "application/json"
        }
    
        $body = @{
            short_description = $short_description
            assignment_group = $assignment_group
        } | ConverTo-Json

        $incident = Invoke-RestMethod -Uri $ServiceNowUrl -Method Post -Headers $headers -Body $body -Credential $Credential

        $incident.result
    }
    
    End {}
}

function Get-SNOWRestAPIGroupMember {
    <#
    .SYNOPSIS
        This script retrieves Group Member information from ServiceNow using the REST API.
 
    .NOTES
        Name: Get-SNOWRestAPIGroupMember
        Author: Bruce Stump
 
    .DESCRIPTION
        This script retrieves Group Member info from ServiceNow using the REST API. There are two
        parameters; $Credential (Credential that has access to ServiceNow Tables), $InstanceUrl
        (ServiceNow Instance URL). The parameters are mandatory. This module gets the mapping between
        a user and the group that the person belongs to.
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        Get-SNOWRestAPIUser -Credential $cred -InstanceUrl $InstanceUrl
    #>

    
    [CmdletBinding()]
    
    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential,
    
        [Parameter(Mandatory=$true)]
        [string]$InstanceUrl
    )

    Begin {

        $SnowUser = $Credential.username
        $SnowPassword = $Credential.password
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SnowPassword)
        $PlainTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

        $Table = "sys_user_grmember"
    }
    
    Process {

        # Url to SNOW Task Table
        $ServiceNowUrl = "$InstanceUrl/api/now/table/$Table"
    
        # Encode Credentials
        $encodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$SnowUser`:$PlainTextPassword"))
    
        # Create the headers
        $headers = @{
            "Authorization" = "Basic $encodedCredentials"
            "Content-Type" = "application/json"
            "Accept" = "application/json"
        }

        $Query = '?sysparm_fields=sys_id,user.name,group.name'

        # Construct final url
        $url = $ServiceNowUrl + $Query
    
        # Invoke REST
        $response = Invoke-RestMethod -Uri "$url" -Headers $headers -Method Get

        $response.result
    }
    
    End {}
}

function Get-SNOWRestAPIGroupType {
    <#
    .SYNOPSIS
        This script retrieves Group Type information from ServiceNow using the REST API.
 
    .NOTES
        Name: Get-SNOWRestAPIGroupType
        Author: Bruce Stump
 
    .DESCRIPTION
        This script retrieves Group Member info from ServiceNow using the REST API. There are two
        parameters; $Credential (Credential that has access to ServiceNow Tables), $InstanceUrl
        (ServiceNow Instance URL). The parameters are mandatory.
 
    .EXAMPLE
        $cred = Get-Credential
        $InstanceUrl = "https://CompanyName.service-now.com"
 
        Get-SNOWRestAPIGroupType -Credential $cred -InstanceUrl $InstanceUrl
    #>

    
    [CmdletBinding()]
    
    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential,
    
        [Parameter(Mandatory=$true)]
        [string]$InstanceUrl
    )

    Begin {

        $SnowUser = $Credential.username
        $SnowPassword = $Credential.password
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SnowPassword)
        $PlainTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

        $Table = "sys_user_group_type"
    }
    
    Process {

        # Url to SNOW Task Table
        $ServiceNowUrl = "$InstanceUrl/api/now/table/$Table"
    
        # Encode Credentials
        $encodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$SnowUser`:$PlainTextPassword"))
    
        # Create the headers
        $headers = @{
            "Authorization" = "Basic $encodedCredentials"
            "Content-Type" = "application/json"
            "Accept" = "application/json"
        }

        $Query = '?sysparm_fields=sys_id,name'

        # Construct final url
        $url = $ServiceNowUrl + $Query
    
        # Invoke REST
        $response = Invoke-RestMethod -Uri "$url" -Headers $headers -Method Get

        $response.result
    }
    
    End {}
}