[CmdletBinding()]
param (
    [string]
    $SamAccountName,

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

    [Parameter(Mandatory = $true)]
    [ValidateSet('VMware_Systems', 'CSRC', 'Shared Linux Password List', 'Peoplesoft Share PW', 'Cohesity', 'VDI', 'Office365')]
    [string]
    $PasswordstateList,

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

    [switch]
    $Quiet
)

$StartTime = Get-Date

$NewITDADServiceAccountParams = @{
    SamAccountName     = $SamAccountName;
    Description        = $Description;
    PasswordstateList  = $PasswordstateList;
    PasswordstateTitle = $PasswordstateTitle;
    Credential         = $Secret:svcitdiaasauto;
}
try {
    New-ITDADServiceAccount @NewITDADServiceAccountParams -Verbose
}
catch {
    Write-Error -Message $error[0] -ErrorAction Stop
}


$EndTime = Get-Date
If ($PSBoundParameters.ContainsKey('Quiet') -and $Quiet -eq $true) {
    Write-Verbose -Message "Quiet mode enabled.  No ServiceNow interactions will be done." -Verbose
}
Else {
    Write-Verbose -Message "Quiet mode disabled.  ServiceNow CHG will be generated." -Verbose
    # create std chg and close it
    New-ITDServiceNowSession Test -Credential $Secret:SNowVMCred
    $NewITDServiceNowChangeRequestParams = @{
        TemplateName              = 'NDIT-SPS-Server Add/Chg/Del'
        RequestedByUsername       = 'zmeier';
        Category                  = 'Systems Platforms - Systems';
        Subcategory               = 'Windows';
        Impact                    = 3;
        ShortDescription          = "New nd.gov Active Directory service account created - $UAJobId";
        Description               = "New nd.gov Active Directory service account created";
        Justification             = "New nd.gov Active Directory service account required for zero-trust policies";
        Implementation            = "PSUniversal execution";
        RiskImpactAnalysis        = "Low";
        BackoutPlan               = "Delete the new user account"
        TestPlan                  = "n/a"
        WhoIsImpacted             = "Windows System Administrators";
        StartTime                 = $StartTime
        EndTime                   = $EndTime;
        AssignmentGroup           = 'NDIT-Computer Systems Windows';
        ChangeManagerUsername     = 'khellman';
        ChangeCoordinatorUsername = 'gpgolberg';
        AssignedToUsername        = 'zmeier';
    }

    $CHG = New-ITDServiceNowChangeRequest @NewITDServiceNowChangeRequestParams -Verbose

    Update-ITDServiceNowRecord -ItemType "Change Request" -Number $CHG.Number.Value -Values @{
        work_notes = $Notes;
    }

    Complete-ITDServiceNowChangeRequest -Number $CHG.Number.value -CloseCode "Successful" -CloseNotes "New nd.gov Active Directory account ndgov\$SamAccountName created." -Verbose
}

