Hello All,
I'm very new to Powershell and am trying to execute some code which will remove commas from certain fields in a CSV file, then output the result back to a file. While my script does generate the following error, it still does finish and create a new file when I run it from the Powershell ISE. However, I need to run this from a SQL job agent (i'm a data guy) and the error actually stops the job.
There error is here:
You cannot call a method on a null-valued expression.
At line:9 char:9
+ if ($i.Community_Name.split(',')[1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
My code i'm running is here:
$array = Import-Csv -Path D:\DataExtracts\HolidayPricing\holiday.csv | Select UniqueID,Community_Name,Company_Number,LastUpdated,UnitType,MinStartMarketRate
$finalarray = @()
Foreach ($i in $array)
{
$newobject = New-Object System.Object
$newobject | Add-Member -Type NoteProperty -Name 'Unique ID' -Value $i.UniqueID
if ($i.Community_Name.split(',')[1])
{
$formatted = $i.Community_Name.split(',')[0] + ';' + $i.Community_Name.split(',')[1]
}
else
{
$formatted = $i.Community_Name.split(',')[0]
}
$newobject | Add-Member -Type NoteProperty -Name 'Community_Name' -Value $formatted
$newobject | Add-Member -Type NoteProperty -Name 'Company_Number' -Value $i.Company_Number
$newobject | Add-Member -Type NoteProperty -Name 'LastUpdated' -Value $i.LastUpdated
$newobject | Add-Member -Type NoteProperty -Name 'UnitType' -Value $i.UnitType
$newobject | Add-Member -Type NoteProperty -Name 'MinStartMarketRate' -Value $i.MinStartMarketRate
$finalarray += $newobject
}
$finalarray | Export-Csv -Path D:\DataExtracts\HolidayPricing\holiday_Out.csv