简体   繁体   中英

Array Of Hashtable Formating in Powershell

I've tried to find a solution by myself and I apologize if it was already answered somewhere but I could'nt make this work by myself.

I have written a script that list of wifi networks, get different informations and put them in a hash table. From there I've created a PSObject.

The code looks like this :

$WLANS=@{}
$wlanData = netsh wlan show networks mode=BSSID

( ... Here i extract all the info from $Wlandata ...)

    $WLANS.SSID = $SSID
    $WLANS.BSSID = $BSSID
    $WLANS.RSSI = $RSSI

$(foreach ($ht in $WLANS){new-object PSObject -Property $ht}) | Format-Table -AutoSize

The outpout looks like this :

RSSI       SSID               BSSID
----       ----               -----
{97, 16}   {TEST, SFR-6019}    {xx:xx:xx:xx:xx:xx, yy:yy:yy:yy:yy:yy}

and i want it to look like this :

RSSI       SSID               BSSID
----       ----               -----
97         TEST               xx:xx:xx:xx:xx:xx
16         SFR-6019           yy:yy:yy:yy:yy:yy

I have tried different things but i always get the same kind of output. If anybody can help it would be greatly appreciated !

Hmm i see to less code. I think you missed a loop. Try this foreach wlan you select data:

$WLANS=@{}

$CurrentWLAN = "" | Select-Object -Property SSID, BSSID, RSSI

$CurrentWLAN.SSID = $SSID
$CurrentWLAN.BSSID = $BSSID
$CurrentWLAN.RSSI = $RSSI

$WLANS += $CurrentWLAN

Instead of putting the properties in a hashtable you need to build a custom object. It would look like this

$Processes = Get-Process | select -first 2
$CustomObj = Foreach ($Process in $Processes)
{
    [pscustomobject] @{
    'Name' = $Process.ProcessName
    'Handles' = $Process.Handles
    'Comment' = 'test123'
    }
}
$CustomObj

So you have your array, you iterate through the array and each loop builds a single custom object with your name/value pairs. The individual objects are then collected in the object array $CustomObj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM