Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PSFramework/PSFramework.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
'Clear-PSFResultCache',
'ConvertFrom-PSFClixml',
'ConvertTo-PSFClixml',
'ConvertTo-PSFHashtable',
'Disable-PSFTaskEngineTask',
'Enable-PSFTaskEngineTask',
'Export-PSFClixml',
Expand Down
3 changes: 3 additions & 0 deletions PSFramework/changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# CHANGELOG
## 0.10.28.???
- New: Command ConvertTo-PSFHashtable converts objects into hashtables

## 0.10.28.144 : 2018-10-28
- Upd: Module Architecture update
- Upd: Linked online help for commands (by Andrew Pla)
Expand Down
56 changes: 56 additions & 0 deletions PSFramework/functions/utility/ConvertTo-PSFHashtable.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function ConvertTo-PSFHashtable
{
<#
.SYNOPSIS
Converts an object into a hashtable.

.DESCRIPTION
Converts an object into a hashtable.
Can exclude individual properties from being included.

.PARAMETER Exclude
The propertynames to exclude.
Must be full property-names, no wildcard/regex matching.

.PARAMETER InputObject
The object(s) to convert

.EXAMPLE
PS C:\> Get-ChildItem | ConvertTo-PSFHashtable

Scans all items in the current path and converts those objects into hashtables.
#>
[OutputType([System.Collections.Hashtable])]
[CmdletBinding()]
Param (
[string[]]
$Exclude,

[Parameter(ValueFromPipeline = $true)]
$InputObject
)

process
{
foreach ($item in $InputObject)
{
if ($null -eq $item) { continue }
if ($item -is [System.Collections.Hashtable])
{
$hashTable = $item.Clone()
foreach ($name in $Exclude) { $hashTable.Remove($name) }
$hashTable
}
else
{
$hashTable = @{ }
foreach ($property in $item.PSObject.Properties)
{
if ($property.Name -in $Exclude) { continue }
$hashTable[$property.Name] = $property.Value
}
$hashTable
}
}
}
}