Page 1 of 2

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

Posted: Fri Nov 02, 2018 7:00 am
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

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

Posted: Fri Nov 02, 2018 7:09 am
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.

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

Posted: Fri Nov 02, 2018 10:11 am
by lontru
could you provide a sample on how to do that?

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

Posted: Fri Nov 02, 2018 2:02 pm
by jvierra
The following will help you understand how to use computed parameters. See Ex #4
help select-object -Examples

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

Posted: Fri Nov 02, 2018 2:46 pm
by lontru
[/Codebox]
  1. $button_search_Click={
  2.  
  3.     $CMDevices = Get-CMDevice -Name "*$($textbox_devicename.Text)*" -ForceWildcardHandling -Fast | Select-Object -Property ResourceId
  4.  
  5.     $CMDeviceObjects = @()
  6.  
  7.     foreach ($CMDevice in $CMDevices)
  8.  
  9.     {
  10.  
  11.         $CMResource = Get-CMResource -ResourceId "$($CMDevice.ResourceId)" -Fast | Select-Object -Property Name, MACAddresses, CreationDate, LastLogonUserName
  12.  
  13.         $CMResource.Name
  14.  
  15.         $CMDeviceObject = New-Object -TypeName psobject
  16.  
  17.         $CMDeviceObject | Add-Member -MemberType NoteProperty -Name Name -Value $CMResource.Name
  18.  
  19.         $CMDeviceObject | Add-Member -MemberType NoteProperty -Name MACAddresses -Value $($CMResource.MACAddresses -join ", ")
  20.  
  21.         $CMDeviceObject | Add-Member -MemberType NoteProperty -Name CreationDate -Value $CMResource.CreationDate
  22.  
  23.         $CMDeviceObject | Add-Member -MemberType NoteProperty -Name LastLogonUserName -Value $CMResource.LastLogonUserName
  24.  
  25.         $CMDeviceObjects += $CMDeviceObject
  26.  
  27.     }
  28.  
  29.     Update-DataGridView -DataGridView $datagridview_device -Item $CMDeviceObjects
  30.  
  31. }
  32.  
  33.  

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

Posted: Fri Nov 02, 2018 3:05 pm
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 ", "}})}

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

Posted: Fri Nov 02, 2018 3:10 pm
by lontru
  1. $button_search_Click={
  2.  
  3. $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 ", " } }) }
  4.  
  5. Update-DataGridView -DataGridView $datagridview_device -Item $CMDevices
  6.  
  7. }
Thank you. Think above code is the best solution. Learned about the -join and 'Name and Expression keys'

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

Posted: Fri Nov 02, 2018 3:27 pm
by lontru
Image

How do i autosize to the whole macaddress is visible? and the name space is smaller?

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

Posted: Fri Nov 02, 2018 3:28 pm
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.

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

Posted: Fri Nov 02, 2018 3:32 pm
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.