Write-Verbose -Message "Prepare variables based on PSU server" -Verbose
switch($UAJob.ComputerName){
    "ITDWINAUTOT1" {
        $ServiceNowEnvironment = "Test"
    }
    "ITDWINAUTOP1" {
        $ServiceNowEnvironment = "Production"
    }
}
New-ITDServiceNowSession -Environment $ServiceNowEnvironment -Credential $Secret:snow_vmcred

<#
Write-Verbose -Message "Retrieve List of all Server Build Request request items from ServiceNow where closed_at is Yesterday and request_type is Change" -Verbose
$Filter = 'cat_item=c64e27af47244610b7853238436d435d^variables.3bf9fc3b47240a10b7853238436d430b=Change^closed_atONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()'
#######   'cat_item=c64e27af47244610b7853238436d435d^variables.3bf9fc3b47240a10b7853238436d430b=Change^closed_atONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()'
$CompletedRitms = Get-ITDServiceNowRecord -ItemType 'Request Item' -Filter $Filter

Write-Verbose -Message ("Found " + @($CompletedRitms).count + " completed Ritms.")
#>

$CompletedRitms = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number RITM0269022
ForEach($Ritm in $CompletedRITMs){
    $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number ($Ritm.number.value) -IncludeCustomVariable -IncludeVariableSet
    # loop each VM row
    ForEach($row in $Ritm.VariableSet){
        # resolve sys_id to host name
        $CmdbCi = Get-ITDServiceNowRecord -Table cmdb_ci -SysId $row.host_name_ref
        Write-Verbose -Message ("Start " + $Ritm.number.value + " server " + $row.host_name_ref + ', ' + $CmdbCi.fqdn.display_value) -Verbose

        # run solarwinds import
        $Func = {
            param ($c)
            Write-Verbose -Message "Attempting to import $c to Solarwinds"
            Import-SWDiscovery -ComputerName $c
        }
        Invoke-Command -ComputerName itdslrwnds.nd.gov -ScriptBlock $Func -ArgumentList $CmdbCi.fqdn.display_value -Credential $Secret:ndgov_svcitdiaasauto
    }
}