Filter array of hashtables - powershell

I have an array of hashtables and I am trying to filter it to those who has a property value to true, but this what I am doing does not looks good.
# object looks like this
$array = #(
#{ Name = 'First'; Passed = $true }
#{ Name = 'Second'; Passed = $false }
)
function Filter {
Param($array)
$filtered = #()
foreach ($item in $array) {
if ($item.Passed = $true) {
$filtered += $item
}
}
return $filtered
}
Is there any other way I can get all elements who has property Passed = $True without the need to add those to another array.

Just pipe your array into a Where-Object like so:
$array = #(
#{ Name = 'First'; Passed = $True }
#{ Name = 'First'; Passed = $False }
)
$array = $array | Where-Object Passed -EQ $True

Related

`Write-Output` in `foreach` collection expression

I'm actually working my way through Bruce Payette's Powershell in Action and run to a code snippet I actually don't understand.
$characterData = #{
'Linus' = #{ age = 8; human = $true}
'Lucy' = #{ age = 8; human = $true}
'Snoopy' = #{ age = 2; human = $true}
}
function Get-Character ($name = '*')
{
foreach ($entry in $characterData.GetEnumerator() | Write-Output)
{
if ($entry.Key -like $name)
{
$properties = #{ 'Name' = $entry.Key } +
$entry.Value
New-Object PSCustomObject -Property $properties
}
}
}
My problem is that I don't understand why Write-Output is used in foreach ($entry in $characterData.GetEnumerator() | Write-Output)? Can this syntax be interpreted as equivalent to $characterData.GetEnumerator() | ForEach-Object { ... ?
Thx

Can you have multiple IN conditions in a PowerShell ForEach loop?

I have the following sample code in a function:
[array]$ARR = $null
foreach ($file in $fileTable.identical)
{
[hashtable]$HT=#{
'FileName' = $file.Name
'AppName' = $file.App
'GroupName' = $file.Group
'Valid' = $true
}
$ARR += $HT
}
foreach ($file in $fileTable.removed)
{
[hashtable]$HT=#{
'FileName' = $file.Name
'AppName' = $file.App
'GroupName' = $file.Group
'Valid' = $false
}
$ARR += $HT
}
foreach ($file in $fileTable.modified)
{
[hashtable]$HT=#{
'FileName' = $file.Name
'AppName' = $file.App
'GroupName' = $file.Group
'Valid' = $false
}
$ARR += $HT
}
return $ARR
+3 more foreach loops for other $fileTable.[properties] where 'Valid' = $false as well.
Instead of having to repeat the block of code multiple times, I want to do something like:
foreach (($file in $fileTable.removed) -and ($file in $fileTable.modified))
{
[hashtable]$HT=#{
'FileName' = $file.Name
'AppName' = $file.App
'GroupName' = $file.Group
'Valid' = $false
}
}
So only variable different in the hashtable will be $value.
$fileTable is a pscustomobject with a few custom properties like identical, modified, added, removed.
I know what I want is not possible in foreach loops but I'm looking for a similar solution to reduce the number of lines of code. Any help would be appreciated :)
Thanks!
Combining your and PetSerAls approaches.
Edit: incorporated #mklement0s hint
$ARR = foreach($Variant in 'identical', 'removed', 'modified'){
$fileTable.$Variant | ForEach-Object{
[PSCustomObject]#{
'FileName' = $_.Name
'AppName' = $_.App
'GroupName' = $_.Group
# 'Valid' = if($Variant -eq 'identical'){$True} else {$False}
'Valid' = $Variant -eq 'identical'
}
}
}

Loop Confusion Comparing Objects

I am trying to create my own service comparison script. I see some online but want to do this myself. I've only gotten so far. I keep getting confused.
The desired output is with the following format. It doesn't even have to show what's different. I just want to see what the previous state was compared to the current state. I did a compare-object and it didn't give me the format I desired. I then thought maybe I should just do two nested loops and create a new object with the states I want in it. It didn't work out correctly, it returns an array. So then I thought, maybe a for loop in the foreach loop... I keep confusing myself and it's so close.
You have to provide a csv with some services to compare to to make this work as part of it's paramaters.
Usage
Inspect-ServiceSnapshot -SnapshotPath "C:\YourPath"
Desired output
Name CurrentState PreviousState
app1 Running Stopped
Code So Far
function Inspect-ServiceSnapshot {
[CmdletBinding()]
param (
#Snapshot
[Parameter(Mandatory=$true)]
[ValidatePattern("C:")]
[string]
$SnapshotPath,
# timer
[Parameter(Mandatory=$false)]
[int]
$TimeToWait
)
if($TimeToWait -ne $null) {
Start-Sleep -Seconds $TimeToWait
$list = #()
$old = Import-Csv -Path $SnapshotPath
foreach($entry in (get-service)) {
foreach($oldItem in $old) {
$object = New-Object -TypeName psobject -Property #{
Name = $entry.Name
CurrentStatus = $entry.status
DisplayName = $entry.displayname
PreviousStatus = $oldItem.status
}
$list += $object
}
}
$list
} else {
$list = #()
$old = Import-Csv -Path $SnapshotPath
foreach($entry in (get-service)) {
foreach($oldItem in $old) {
$object = New-Object -TypeName psobject -Property #{
Name = $entry.Name
CurrentStatus = $entry.status
DisplayName = $entry.displayname
PreviousStatus = $oldItem.status
}
$list += $object
}
}
$list
}
}
This should do it because it is actually checking the value for the old service to be the same as the one Get-Service provides at a certain time.
function Inspect-ServiceSnapshot {
[CmdletBinding()]
param (
#Snapshot
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_ -PathType Leaf})]
[string]$SnapshotPath,
# timer
[Parameter(Mandatory=$false)]
[int]$TimeToWait = 0
)
if($TimeToWait) { Start-Sleep -Seconds $TimeToWait }
$list = #()
$old = Import-Csv -Path $SnapshotPath
foreach($entry in (Get-Service)) {
# make sure we are dealing with the SAME service
$oldItem = $old | Where-Object { $_.Name -eq $entry.Name }
$object = New-Object -TypeName psobject -Property #{
Name = $entry.Name
CurrentStatus = $entry.status
DisplayName = $entry.displayname
PreviousStatus = if ($oldItem) { $oldItem.status } else { 'Unknown' }
}
$list += $object
}
$list
}
This answer is assuming there are no new services added on a regular basis.
You almost got it! You can forgo the nested loops, get rid of the else block (it is redundant), and use an index loop. When you're using nested loops like that, you are iterating through all of the array elements in the $old array every time you iterate through one of the (get-service) objects. This can cause issues when your arrays include thousands of objects.
You can easily get to what you want by using a for loop.
e.g.
if($TimeToWait -ne $null) {
Start-Sleep -Seconds $TimeToWait
}
$list = #();
$old = Import-Csv -Path $SnapshotPath | Sort-Object Name;
$new = Get-Service | Sort-Object Name;
for ($i -eq 0; $i -lt $old.length -or $i -lt $new.length; $i++) {
$object = New-Object -TypeName psobject -Property #{
Name = $new[$i].Name
CurrentStatus = $new[$i].status
DisplayName = $new[$i].displayname
PreviousStatus = $old[$i].status
}
$list += $object;
}
$list;
A lot of your code is redundant and you can just do this all in one go. Since you're not technically comparing any objects, you can just populate the fields as they go.

Foreach inside hash table with powershell

Is there a way to insert foreach loop inside hash table.
Something like this?
$vms = get-vm
foreach ($vm in $vms) {
$disks=Get-Vhd $vm.id
$hash = [ordered]#{
'VM<br>Name' = $vm.vmname
'State' = $vm.state
'Disk' = foreach ($disk in $disks) {$disks.size -join '.' }
}
New-Object -TypeName PSObject -Property $hash
$data = foreach ($disk in $disks) {$disks.size -join '.' }
$hash = [ordered]#{
'VM<br>Name' = $vm.vmname
'State' = $vm.state
'Disk' = $data
}

PSCustomObject to Hashtable

What is the easiest way to convert a PSCustomObject to a Hashtable? It displays just like one with the splat operator, curly braces and what appear to be key value pairs. When I try to cast it to [Hashtable] it doesn't work. I also tried .toString() and the assigned variable says its a string but displays nothing - any ideas?
Shouldn't be too hard. Something like this should do the trick:
# Create a PSCustomObject (ironically using a hashtable)
$ht1 = #{ A = 'a'; B = 'b'; DateTime = Get-Date }
$theObject = new-object psobject -Property $ht1
# Convert the PSCustomObject back to a hashtable
$ht2 = #{}
$theObject.psobject.properties | Foreach { $ht2[$_.Name] = $_.Value }
Keith already gave you the answer, this is just another way of doing the same with a one-liner:
$psobject.psobject.properties | foreach -begin {$h=#{}} -process {$h."$($_.Name)" = $_.Value} -end {$h}
Here's a version that works with nested hashtables / arrays as well (which is useful if you're trying to do this with DSC ConfigurationData):
function ConvertPSObjectToHashtable
{
param (
[Parameter(ValueFromPipeline)]
$InputObject
)
process
{
if ($null -eq $InputObject) { return $null }
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string])
{
$collection = #(
foreach ($object in $InputObject) { ConvertPSObjectToHashtable $object }
)
Write-Output -NoEnumerate $collection
}
elseif ($InputObject -is [psobject])
{
$hash = #{}
foreach ($property in $InputObject.PSObject.Properties)
{
$hash[$property.Name] = ConvertPSObjectToHashtable $property.Value
}
$hash
}
else
{
$InputObject
}
}
}
My extremely lazy approach, enabled by a new feature in PowerShell 6:
$myhashtable = $mypscustomobject | ConvertTo-Json | ConvertFrom-Json -AsHashTable
This works for PSCustomObjects created by ConvertFrom_Json.
Function ConvertConvertFrom-JsonPSCustomObjectToHash($obj)
{
$hash = #{}
$obj | Get-Member -MemberType Properties | SELECT -exp "Name" | % {
$hash[$_] = ($obj | SELECT -exp $_)
}
$hash
}
Disclaimer: I barely understand PowerShell so this is probably not as clean as it could be. But it works (for one level only).
My code:
function PSCustomObjectConvertToHashtable() {
param(
[Parameter(ValueFromPipeline)]
$object
)
if ( $object -eq $null ) { return $null }
if ( $object -is [psobject] ) {
$result = #{}
$items = $object | Get-Member -MemberType NoteProperty
foreach( $item in $items ) {
$key = $item.Name
$value = PSCustomObjectConvertToHashtable -object $object.$key
$result.Add($key, $value)
}
return $result
} elseif ($object -is [array]) {
$result = [object[]]::new($object.Count)
for ($i = 0; $i -lt $object.Count; $i++) {
$result[$i] = (PSCustomObjectConvertToHashtable -object $object[$i])
}
return ,$result
} else {
return $object
}
}
For simple [PSCustomObject] to [Hashtable] conversion Keith's Answer works best.
However if you need more options you can use
function ConvertTo-Hashtable {
<#
.Synopsis
Converts an object to a hashtable
.DESCRIPTION
PowerShell v4 seems to have trouble casting some objects to Hashtable.
This function is a workaround to convert PS Objects to [Hashtable]
.LINK
https://github.com/alainQtec/.files/blob/main/src/scripts/Converters/ConvertTo-Hashtable.ps1
.NOTES
Base ref: https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/turning-objects-into-hash-tables-2
#>
PARAM(
# The object to convert to a hashtable
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
$InputObject,
# Forces the values to be strings and converts them by running them through Out-String
[switch]$AsString,
# If set, empty properties are Included
[switch]$AllowNulls,
# Make each hashtable to have it's own set of properties, otherwise,
# (default) each InputObject is normalized to the properties on the first object in the pipeline
[switch]$DontNormalize
)
BEGIN {
$headers = #()
}
PROCESS {
if (!$headers -or $DontNormalize) {
$headers = $InputObject | Get-Member -type Properties | Select-Object -expand name
}
$OutputHash = #{}
if ($AsString) {
foreach ($col in $headers) {
if ($AllowNulls -or ($InputObject.$col -is [bool] -or ($InputObject.$col))) {
$OutputHash.$col = $InputObject.$col | Out-String -Width 9999 | ForEach-Object { $_.Trim() }
}
}
} else {
foreach ($col in $headers) {
if ($AllowNulls -or ($InputObject.$col -is [bool] -or ($InputObject.$col))) {
$OutputHash.$col = $InputObject.$col
}
}
}
}
END {
return $OutputHash
}
}
Maybe this is overkill but I hope it Helps
Today, the "easiest way" to convert PSCustomObject to Hashtable would be so:
$custom_obj | ConvertTo-HashtableFromPsCustomObject
OR
[hashtable]$custom_obj
Conversely, you can convert a Hashtable to PSCustomObject using:
[PSCustomObject]$hash_table
Only snag is, these nifty options may not be available in older versions of PS