$datagridview - System.String[]? not correct value?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

$datagridview - System.String[]? not correct value?

Post by lontru »

Hi all

PowerShell Studio 2018 v5.5.154

Trying to build af tool for SCCM.
Why do i get this "System.String[]" i want the macaddr "{00:0A:E4:1E:8F:6E}" in the cell.

Code: Select all

[Codebox=powershell file=Untitled.ps1]

$button_search_Click={
	$textbox_devicename.Text
	$combo_item = Get-CMDevice -Name "*$($textbox_devicename.Text)*" -ForceWildcardHandling | Select-Object -Property Name, MACAddresses, CreationDate
	Update-DataGridView -DataGridView $datagridview_device -Item $combo_item
}
[/Codebox]
ISE Code
Image

Powershell Studio
Image
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $datagridview - System.String[]? not correct value?

Post by jvierra »

MacAddresses is an array of MAC addresses. You need to subscript it to get the address. You can also use "-join" to concatenate the array. Use a computed property to display the addresses.
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: $datagridview - System.String[]? not correct value?

Post by lontru »

could you provide a sample on how to do that?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $datagridview - System.String[]? not correct value?

Post by jvierra »

The following will help you understand how to use computed parameters. See Ex #4
help select-object -Examples
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: $datagridview - System.String[]? not correct value?

Post by lontru »

[/Codebox]
  1. $button_search_Click={
  2.     $CMDevices = Get-CMDevice -Name "*$($textbox_devicename.Text)*" -ForceWildcardHandling -Fast | Select-Object -Property ResourceId
  3.     $CMDeviceObjects = @()
  4.     foreach ($CMDevice in $CMDevices)
  5.     {
  6.         $CMResource = Get-CMResource -ResourceId "$($CMDevice.ResourceId)" -Fast | Select-Object -Property Name, MACAddresses, CreationDate, LastLogonUserName
  7.         $CMResource.Name
  8.         $CMDeviceObject = New-Object -TypeName psobject
  9.         $CMDeviceObject | Add-Member -MemberType NoteProperty -Name Name -Value $CMResource.Name
  10.         $CMDeviceObject | Add-Member -MemberType NoteProperty -Name MACAddresses -Value $($CMResource.MACAddresses -join ", ")
  11.         $CMDeviceObject | Add-Member -MemberType NoteProperty -Name CreationDate -Value $CMResource.CreationDate
  12.         $CMDeviceObject | Add-Member -MemberType NoteProperty -Name LastLogonUserName -Value $CMResource.LastLogonUserName
  13.         $CMDeviceObjects += $CMDeviceObject
  14.     }
  15.     Update-DataGridView -DataGridView $datagridview_device -Item $CMDeviceObjects
  16. }
  17.  
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: $datagridview - System.String[]? not correct value?

Post by lontru »

jvierra wrote: Fri Nov 02, 2018 2:02 pm The following will help you understand how to use computed parameters. See Ex #4
help select-object -Examples
With above i came to this solution.
  1. Get-CMDevice -Name *9999* -ForceWildcardHandling | foreach {(Get-CMResource -ResourceId "$($PSItem.ResourceId)" -Fast | Select-Object -Property Name,CreationDate,@{Name="MACAddresses"; Expression = {$($_.MACAddresses) -join ", "}})}
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: $datagridview - System.String[]? not correct value?

Post by lontru »

  1. $button_search_Click={
  2. $CMDevices = Get-CMDevice -Name "*$($textbox_devicename.Text)*" -ForceWildcardHandling -fast | foreach { (Get-CMResource -ResourceId "$($PSItem.ResourceId)" -Fast | Select-Object -Property Name, CreationDate,LastLogonUserName,@{ Name = "MACAddresses"; Expression = { $($_.MACAddresses) -join ", " } }) }
  3. Update-DataGridView -DataGridView $datagridview_device -Item $CMDevices
  4. }
Thank you. Think above code is the best solution. Learned about the -join and 'Name and Expression keys'
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: $datagridview - System.String[]? not correct value?

Post by lontru »

Image

How do i autosize to the whole macaddress is visible? and the name space is smaller?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $datagridview - System.String[]? not correct value?

Post by jvierra »

Ok. Now lets simplify you code and make it readable and easier to maintain:

Code: Select all

$button_search_Click={
    $properties = @(
        'Name',
        'CreationDate',
        'LastLogonUserName',
        @{ n='MACAddresses'; e = {$_.MACAddresses -join ', '}}
    )
    $CMDevices = Get-CMDevice -Name "*$($textbox_devicename.Text)*" -ForceWildcardHandling -fast | 
        ForEach-Object{
            Get-CMResource -ResourceId $PSItem.ResourceId -Fast
        } |
        Select-Object $properties
    Update-DataGridView -DataGridView $datagridview_device -Item $CMDevices
}
Avoid long code lines by using PowerShell's capability to use advanced arguments and syntax formatting.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $datagridview - System.String[]? not correct value?

Post by jvierra »

lontru wrote: Fri Nov 02, 2018 3:27 pm How do i autosize to the whole macaddress is visible? and the name space is smaller?
Set the autosize property of the grid.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked