
# property expressions for only custom attributes
$PropertyList = @(
    @{
        name       = "name";
        expression = { $_.name.display_value };
    },
    @{
        name       = "dns_domain";
        expression = { $_.dns_domain.display_value } ;
    },
    @{
        name       = "environment";
        expression = { $_.environment.display_value } ;
    },
    @{
        name       = 'operational_status';
        expression = { $_.operational_status.display_value };
    }
    @{
        name       = 'sys_id';
        expression = { $_.sys_id.value };
    }
    @{
        name       = 'sys_class_name';
        expression = { $_.sys_class_name.value } 
    },
    @{
        name       = "u_nd_type";
        expression = { $_.u_nd_type.display_value }
    },
    @{
        name       = "u_nd_dr_protection";
        expression = { $_.u_nd_dr_protection.display_value };
    },
    @{
        name       = "u_nd_licensing_restrictions";
        expression = { $_.u_nd_licensing_restrictions.display_value }
    },
    @{
        name       = "u_nd_application_svc";
        expression = { $_.u_nd_application_svc.display_value }
    },
    @{
        name       = "u_support_hours";
        expression = { $_.u_support_hours.display_value }
    },
    @{
        name       = "u_srm_recovery_type";
        expression = { $_.u_srm_recovery_type.display_value }
    }
)

# Get all Ci properties and sort them alphabetically
# $Ci | Select-Object ([string[]]($Ci | Get-Member -MemberType NoteProperty | ForEach-Object{$_.Name} | Sort-Object))

$SharePointList = Get-ITDVMwareSharePointVMGuestList | Where-Object { $_.Status -ne 'Delete' -and $_.Status -ne 'Deleted' } | Sort-Object Title

# sync sharepoint to servicenow cmdb
ForEach ($SPItem in $SharePointList) {
    try {
        Write-Verbose -Message ("Start " + $SPItem.Title) -Verbose
        $HostName = $SPItem.Title.split('.')[0]
        $Ci = Get-ITDServiceNowRecord -Table cmdb_ci_server -Filter "name=$HostName" | Select-Object -Property $PropertyList
    
        #If ($null -eq $Ci.u_nd_dr_protection) {
            switch ($SPItem.DR_Protection) {
                'None' { $DRProtection = 'No DR' }
                'VMware: ABR' { $DRProtection = 'VMWare: ABR' }
                'VMware: RPO 0:15' { $DRProtection = 'VMWARE RPO: 0:15' }
                'VMware: RPO 0:30' { $DRProtection = 'VMWARE RPO: 0:30' }
                'VMware: RPO 1:00' { $DRProtection = 'VMWARE RPO: 1:00' }
                'VMware: RPO 2:00' { $DRProtection = 'VMWARE RPO: 2:00' }
                'VMware: RPO 4:00' { $DRProtection = 'VMWARE RPO: 4:00' }
                'VMware: RPO 8:00' { $DRProtection = 'VMWARE RPO: 8:00' }
            }
            If ($Ci.u_nd_dr_protection -ne $DRProtection) {
                Write-Verbose -Message ($Ci.Name + " dr_protection") -Verbose
                Update-ITDServiceNowRecord -Table cmdb_ci_server -SysId $Ci.sys_id -Values @{
                    u_nd_dr_protection = $DRProtection;
                } | Out-Null
            }
        #
    
        If ($null -eq $Ci.u_nd_licensing_restrictions) {
            Write-Verbose -Message ($Ci.Name + " licensing restrictions") -Verbose
            Update-ITDServiceNowRecord -Table cmdb_ci_server -SysId $Ci.sys_id -Values @{
                u_nd_licensing_restrictions = $SPItem.LicensingRestrictions;
            }
        }
    
        
        If ($null -eq $SPItem.SRM_RecoveryVMtype) {
            # do nothing
        }
        Else {
            
            switch ($SPItem.SRM_RecoveryVMtype) {
                'Reserved' { $SRMRecoveryType = 'Reserved' }
                'Repurposed' { $SRMRecoveryType = 'Repurposed' }
            }
            
            If ($Ci.u_srm_recovery_type -ne $SRMRecoveryType) {
                Write-Verbose -Message ($Ci.Name + " srm recovery type") -Verbose
                Update-ITDServiceNowRecord -Table cmdb_ci_server -SysId $Ci.sys_id -Values @{
                    u_srm_recovery_type = $SRMRecoveryType;
                }
            }
            $SRMRecoveryType = $null
        }
        
        If ($null -eq $Ci.u_support_hours) {
            Write-Verbose -Message ($Ci.Name + " support hours") -Verbose
            switch ($SPitem.Support_x0020_HoursId) {
                1 {
                    $SupportHours = "All Day Every Day" ;
                }
                4 {
                    $SupportHours = "All Week 500 to 2300";
                }
                11 {
                    $SupportHours = "Weekdays 700 to 1800";
                }
            }
            Write-Verbose -Message ($Ci.name + $SupportHours)
            Update-ITDServiceNowRecord -Table cmdb_ci_server -SysId $Ci.sys_id -Values @{
                u_support_hours = $SupportHours
            } | Out-Null
            $SupportHours = $null
        }
    }

    catch {
        $Hostname | Add-Content "C:\temp\syncerrors.txt"
    }
    Write-Verbose -Message ("End   " + $SPItem.Title) -Verbose
}


Get-ITDServiceNowRecord -Table cmdb_ci_server | Select-Object Name, dns_domain


Get-ITDServiceNowRecord -Table cmdb_ci_server -Filter "name=itdnet35p1" | Select-Object $PropertyList





$Filter = '&$select=ID,Title,Status,Network/Vlan_Id,Network/CIDR,AppName/Title,Cluster/Name,Support_x0020_Hours/Hours&$expand=Network/Id,AppName/Id,Cluster/Id,Support_x0020_Hours/Id'
$URLVMItems = "https://share.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VM Guests')/items?" + '$top=10000' + $Filter




$Filter = '&$select=ID,Title,Support_x0020_Hours/Hours&$expand=Support_x0020_Hours/Id'
$URLVMItems = "https://share.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VM Guests')/items?" + '$top=10000'

$InvokeWebRequestParams = @{
    Uri             = $URLVMItems;
    Method          = "Get";
    headers         = @{ "Accept" = "application/json;odata=verbose" };
    UseBasicParsing = $true;
}

If ($Credential) { $InvokeWebRequestParams += @{Credential = $Credential } }
Else { $InvokeWebRequestParams += @{UseDefaultCredentials = $true } }

#$z = (Invoke-WebRequest -Uri $URL -Method Get -UseDefaultCredentials -headers @{ "Accept" = "application/json;odata=verbose" }) -creplace '"Id":', '"Idx":' | ConvertFrom-Json
$z = (Invoke-WebRequest @InvokeWebRequestParams) -creplace '"Id":', '"Idx":' | ConvertFrom-Json

