Dealing with an unordered array

This topic contains 1 reply, has 1 voice, and was last updated by  ssp1 2 months, 3 weeks ago.

  • Author
    Posts
  • #94734

    ssp1
    Participant

    Hello All – I am new to the forum and am trying to work out an approach to dealing with some data that comes back in a large, unordered array. I am using some custom cmdlets from a 3rd party so I somewhat have to work with what I've been given. I am dealing with 3 cmdlets they provided that we'll call: Get-MyDevices, Get-MyDeviceProperties, and Update-MyDeviceProperties.

    My goal is to be able to update the device display names with the data from a custom property if that property exists that we'll call "targetName".

    So, when I run Get-MyDevices, I get an id, displayName, and systemProperties. systemProperties comes back as an array. However, the contents of that array will differ between devices and may have a different number of array items even if for the same device type that I want to update.

    What I've tried so far is to use:

    $devices=Get-MyDevices | select id,displayName,systemProperties | where {$_.systemProperties -like "*targetName*"}

    What this seems to do is grab any device from the list that has a targetName property buried somewhere in the systemProperties array and set with some value is populated (not null) with the name I'd like to use to update the displayName. Note: I do not want any results that have a null value for the targetName within the systemProperties array, since I wouldnt't be able to update the actual displayName with null. My challenge is where to go from here (or if maybe I have already taken the wrong approach).

    I need to eventually push the list back into the update command with Update-MyDeviceProperties, but that only takes one device at a time and required an id parameter (for example):

    Update-MyDeviceProperties -id  -Property displayName -Value  

    Ideally, I could generate an array that was something like this and then perhaps pipe that into the update command with a for loop. To do that, I'd think the list would need to look like this (conceptually; assume systemPropertes.targetName is the expanded value of the array property targetName):
    id, displayName, systemProperties.targetName

    I hope this is making sense to someone and any help is greatly appreciated on how I could approach this.

  • #94771

    ssp1
    Participant

    I ended up figuring out how to handle this with something like the following followed by finding names that did not match and then updating:

    $list = @()
    
    foreach ($device in $devices) {
    	$item = New-Object psobject 
    	$item | Add-Member -type NoteProperty -Name 'id' -Value $device.id
    	$item | Add-Member -type NoteProperty -Name 'displayName' -Value $device.displayName
    	$item | Add-Member -type NoteProperty -Name 'targetName' -Value (Get-DeviceProperties -DeviceId $device.id | select name,value | where {($_.name -eq "targetName")} | select value) 
    	$list += $item
    
    }
    

You must be logged in to reply to this topic.