Reputation: 165
So I have this Powershell-script that is supposed to get the status of all SQL Agent Jobs in my cluster. The problem is that one of the objects value is withing square brackets, here is the code:
$output = ForEach ($instance in $instances)
{
$instanceName = $instance.InstanceName
Get-SqlAgentJob -ServerInstance "$env:computername\$instanceName" |
Where-Object {$_.IsEnabled -eq "True" -and $_.LastRunDate -gt [DateTime]::Today.AddDays(-2)} |
Select-Object Name,Description,Parent,LastRunOutcome,LastRunDate,LastRunDuration,HasSchedule,@{
name='JobSchedules'
expression={$_.JobSchedules -join ','}
}
}
This is the $output:
Name : TestJob
Description : No description available.
Parent : [SERVER\INSTANCE]
LastRunOutcome : Succeeded
LastRunDate : 2020-06-01 05:30:00
LastRunDuration : 00:03:44
HasSchedule : True
JobSchedules : Daily
As you can see, the "Parent" object is within square brackets [], how do I delete them in the end result?
Upvotes: 0
Views: 767
Reputation: 13537
PowerShell has a feature called Calculated properties you can use to solve this.
It uses this syntax where Property1
and Property2
are normal properties:
Select-Object Property1,Property2, @{Name=‘NewColumn‘;Expression={<#code to calculate#>}}
For instance, if you wanted to show the file size of each item in a directory in Megabytes, you could just add this column to your Select-Object
command for Get-ChildItem
.
dir | Select-Object Name,Length,@{Name='SizeMB';Exp={$_.Length/1mb -as [int]}}
Name Length SizeMB
---- ------ ------
My Neighbor Totoro OST - 03 A Haunted House.mp4 4530480 4
myCoolBmp.bmp 1243 0
Ponyo Theme Song (with lyrics)-vUCnXCyUm7w.mp4 7884658 8
ResultUI.png 42887 0
shutUpandTakeMyMoney.png 575992 1
SomeNotes.txt 14 0
SONIC • Chill Out Music Compilation (Part 1).mkv 168230106 160
The Village in May (My Neighbor Totoro)-5QXF2GeniSY.mp4 2686660 3
twimage.png 167827 0
Video Game Music for Studying 19-P-BqmegCUpA.webm 359146270 343
So, to put it in practice for you, in the Select-Object
statement simply remove Parent
and replace it with
@{Name=‘Parent‘;Expression={$_.Parent.Replace('[','').Replace(']','')}}
Upvotes: 2