New-UDPage -Url "/CMDBServerList" -Name "CMDBServerList" -Content {
# Use Get-UDPage -Name 'CmdbServer' to use this page in your dashboard

<#
    New-UDCard -Content {
        New-UDList -children {
            New-UDListItem -Label "Enter the Name of the Cmdb Ci.  Usually host name, not FQDN.  `nFor example:  itdsccmp2.nd.gov, enter itdsccmp2.  The search is using 'LIKE' comparsion operator, so wildcards are automatically applied at both sides of the entered value.  "
            New-UDListItem -Label 'This search is a live query of the ServiceNow Cmdb.'
            New-UDListItem -Label 'This is a work in progress.  Use at your own discretion.'
        }
    }#>




New-UDForm -Id 'CiLookup' -Content {
    New-UDRow -Columns {
        New-UDColumn -SmallSize 6 -LargeSize 6 -Content {
            New-UDTextbox -Id 'CiName'
        }
    }          
} -OnValidate {
    $FormContent = $EventData
    if ($FormContent.CiName -eq $null -or $FormContent.VMName -eq '') {
        New-UDFormValidationResult -ValidationError "CiName is required"
    }
    else {
        New-UDFormValidationResult -Valid
    }
} -OnSubmit {
    Show-UDToast -Message ("Querying ServiceNow table cmdb_ci_server for record with name " + $EventData.Name) -Duration 20000

    New-ITDServiceNowSession -Environment Production -Credential $Secret:snow_vmcred
    $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_application_svc";
            expression = { $_.u_nd_application_svc.display_value }
        },
        @{
            name       = "u_nd_licensing_restrictions";
            expression = { $_.u_nd_licensing_restrictions.display_value }
        },
        @{
            name       = "u_support_hours";
            expression = { $_.u_support_hours.display_value }
        },
        @{
            name       = "u_nd_dr_protection";
            expression = { $_.u_nd_dr_protection.display_value };
        },
        @{
            name       = "u_srm_recovery_type";
            expression = { $_.u_srm_recovery_type.display_value }
        },
        @{
            name       = "os";
            expression = { $_.os.display_value }
        },
        @{
            name       = "os_version";
            expression = { $_.os_version.display_value }
        },
        @{
            name       = "model_id";
            expression = { $_.model_id.display_value }
        }
    )
    #$Result = Get-ITDServiceNowRecord -Table cmdb_ci_server -Filter ("nameLIKE" + $EventData.CiName) -IncludeTotalCount | Select-Object -Property $PropertyList
    $GetITDServiceNowRecordParams = @{
            #Filter            = "model_id=3161ee6c1bf810502653997fbd4bcb4c^ORmodel_id%253Da75f244c1bd75014bba0113fad4bcb72"
            Filter            = "model_idLIKEVMware^ORmodel_idLIKEMicrosoft"
            Table             = 'cmdb_ci_server';
            IncludeTotalCount = $true;
            Fields            = @(
                'name',
                'sys_id',
                'dns_domain',
                'sys_class_name',
                'operational_status',
                'environment',
                'u_nd_type',
                'model_id',
                'os',
                'os_version',
                'u_nd_application_svc',
                'u_nd_licensing_restrictions',
                'u_nd_dr_protection',
                'u_srm_recovery_type',
                'u_support_hours'
            )
        }
    $Result = Get-ITDServiceNowRecord @GetITDServiceNowRecordParams | Select-Object -Property $PropertyList
    $CmdbCiUrl = "https://northdakota.service-now.com/nav_to.do?uri=" + $Result.sys_class_name + ".do?sys_id=" + $Result.sys_id

    New-UDTable -Columns @(
        New-UDTableColumn -Property 'CmdbCiUrl' -Title 'CmdbCiUrl' -Render { #https://northdakota.service-now.com/nav_to.do?uri=cmdb_ci_win_server.do?sys_id=8bc3ecd11b0154509d7ada01dd4bcb59
            New-UDLink -Url ("https://northdakota.service-now.com/nav_to.do?uri=" + $EventData.sys_class_name + ".do?sys_id=" + $EventData.sys_id) -Id "lnkCmdbCi" -Content {
                New-UDImage -Url "https://psuniversal.nd.gov/PSUniversal_Extras/servicenow_logo_icon_168835.png" -Height 20 -Width 20
            }
        }
        New-UDTableColumn -Property 'name' -Title 'name' -ShowSort -ShowFilter
        New-UDTableColumn -Property 'dns_domain' -Title 'dns_domain' -ShowFilter
        New-UDTableColumn -Property 'u_nd_application_svc' -Title 'u_nd_application_svc' -ShowFilter
        New-UDTableColumn -Property 'environment' -Title 'environment' -ShowFilter
        New-UDTableCOlumn -Property 'operational_status' -Title 'operational_status' -ShowFilter
        New-UDTableColumn -Property 'u_nd_type' -Title 'u_nd_type' -ShowFilter
        #New-UDTableColumn -Property 'model_id' -Title 'model_id' -ShowFilter
        #New-UDTableColumn -Property 'os' -Title 'os' -ShowFilter
        #New-UDTableColumn -Property 'os_version' -Title 'os_version' -ShowFilter
        New-UDTableColumn -Property 'u_nd_licensing_restrictions' -Title 'u_nd_licensing_restrictions' -ShowFilter
        New-UDTableColumn -Property 'u_support_hours' -Title 'u_support_hours' -ShowFilter
        New-UDTableColumn -Property 'u_nd_dr_protection' -Title 'u_nd_dr_protection' -ShowFilter
        New-UDTableColumn -Property 'u_srm_recovery_type' -Title 'u_srm_recovery_type' -ShowFilter
    ) -Data ($Result | sort-object Name) -ShowPagination -Dense -PageSize 50

    
} -OnProcessing {
    
}


New-UDCard -Id 'Result'
} -Title "CMDBServerList" -Icon @{
		id = 'dc0de7d9-022b-4e24-a7c7-7f0e59710542'
		type = 'icon'
	}