浏览代码

add convertto-hashtable

Tobias Simetsreiter 2 年之前
父节点
当前提交
0a996d42b1
共有 1 个文件被更改,包括 33 次插入0 次删除
  1. 33 0
      bin/ConvertTo-Hashtable.ps1

+ 33 - 0
bin/ConvertTo-Hashtable.ps1

@@ -0,0 +1,33 @@
+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) { &$PSCommandPath $object }
+        )
+
+        Write-Output -NoEnumerate $collection
+    }
+    elseif ($InputObject -is [psobject])
+    {
+        $hash = @{}
+
+        foreach ($property in $InputObject.PSObject.Properties)
+        {
+            $hash[$property.Name] = (&$PSCommandPath $property.Value).PSObject.BaseObject
+        }
+
+        $hash
+    }
+    else
+    {
+        $InputObject
+    }
+}