$Url = "https://northdakotatest.service-now.com"
$HeaderAuth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Secret:SNowVMCred.UserName, $Secret:SnowVMCred.GetNetworkCredential().Password)))
$SNOWSessionHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$SNOWSessionHeader.Add('Authorization', ('Basic {0}' -f $HeaderAuth))
$SNOWSessionHeader.Add('Accept', 'application/json')
$Type = "application/json"


$PSUJobId = 123456
$DomainName = 'nd.gov'
$RequestedBy = 'zmeier@nd.gov'
$StartDateTime = Get-Date

New-ITDServiceNowSession -Environment Test -Credential $Secret:SNowVMCred
Get-ITDServiceNowSession
Write-Verbose -Message "Prep variables" -Verbose

        $TemplateName = 'NDIT-SPS-Server Add/Chg/Del'
        $RequestedByUsername = 'khellman'
        $Category = 'Systems Platforms - Systems'
        $ShortDescription = 'test'
        $Description = 'test'
        $Priority = 3
        $Impact = 3
        $Justification = 'justification'
        $Implementation = 'implementation'
        $RiskImpactAnalysis = 'riskimpactanalysis'
        $BackoutPlan = 'backoutplan'
        $TestPlan = 'testplan'
        $WhoIsImpacted = 'whoimpacted'
        $ChangeManagerUsername = 'khellman'
        $ChangeCoordinatorUsername = 'khellman'
        $AssignmentGroup = 'NDIT-Computer Systems Windows'
        $AssignedToUsername = 'khellman'
        $StartTime = Get-Date
        $EndTime = $StartTime.AddMinutes(1)
    
    $ChgTemplateStd = Get-ITDServiceNowChangeTemplateStandard -Name $TemplateName
    $ChgTemplateStdSysId = $ChgTemplateStd.sys_id.value
    Write-Warning -Message ("ChgTemplateStdSysId = " + $ChgTemplateStdSysId)

         Write-Verbose -Message "Start NewRecord" -Verbose
        [PSCustomObject]$NewRecord = @{
            category             = $Category;
            u_subcategory        = $Subcategory
            impact               = $Impact;
            urgency              = $Urgency;
            short_description    = $ShortDescription;
            description          = $Description;
            justification        = $Justification;
            implementation_plan  = $Implementation;
            risk_impact_analysis = $RiskImpactAnalysis;
            backout_plan         = $BackoutPlan;
            test_plan            = $TestPlan
            u_who_is_impacted    = $WhoIsImpacted;
            start_date           = (Get-Date -Date $StartTime -AsUTC).ToString('yyyy-MM-dd HH:mm:ss')
            end_date             = (Get-Date -Date $EndTime -AsUTC).ToString('yyyy-MM-dd HH:mm:ss')
        }

        Write-Verbose -Message "Start RequestedBy" -Verbose
        If ($RequestedBy) {
            $ReqBy = Get-ITDServiceNowUser -Username $RequestedBy
            If (@($ReqBy).count -gt 1) {
                Write-Error "Multiple requested users found, creation failed." -ErrorAction Stop
            }
            Else {
                $NewRecord += @{requested_by = $ReqBy.sys_id }
            }
        }

Write-Verbose -Message "Start AssignmentGroup" -Verbose
        If ($AssignmentGroup) {
            $AssGroup = Get-ITDServiceNowUserGroup -Name $AssignmentGroup
            If (@($AssGroup).count -gt 1) {
                Write-Error "Multiple assignment groups found, creation failed." -ErrorAction Stop
            }
            Else {
                $NewRecord += @{assignment_group = $AssGroup.sys_id }
            }
        }

Write-Verbose -Message "Start ChgManagerName" -Verbose
        $ChgManagerUsername = Get-ITDServiceNowUser -Username $ChangeManagerUsername
        If (@($ChgManagerUsername).count -gt 1) {
            Write-Error "Multiple users found for ChangeManagerUsername, creation failed." -ErrorAction Stop
        }
        Else {
            $NewRecord += @{u_change_manager = $ChgManagerUsername.sys_id }
        }

Write-Verbose -Message "Start ChgCoordName" -Verbose
        $ChgCoordUsername = Get-ITDServiceNowUser -Username $ChangeCoordinatorUsername
        If (@($ChgCoordUsername).count -gt 1) {
            Write-Error "Multiple users found for ChangeCoordinator, creation failed." -ErrorAction Stop
        }
        Else {
            $NewRecord += @{u_change_coordinator = $ChgCoordUsername.sys_id }
        }

Write-Verbose -Message "Start AssignedTousername" -Verbose
        If ($AssignedToUsername) {
            $AssTo = Get-ITDServiceNowUser -Username $AssignedToUsername
            If (@($AssTo).count -gt 1) {
                Write-Error "Multiple assignment users found, incident creation failed." -ErrorAction Step
            }
            Else {
                $NewRecord += @{assigned_to = $AssTo.sys_id }
            }
        } 
$Uri = ($Url + "/api/sn_chg_rest/change/standard/$ChgTemplateStdSysId")
        Write-Verbose -Message "Standard CHG Template SysId = $Uri" -Verbose
        $InvokeRestMethodParams = @{
            Method      = 'Post';
            Uri         = $Uri;
            Body        = $NewRecord | ConvertTo-Json;
            Headers     = $SnowSessionHeader
            ContentType = "application/json"
        }

        #Write-Output $InvokeRestMethodParams
        $result = (Invoke-RestMethod @InvokeRestMethodParams).result
    
        Write-Output $result   
    #>
