Skip to content

Update Get-ServiceNowRecord.ps1 #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions ServiceNow/Public/Get-ServiceNowRecord.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ function Get-ServiceNowRecord {
[ValidateScript( {
if ($_ -match '^[a-zA-Z0-9]{32}$' -or $_ -match '^([a-zA-Z]+)[0-9]+$') {
$true
}
else {
} else {
throw 'Id must either be a SysId 32 character alphanumeric or Number with prefix and id.'
}
})]
Expand All @@ -185,8 +184,7 @@ function Get-ServiceNowRecord {
[ValidateScript( {
if ($_ -match '^[a-zA-Z0-9]{32}$' -or $_ -match '^([a-zA-Z]+)[0-9]+$') {
$true
}
else {
} else {
throw 'ParentId must either be a SysId 32 character alphanumeric or Number with prefix and id.'
}
})]
Expand Down Expand Up @@ -269,8 +267,7 @@ function Get-ServiceNowRecord {

if ( $thisID -match '^[a-zA-Z0-9]{32}$' ) {
$thisParams.Filter += , @('sys_id', '-eq', $thisID)
}
else {
} else {
$thisParams.Filter += , @('number', '-eq', $thisID)
}
}
Expand All @@ -283,8 +280,7 @@ function Get-ServiceNowRecord {

if ( $ParentID -match '^[a-zA-Z0-9]{32}$' ) {
$thisParams.Filter += , @('parent.sys_id', '-eq', $ParentID)
}
else {
} else {
$thisParams.Filter += , @('parent.number', '-eq', $ParentID)
}

Expand Down Expand Up @@ -363,43 +359,50 @@ function Get-ServiceNowRecord {

# show the underlying value if the option is a reference type
if ( $newVar.Type -eq 'Reference' ) {
$newVar | Add-Member @{'ReferenceTable' = $var.'sc_item_option.item_option_new.reference' }
# issue 234. ID might not be sysid or number for reference...odd
$refValue = Get-ServiceNowRecord -Table $var.'sc_item_option.item_option_new.reference' -ID $var.'sc_item_option.value' -Property name -AsValue -ServiceNowSession $ServiceNowSession -ErrorAction SilentlyContinue
if ( $refValue ) {
$newVar.Value = $refValue
#do not do any further lookup when the value is blank or null
#resolves #234 and 262
if ($var.'sc_item_option.value' -eq "" -or $null -eq $var.'sc_item_option.value') {
continue
}
$sysidPattern = "[0-9a-fA-F]{32}"
$sysid = [Regex]::Matches($var.'sc_item_option.value', $sysidPattern).Value
if ($sysid) {
Write-Verbose "Custom variable lookup for $($newvar.name) from table '$($var.'sc_item_option.item_option_new.reference')' sysid:'$($var.'sc_item_option.value')'"
$newVar | Add-Member @{'ReferenceTable' = $var.'sc_item_option.item_option_new.reference' }
$newVar | Add-Member @{'ReferenceID' = $var.'sc_item_option.value' }
# issue 234. ID might not be sysid or number for reference...odd
$refValue = Get-ServiceNowRecord -Table $var.'sc_item_option.item_option_new.reference' -ID $var.'sc_item_option.value' -Property name -AsValue -ServiceNowSession $ServiceNowSession -ErrorAction SilentlyContinue
if ( $refValue ) {
$newVar.Value = $refValue
}
}

}

if ( $var.'sc_item_option.item_option_new.name' ) {
$record.CustomVariable | Add-Member @{ $var.'sc_item_option.item_option_new.name' = $newVar }
}
else {
} else {
$record.CustomVariable | Add-Member @{ $var.'sc_item_option.item_option_new.question_text' = $newVar }
}
}

if ( $addedSysIdProp ) {
$record | Select-Object -Property * -ExcludeProperty sys_id
}
else {
} else {
$record
}
}
}
else {
} else {

# format the results
if ( $Property ) {
if ( $Property.Count -eq 1 -and $AsValue ) {
$propName = $result | Get-Member | Where-Object { $_.MemberType -eq 'NoteProperty' } | Select-Object -ExpandProperty Name
$result.$propName
}
else {
} else {
$result
}
}
else {
} else {
if ($thisTable.Type) {
$result | ForEach-Object { $_.PSObject.TypeNames.Insert(0, $thisTable.Type) }
}
Expand Down
Loading