Hello.
I have written my first PS script and very excited to be using the tool. I was able to combine two objects and mostly it works, but I when I run it, the Combined Object displays on each loop. I am curious which line of code causes this. Also, it appears my final Combined Object is only a single row and not the rows from the file.
I appreciate all critiques, this is the way we learn.
Thank you.
function Merge-Objects {
[CmdletBinding()]
param (
[Object] $Object1,
[Object] $Object2
)
$global:CombinedObject = [ordered] @{}
foreach ($Property in $Object1.PSObject.Properties) {
$CombinedObject += @{$Property.Name = $Property.Value}
}
foreach ($Property in $Object2.PSObject.Properties) {
$CombinedObject += @{$Property.Name = $Property.Value}
}
return [pscustomobject]$CombinedObject
}
#-----------------------------------------------------------------------------------------------------
# Main processing routine
#-----------------------------------------------------------------------------------------------------
#Import the Netsuite file into object
Write-Host "Import $netsuite_file"
$netsuite_rows = Import-Excel $report_directory\$netsuite_file -ImportColumns @(2, 3, 5, 13, 14, 15, 16, 20) -StartRow 1 -EndRow 1000
#
#Import the Magento file into object
Write-Host "Import Magento file"
$magento_rows = Import-Excel $report_directory\$magento_file -ImportColumns @(1, 8, 9, 22, 24, 26, 47, 77) -StartRow 1 -EndRow 1000
#
#Loop through the Netsuite rows and query the Magento rows for the item
foreach ($nsrow in $netsuite_rows) {
if ($nsrow.Name -like "*.00*") { #If the item ends in .00, just search for the first 5 characters
$searchfor = $nsrow.Name.Substring(0,5)
} else {
$searchfor = $nsrow.Name
}
foreach ($mgrow in $magento_rows)
{
$mgrow | Where-Object -Property sku -Like $searchfor |
Foreach-Object {
#Test add values to object
$newObject = $mgrow | Select-Object *, @{label='CA Prop 65 Warning'; expression={'No'}}
#Test remove values from object
#$object = $newObject | Select-Object * -ExcludeProperty ID, Address
#Test modify the value of an object
$newObject | ForEach-Object {$_.'thumbnail_image' = "Mickey Mouse"
$_
}
Merge-Objects -Object1 $nsrow -Object2 $newObject
}
}
}
#future - create the Excel file and output CombinedObject
#rm $report_directory$output_file
#Export-Excel @ExcelParams
#$CombinedObject | Export-Excel $report_directory$output_file -Append -WorksheetName "Sheet1"
#$CombinedObject - for testing, only shows one element?