Files
Sandbox/ServiceNow-RitmDumpMK2.ps1
T
Zack Meier 03dba08135 sync
2026-04-15 15:42:41 -05:00

151 lines
9.4 KiB
PowerShell

# modified last hour block
#[datetime]$StartDate = (Get-Date -Minute 0 -Second 0).AddHours(-1)
#[datetime]$EndDate = (Get-Date -Minute 59 -Second 59).AddHours(-1)
# modified yesterday
#[datetime]$StartDate = (Get-Date -Hour 0 -Minute 0 -Second 0).AddDays(-1)
#[datetime]$EndDate = (Get-Date -hour 23 -Minute 59 -Second 59).AddDays(-1)
# modified 2023
[datetime]$StartDate = (Get-Date -Year 2023 -Month 4 -Day 1 -Hour 0 -Minute 0 -Second 0).AddDays(-1)
[datetime]$EndDate = (Get-Date -Year 2023 -Month 5 -Day 31 -Hour 23 -Minute 59 -Second 59).AddDays(-1)
# edit filter as needed
$Filter = @('cat_item', '-eq', '89efc7041bb41dd04d8943b1b24bcb63'),
'-and',
@('sys_updated_on', '-gt', $StartDate ),
'-and',
@('sys_updated_on', '-lt', $EndDate )
#>
$RecordSearch = Get-ServiceNowRecord -Table 'Requested Item' -Filter $Filter -IncludeTotalCount | select *, @{n = 'cat_item_display_name'; e = { $_.cat_item.display_value } }
# TO-DO: loop each cat_item_display_name, and export to dedicated csv
$CatItemDisplayNames = ($RecordSearch | Select-Object -Unique cat_item_display_name).cat_item_display_name
ForEach ($CatItemDisplayName in $CatItemDisplayNames) {
Clear-Variable -Name RecordsToImport -ErrorAction SilentlyContinue
$RecordsToImport = $RecordSearch | Where-Object cat_item_display_name -EQ $CatItemDisplayName
$CsvPath = "D:\OneDrive - State of North Dakota\ServiceNow\RitmDump-$CatItemDisplayName.csv"
Write-Warning -Message ($CatItemDisplayName + " records to import = " + $RecordsToImport.count)
$RecordsToExport = [System.Collections.ArrayList]@()
ForEach ($Record in $RecordsToImport) {
Write-Warning -Message ("Begin record " + $Record.number)
$RecordDetail = Get-ServiceNowRecord -Table 'Requested Item' -ID $Record.Number -IncludeCustomVariable
$obj = [PSCustomObject][ordered]@{
'number' = $Record.number;
'sys_id' = $Record.sys_id;
'opened_at' = $Record.opened_at;
'requested_for' = $Record.requested_for.display_value;
'sys_updated_on' = $Record.sys_updated_on
'short_description' = $Record.short_description;
}
ForEach ($Property in $Record.PSObject.Properties) {
If ($obj.PSObject.Properties.Name -match $Property.Name) {
#Write-Verbose -Message ("Property " + $Property.Name + " already in obj")
}
Else {
# add missing default RITM property to new obj
$obj | Add-Member -MemberType $Property.MemberType -Name $Property.Name -Value $Property.Value -TypeName $Property.TypeNameOfValue -Verbose
}
}
ForEach ($Property in ($RecordDetail.CustomVariable | Sort-Object Name)) {
If ($obj.PSObject.Properties.Name -match $Property.Name) {
#Write-Verbose -Message ("Property " + $Property.Name + " already in obj")
}
Else {
# add missing custom variables to new obj
$obj | Add-Member -MemberType NoteProperty -Name $Property.Name -Value $Property.Value
}
}#>
$null = $RecordsToExport.Add($obj)
}
If ($RecordsToExport) { $RecordsToExport | Export-Csv $CsvPath -Append -NoTypeInformation -Force }
}
<#
$CurrentCsv = Import-Csv -Path $CsvPath
ForEach ($Record in $RecordSearch) {
Write-Warning -Message ("Begin record " + $Record.number)
If ( @($CurrentCsv.number | Where-Object { $_ -eq $Record.Number }).count -gt 0) {
Write-Warning -Message ("Number " + $Record.Number + " already in csv")
# do nothing
}
Else {
#Write-Warning -Message $record.number
$RecordDetail = Get-ServiceNowRecord -Table 'Requested Item' -ID $Record.Number -IncludeCustomVariable
ForEach ($Record in $RecordDetail) {
$RecordsToExport = [System.Collections.ArrayList]@()
$obj = [PSCustomObject][ordered]@{
# get specific custom fields first, so the columns are on the left of csv
'number' = $Record.number;
'sys_id' = $Record.sys_id;
'opened_at' = $Record.opened_at;
'requested_for' = $Record.requested_for.display_value;
'short_description' = $Record.short_description;
'request_type' = ($Record.CustomVariable | Where-Object Name -EQ 'request_type').Value
'application_name' = ($Record.CustomVariable | Where-Object Name -EQ 'application_name').Value
'environment' = ($Record.CustomVariable | Where-Object Name -EQ 'environment').Value
'additional_comments' = ($Record.CustomVariable | Where-Object Name -EQ 'additional_comments').Value
'vm_work_needed' = ($Record.CustomVariable | Where-Object Name -EQ 'vm_work_needed').Value
'server_name' = ($Record.CustomVariable | Where-Object Name -EQ 'server_name').Value
'host_name' = ($Record.CustomVariable | Where-Object Name -EQ 'host_name').Value
'server_type' = ($Record.CustomVariable | Where-Object Name -EQ 'server_type').Value
'operating_system' = ($Record.CustomVariable | Where-Object Name -EQ 'operating_system').Value
'target_platform' = ($Record.CustomVariable | Where-Object Name -EQ 'target_platform').Value
'processors' = ($Record.CustomVariable | Where-Object Name -EQ 'processors').Value
'memory_gb' = ($Record.CustomVariable | Where-Object Name -EQ 'memory_gb').Value
'cidr_block' = ($Record.CustomVariable | Where-Object Name -EQ 'cidr_block').Value
'data_center' = ($Record.CustomVariable | Where-Object Name -EQ 'data_center').Value
'licensing_restrictions' = ($Record.CustomVariable | Where-Object Name -EQ 'licensing_restrictions').Value
'agency_name' = ($Record.CustomVariable | Where-Object Name -EQ 'agency_name').Value
'application_info' = ($Record.CustomVariable | Where-Object Name -EQ 'application_info').Value
'support_hours' = ($Record.CustomVariable | Where-Object Name -EQ 'support_hours').Value
'dr_protection' = ($Record.CustomVariable | Where-Object Name -EQ 'dr_protection').Value
'startup_priority' = ($Record.CustomVariable | Where-Object Name -EQ 'startup_priority').Value
'disk_1_os' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_1_os').Value
'disk_2_swap_disk' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_2_swap_disk').Value
'disk_3' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_3').Value
'disk_4' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_4').Value
'disk_5' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_5').Value
'disk_6' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_6').Value
'disk_7' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_7').Value
'disk_8' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_8').Value
'disk_9' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_9').Value
'disk_10' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_10').Value
'disk_11' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_11').Value
'disk_12' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_12').Value
'disk_13' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_13').Value
'disk_14' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_14').Value
'disk_15' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_15').Value
'disk_16' = ($Record.CustomVariable | Where-Object Name -EQ 'disk_16').Value
}
ForEach ($Property in $Record.PSObject.Properties) {
If ($obj.PSObject.Properties.Name -match $Property.Name) {
#Write-Verbose -Message ("Property " + $Property.Name + " already in obj")
}
Else {
# add missing default RITM property to new obj
$obj | Add-Member -MemberType $Property.MemberType -Name $Property.Name -Value $Property.Value -TypeName $Property.TypeNameOfValue -Verbose
}
}
ForEach ($Property in $Record.CustomVariable) {
If ($obj.PSObject.Properties.Name -match $Property.Name) {
#Write-Verbose -Message ("Property " + $Property.Name + " already in obj")
}
Else {
# add missing custom variables to new obj
$obj | Add-Member -MemberType NoteProperty -Name $Property.Name -Value $Property.Value
}
}
$null = $RecordsToExport.Add($obj)
If ($RecordsToExport) { $RecordsToExport | Export-Csv $CsvPath -Append -NoTypeInformation -Force }
}
}
}
#>