Author Posts

March 12, 2015 at 10:17 am

Trying to create a list of delivery addresses for UPS. This consists of data returned from our order system, in the form of

I created a custom object to pull in data from other objects that are returned from our order system API .

$Delivery = New-Object -TypeName PSObject -Property @{
ID = $PBOrder.Response.ID
AddressFullNames = $BPOrder.Response.Parties.Delivery.AddressFullNames
AddressLine1 = $BP{Order.Response.Parties.Delivery.AddressLine1
AddressLine2 = $BPOrder.Response.Parties.Delivery.AddressLine2
AddressLine3 = $BPOrder.Response.Parties.Delivery.AddressLine3
PostalCode = $BPOrder.Response.Parties.Delivery.PostalCode

The data structure that is returned is a custom system object. which when printed out looks like this

ID : {68, 69, 70, 71...}
AddressFullNames : {$null, $null, $null, $null...}
AddressLine1 : {237 Commerce St. Suite 101, PO 58, 237 Commerce St. Suite 101, PO 58...}
AddressLine3 : {, Saxeville, , Saxeville...}
PostalCode : {, R0H 0M0, , R0H 0M0...}
AddressLine2 : {Wiiliston VT 05495, , Wiiliston VT 05495, ...}

I can access any of the items individual using dot notation.
For example
C:>PS $Delivery.PostalCode #But this prints out all of the postal codes.

R0H 0M0
R0H 0M0
S1 4RT
R0H 0M0

Of course, what I want is a regular address records that look like this.

ID : 70
AddressFullNames : Joe Blow
AddressLine1 : 123 Anywhere Lane
AddressLine2 : Sometown
AddressLine3 : VT
PostalCode : 05446

ID : 72
AddressFullNames : Mary Smith
AddressLine1 : 18 Oak Street
AddressLine2 : Nyack
AddressLine3 : NY
PostalCode : 20342

Ultimately, I want to convertto-CSV to have this as a CSV File which I can then import into my UPS shipping program.

I tried various ForEach-Object.... but don't know if you can actually nest those? So, if an object contains strings fields and fields that are hash tables.... how would you print out the string field and the hash table field in a coherent record, where

PS>$customer= @{Name="Davy Jones"; Delivery=@{Company="BigCorp"; Address="123 Corporate Lane"; City="Gotham"; State="NY"; Zip="03232"}}

Name Value
—- —–
Delivery {Zip, State, Company, Address...}
Name Davy Jones

I'm missing something here...


March 12, 2015 at 10:50 am

Try using Format-Custom. There's a parameter to control the enumeration depth.

And yes, you can have nested ForEach loops, and nested ForEach-Object, although with the cmdlet it can get tough to keep track of what $_ refers to.

March 12, 2015 at 11:28 am

Don...thanks much! I'lll look in to Forma-Custom... hadn't thought of that.

Could you provide a pointer to the syntax for the nested ForEach or ForEach-Object loops?
I tried them both every which way, but can't seem to figure out how they would work.

So, for my example
$customer= @{Name="Davy Jones"; Delivery=@{Company="BigCorp"; Address="123 Corporate Lane"; City="Gotham"; State="NY"; Zip="03232"}}
I would think it should be something like:

$customer | foreach-object ($_) { (Out-String -InputObject $ + (foreach-object -InputObject $ ($_) {Out-String – InputObject $_.Company})}

Everything up to the plus sign works, but then I get errors;
"You must provide a value folllowing the '+' operator"
"Systems.collections.hashtable cannot be resolved to a method."

March 12, 2015 at 12:20 pm

So, I eliminated the plus sign, clearly that wasn't working. I can get it to print out (at least) with this:

$customer | ForEach-Object ($_) {
Out-String -InputObject ( `
$_.Name,  `
$_.Delivery.City , 

123 Corporate Lane

piping this to get-member yields a string type.
What I'd like is the name pairs.... which would then go nicely into convertto-CSV.