{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1908-powershell-hashtable " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## overview" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- hashtable object on powershell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![Image from Gyazo](https://i.gyazo.com/9c23d8a166aeaa46a042f3b1f33dcf33.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## jupyter notebook\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- https://nbviewer.jupyter.org/github/sakai-memoru/pwshnote/blob/master/1908-powershell-hashtable.ipynb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Usage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### basic" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$d = @{\n", " 'script' = 'powershell'\n", " 'os' = 'windows 10'\n", " 'date' = Get-Date\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "date 02/09/2019 13:47:21 \n", "script powershell \n", "os windows 10 \n", "\n", "\n" ] } ], "source": [ "$d" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "powershell\n" ] } ], "source": [ "$d['script']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "IsPublic IsSerial Name BaseType \n", "-------- -------- ---- -------- \n", "True True Hashtable System.Object \n", "\n", "\n" ] } ], "source": [ "$d.GetType()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "IsPublic IsSerial Name BaseType \n", "-------- -------- ---- -------- \n", "True True String System.Object \n", "\n", "\n" ] } ], "source": [ "$d['os'].GetType()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "IsPublic IsSerial Name BaseType \n", "-------- -------- ---- -------- \n", "True True DateTime System.ValueType \n", "\n", "\n" ] } ], "source": [ "$d['date'].GetType()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get-Help" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Category Module Synopsis \n", "---- -------- ------ -------- \n", "about_Hash_Tables HelpFile Describes how to create, use, and sort hash ta...\n", "\n", "\n" ] } ], "source": [ "Get-Help -Category HelpFile | where-object {$_.Name -match \"hash\"}" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Get-Help about_Hash* | set -Name store\n", "$store -split \"`r`n\" | set -Name ary_store\n", "$ary_store[0..25] + 'and more .....'\n", "TOPIC\n", " about_Hash_Tables\n", "\n", "SHORT DESCRIPTION\n", " Describes how to create, use, and sort hash tables in Windows PowerShell.\n", "\n", "\n", "LONG DESCRIPTION\n", " A hash table, also known as a dictionary or associative array, is a\n", " compact data structure that stores one or more key/value pairs. For\n", " example, a hash table might contain a series of IP addresses and\n", " computer names, where the IP addresses are the keys and the computer\n", " names are the values, or vice versa.\n", "\n", " In Windows PowerShell, each hash table is a Hashtable \n", " (System.Collections.Hashtable) object. You can use the properties\n", " and methods of Hashtable objects in Windows PowerShell.\n", "\n", " Beginning in Windows PowerShell 3.0, you can use the [ordered]\n", " attribute to create an ordered dictionary\n", " (System.Collections.Specialized.OrderedDictionary) in Windows PowerShell.\n", " \n", " Ordered dictionaries differ from hash tables in that the keys always\n", " appear in the order in which you list them. The order of keys in a hash\n", " table is not determined.\n", "\n", "and more .....\n", "\n", "\n" ] } ], "source": [ "Get-Help about_Hash* | set -Name store\n", "$store -split \"`r`n\" | set -Name ary_store\n", "$ary_store[0..25] + 'and more .....'\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initialize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Empty object " ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$dic = @{}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### truthiness and falsiness" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "true\n" ] } ], "source": [ "if($dic){'true'}else{'false'}" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "false\n" ] } ], "source": [ "if($dic.Count){'true'}else{'false'}" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "$dic.Count" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get-Member" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Definition \n", "---- ---------- \n", "Count int Count {get;} \n", "IsFixedSize bool IsFixedSize {get;} \n", "IsReadOnly bool IsReadOnly {get;} \n", "IsSynchronized bool IsSynchronized {get;} \n", "Keys System.Collections.ICollection Keys {get;} \n", "SyncRoot System.Object SyncRoot {get;} \n", "Values System.Collections.ICollection Values {get;}\n", "\n", "\n" ] } ], "source": [ "$dic | Get-Member -MemberType Property | select Name, Definition" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Definition \n", "---- ---------- \n", "Add void Add(System.Object key, System.Object value), void IDictionary.Add(System.Object key, System.O...\n", "Clear void Clear(), void IDictionary.Clear() \n", "Clone System.Object Clone(), System.Object ICloneable.Clone() \n", "Contains bool Contains(System.Object key), bool IDictionary.Contains(System.Object key) \n", "ContainsKey bool ContainsKey(System.Object key) \n", "ContainsValue bool ContainsValue(System.Object value) \n", "CopyTo void CopyTo(array array, int arrayIndex), void ICollection.CopyTo(array array, int index) \n", "Equals bool Equals(System.Object obj) \n", "GetEnumerator System.Collections.IDictionaryEnumerator GetEnumerator(), System.Collections.IDictionaryEnumerator...\n", "GetHashCode int GetHashCode() \n", "GetObjectData void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serializati...\n", "GetType type GetType() \n", "OnDeserialization void OnDeserialization(System.Object sender), void IDeserializationCallback.OnDeserialization(Syst...\n", "Remove void Remove(System.Object key), void IDictionary.Remove(System.Object key) \n", "ToString string ToString() \n", "\n", "\n" ] } ], "source": [ "$dic | Get-Member -MemberType Method | Format-Table Name, Definition" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Methods " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "$dic = @{}\n", "$dic | Get-Member -MemberType Method | foreach-object { $_.Name} \n", "Add\n", "Clear\n", "Clone\n", "Contains\n", "ContainsKey\n", "ContainsValue\n", "CopyTo\n", "Equals\n", "GetEnumerator\n", "GetHashCode\n", "GetObjectData\n", "GetType\n", "OnDeserialization\n", "Remove\n", "ToString\n", "\n", "\n" ] } ], "source": [ "$dic = @{}\n", "$dic | Get-Member -MemberType Method | foreach-object { $_.Name} \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$dic = @{}" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "$dic.Add('one', 1)\n", "$dic.Add('Two', 2)\n", "$dic.Add('Three', 3)\n", "\n" ] } ], "source": [ "$dic.Add('one', 1)\n", "$dic.Add('Two', 2)\n", "$dic.Add('Three', 3)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "one 1 \n", "Three 3 \n", "Two 2 \n", "\n", "\n" ] } ], "source": [ "$dic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### foreach" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "1\n", "Three\n", "3\n", "Two\n", "2\n" ] } ], "source": [ "foreach($itm in $dic.GetEnumerator())\n", "{\n", " echo $itm.Key $itm.Value\n", "}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "11\n", "Three\n", "13\n", "Two\n", "12\n" ] } ], "source": [ "foreach($key in $dic.Keys)\n", "{\n", " echo $Key ($dic[$key] + 10)\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### contains" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "one 1 \n", "Three 3 \n", "Two 2 \n", "\n", "\n" ] } ], "source": [ "$dic" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "$dic.Contains('one')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "$dic.Contains('four')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "$dic.ContainsValue(4)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "$dic.ContainsValue(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Remove and Clear" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "one 1 \n", "Three 3 \n", "Two 2 \n", "\n", "\n" ] } ], "source": [ "$dic" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$dic.Remove('Two')" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "one 1 \n", "Three 3 \n", "\n", "\n" ] } ], "source": [ "$dic" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$dic.Remove('Four')" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "one 1 \n", "Three 3 \n", "\n", "\n" ] } ], "source": [ "$dic" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$dic.Clear()" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dic = System.Collections.Hashtable\n" ] } ], "source": [ "echo \"dic = $dic\"" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dic---\n", "+++\n" ] } ], "source": [ "echo \"dic---\" $dic \"+++\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Clone" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$dic_org = @{\n", " 'first' = '1st'\n", " 'second' = '2nd'\n", " 'third' = '3rd'\n", " 'fourth' = '4th'\n", "}" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "third 3rd \n", "first 1st \n", "second 2nd \n", "fourth 4th \n", "\n", "\n" ] } ], "source": [ "$dic_org" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$dic_dist = $dic_org" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$dic_dist['first'] = '1st step'" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "third 3rd \n", "first 1st step \n", "second 2nd \n", "fourth 4th \n", "\n", "\n" ] } ], "source": [ "$dic_dist" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "third 3rd \n", "first 1st step \n", "second 2nd \n", "fourth 4th \n", "\n", "\n" ] } ], "source": [ "$dic_org" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "$dic_dist_deep = $dic_org.Clone()\n", "$dic_dist_deep['first'] = '1st'\n", "\n" ] } ], "source": [ "$dic_dist_deep = $dic_org.Clone()\n", "$dic_dist_deep['first'] = '1st'" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1st\n" ] } ], "source": [ "$dic_dist_deep['first']" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1st step\n" ] } ], "source": [ "$dic_org['first']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### hash -> string" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "$dic_for_conv = @{\n", ">> 'one' = 1\n", ">> 'two' = 2\n", ">> 'three' = 3\n", ">> }\n", ">> \n", "\n" ] } ], "source": [ "$dic_for_conv = @{\n", " 'one' = 1\n", " 'two' = 2\n", " 'three' = 3\n", "}\n" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "one 1 \n", "three 3 \n", "two 2 \n", "\n", "\n" ] } ], "source": [ "$dic_for_conv" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry\n" ] } ], "source": [ "write-host $dic_for_conv" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dic_for_conv = System.Collections.Hashtable\n" ] } ], "source": [ "echo \"dic_for_conv = $dic_for_conv\"" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dic_for_conv = \n", "\n", "Name Value \n", "---- ----- \n", "one 1 \n", "three 3 \n", "two 2 \n", "\n", "\n" ] } ], "source": [ "echo \"dic_for_conv = \" $dic_for_conv" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "System.Collections.Hashtable\n" ] } ], "source": [ "$dic_for_conv.ToString()" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "#TYPE System.Collections.Hashtable\n", "\"IsReadOnly\",\"IsFixedSize\",\"IsSynchronized\",\"Keys\",\"Values\",\"SyncRoot\",\"Count\"\n", "\"False\",\"False\",\"False\",\"System.Collections.Hashtable+KeyCollection\",\"System.Collections.Hashtable+ValueCollection\",\"System.Object\",\"3\"\n" ] } ], "source": [ "$dic_for_conv | Convertto-Csv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Convertto-Csv\n", "[![Image from Gyazo](https://i.gyazo.com/bbe8209cf4e56f25edb744092bfd0cc3.png)](https://gyazo.com/bbe8209cf4e56f25edb744092bfd0cc3)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"IsReadOnly\",\"IsFixedSize\",\"IsSynchronized\",\"Keys\",\"Values\",\"SyncRoot\",\"Count\"\n", "\"False\",\"False\",\"False\",\"System.Collections.Hashtable+KeyCollection\",\"System.Collections.Hashtable+ValueCollection\",\"System.Object\",\"3\"\n" ] } ], "source": [ "$dic_for_conv | Convertto-Csv -NoTypeInformation" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", " TypeName: System.Collections.Hashtable\n", "\n", "Name MemberType Definition \n", "---- ---------- ---------- \n", "Count Property int Count {get;} \n", "IsFixedSize Property bool IsFixedSize {get;} \n", "IsReadOnly Property bool IsReadOnly {get;} \n", "IsSynchronized Property bool IsSynchronized {get;} \n", "Keys Property System.Collections.ICollection Keys {get;} \n", "SyncRoot Property System.Object SyncRoot {get;} \n", "Values Property System.Collections.ICollection Values {get;}\n", "\n", "\n" ] } ], "source": [ "$dic_for_conv | Get-Member -MemberType Property" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"one\": 1,\n", " \"three\": 3,\n", " \"two\": 2\n", "}\n" ] } ], "source": [ "$dic_for_conv | Convertto-Json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convertto-Json\n", "[![Convertto-Json](https://i.gyazo.com/5aa4ff16abed8085317c64dd041d1c63.png)](https://gyazo.com/5aa4ff16abed8085317c64dd041d1c63)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dic_for_conv Json = \n", "{\n", " \"one\": 1,\n", " \"three\": 3,\n", " \"two\": 2\n", "}\n" ] } ], "source": [ "echo \"dic_for_conv Json = \" ; $dic_for_conv | Convertto-Json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### hash -> psobj" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$jsn = $dic_for_conv | Convertto-Json -Compress" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dic_for_conv Json = {\"one\":1,\"three\":3,\"two\":2}\n" ] } ], "source": [ "echo \"dic_for_conv Json = $jsn\" " ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "$psobj = $jsn | ConvertFrom-Json\n", "$psobj\n", "\n", "one three two\n", "--- ----- ---\n", " 1 3 2\n", "\n", "\n", "\n" ] } ], "source": [ "$psobj = $jsn | ConvertFrom-Json\n", "$psobj" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "$psobj.ToString()" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"one\",\"three\",\"two\"\n", "\"1\",\"3\",\"2\"\n" ] } ], "source": [ "$psobj | Convertto-Csv -NoType" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"one\":1,\"three\":3,\"two\":2}\n" ] } ], "source": [ "$psobj | Convertto-Json -Compre" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "$dic_for_conv | Convertto-Json | ConvertFrom-Json | set -Name psobj2\n", "\n", "\n" ] } ], "source": [ "$dic_for_conv | Convertto-Json | ConvertFrom-Json | set -Name psobj2\n" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "one three two\n", "--- ----- ---\n", " 1 3 2\n", "\n", "\n" ] } ], "source": [ "$psobj2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Serialize" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "$dic_for_serialized = @{\n", ">> 'one' = '1st'\n", ">> 'two' = '2nd'\n", ">> 'three' = '3rd'\n", ">> }\n", ">> \n", "\n" ] } ], "source": [ "$dic_for_serialized = @{\n", " 'one' = '1st'\n", " 'two' = '2nd'\n", " 'three' = '3rd'\n", "}\n" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$dic_for_serialized | Export-Clixml dic_for_serialized.clixml" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Path \n", "---- \n", "G:\\workspace\\pwshnote\n", "\n", "\n" ] } ], "source": [ "pwd" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", " Directory: G:\\workspace\\pwshnote\n", "\n", "\n", "Mode LastWriteTime Length Name \n", "---- ------------- ------ ---- \n", "-a---- 02/09/2019 13:47 990 dic_for_serialized.clixml \n", "\n", "\n" ] } ], "source": [ "dir $pwd/*.clixml" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " \n", " \n", " System.Collections.Hashtable\n", " System.Object\n", " \n", " \n", " \n", " one\n", " 1st\n", " \n", " \n", " three\n", " 3rd\n", " \n", " \n", " two\n", " 2nd\n", " \n", " \n", " \n", "\n" ] } ], "source": [ "cat dic_for_serialized.clixml" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "$dic_for_deserialized = Import-Clixml dic_for_serialized.clixml" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Name Value \n", "---- ----- \n", "one 1st \n", "three 3rd \n", "two 2nd \n", "\n", "\n" ] } ], "source": [ "$dic_for_deserialized" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### appendix" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TOPIC\n", " about_Hash_Tables\n", "\n", "SHORT DESCRIPTION\n", " Describes how to create, use, and sort hash tables in Windows PowerShell.\n", "\n", "\n", "LONG DESCRIPTION\n", " A hash table, also known as a dictionary or associative array, is a\n", " compact data structure that stores one or more key/value pairs. For\n", " example, a hash table might contain a series of IP addresses and\n", " computer names, where the IP addresses are the keys and the computer\n", " names are the values, or vice versa.\n", "\n", " In Windows PowerShell, each hash table is a Hashtable \n", " (System.Collections.Hashtable) object. You can use the properties\n", " and methods of Hashtable objects in Windows PowerShell.\n", "\n", " Beginning in Windows PowerShell 3.0, you can use the [ordered]\n", " attribute to create an ordered dictionary\n", " (System.Collections.Specialized.OrderedDictionary) in Windows PowerShell.\n", " \n", " Ordered dictionaries differ from hash tables in that the keys always\n", " appear in the order in which you list them. The order of keys in a hash\n", " table is not determined.\n", "\n", " The keys and value in hash tables are also .NET objects. They are most\n", " often strings or integers, but they can have any object type. You can also\n", " create nested hash tables, in which the value of a key is another hash table.\n", "\n", " Hash tables are frequently used because they are very efficient for finding\n", " and retrieving data. You can use hash tables to store lists and to create\n", " calculated properties in Windows PowerShell. And, Windows PowerShell has a\n", " cmdlet, ConvertFrom-StringData, that converts strings to a hash table.\n", "\n", "\n", " Syntax\n", " The syntax of a hash table is as follows:\n", "\n", " @{ = ; [ = ] ...}\n", "\n", " The syntax of an ordered dictionary is as follows:\n", "\n", " [ordered]@{ = ; [ = ] ...}\n", "\n", " The [ordered] attribute was introduced in Windows PowerShell 3.0.\n", "\n", "\n", " Creating Hash Tables\n", " To create a hash table, follow these guidelines:\n", "\n", " - Begin the hash table with an at sign (@).\n", "\n", " - Enclose the hash table in braces ({}).\n", "\n", " - Enter one or more key/value pairs for the content of the hash \n", " table.\n", "\n", " - Use an equal sign (=) to separate each key from its value.\n", "\n", " - Use a semicolon (;) or a line break to separate the\n", " key/value pairs.\n", "\n", " - Key that contains spaces must be enclosed in quotation marks.\n", " Values must be valid Windows PowerShell expressions. Strings \n", " must appear in quotation marks, even if they do not include\n", " spaces.\n", "\n", " - To manage the hash table, save it in a variable.\n", " \n", " - When assigning an ordered hash table to a variable, place\n", " the [ordered] attribute before the \"@\" symbol. If you place\n", " it before the variable name, the command fails.\n", "\n", "\n", " To create an empty hash table in the value of $hash, type:\n", "\n", " $hash = @{}\n", "\n", "\n", " You can also add keys and values to a hash table when you create it. For\n", " example, the following statement creates a hash table with three keys.\n", "\n", " $hash = @{ Number = 1; Shape = \"Square\"; Color = \"Blue\"}\n", "\n", "\n", " Creating Ordered Dictionaries\n", " You can create an ordered dictionary by adding an object of the \n", " OrderedDictiory type, but the easiest way to create an ordered \n", " dictionary is use the [Ordered] attribute.\n", "\n", " The [ordered] attribute is introduced in Windows PowerShell 3.0.\n", "\n", " Place the attribute immediately before the \"@\" symbol.\n", "\n", " $hash = [ordered]@{ Number = 1; Shape = \"Square\"; Color = \"Blue\"}\n", " \n", " You can use ordered dictionaries in the same way that you use hash tables.\n", " Either type can be used as the value of parameters that take a hash table\n", " or dictionary (iDictionary). \n", "\n", "\n", " You cannot use the [ordered] attribute to convert or cast a hash\n", " hash table. If you place the ordered attribute before the variable \n", " name, the command fails with the following error message.\n", "\n", " PS C:\\> [ordered]$hash = @{}\n", " At line:1 char:1\n", " + [ordered]$hash = @{}\n", " + ~~~~~~~~~~~~~~\n", " The ordered attribute can be specified only on a hash literal node.\n", " + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException\n", " + FullyQualifiedErrorId : OrderedAttributeOnlyOnHashLiteralNode\n", "\n", " To correct the expression, move the [ordered] attribute. \n", " PS C:\\> $hash = [ordered]@{}\n", "\n", "\n", " You can cast an ordered dictionary to a hash table, but you cannot \n", " recover the ordered attribute, even if you clear the variable and \n", " enter new values. To re-establish the order, you must remove and\n", " recreate the variable.\n", "\n", " PS C:\\> [hashtable]$hash = [ordered]@{ Number = 1; Shape = \"Square\"; Color = \"Blue\"}\n", " PS C:\\ps-test> $hash\n", "\n", " Name Value\n", " ---- -----\n", " Color Blue\n", " Shape Square\n", " Number 1\n", "\n", "\n", " Displaying Hash Tables \n", " To display a hash table that is saved in a variable, type the variable\n", " name. By default, a hash tables is displayed as a table with one column \n", " for keys and one for values.\n", "\n", " C:\\PS> $hash\n", " \n", " Name Value\n", " ---- -----\n", " Shape Square\n", " Color Blue\n", " Number 1\n", "\n", "\n", " Hash tables have Keys and Values properties. Use dot notation to display\n", " all of the keys or all of the values.\n", "\n", " C:\\PS> $hash.keys\n", " Number\n", " Shape\n", " Color\n", " \n", " C:\\PS> $hash.values\n", " 1\n", " Square\n", " Blue\n", " \n", "\n", " Each key name is also a property of the hash table, and its value is \n", " the value of the key-name property. Use the following format to display the\n", " property values.\n", "\n", " $hashtable.\n", " \n", "\n", " For example:\n", "\n", " C:\\PS> $hash.Number\n", " 1\n", "\n", " C:\\PS> $hash.Color\n", " Blue\n", " \n", " \n", " Hash tables have a Count property that indicates the number of key-value\n", " pairs in the hash table.\n", "\n", " C:\\PS> $hash.count\n", " 3\n", "\n", "\n", " Hash table tables are not arrays, so you cannot use an integer as an\n", " index into the hash table, but you can use a key name to index into the\n", " hash table. If the key is a string value, enclose the key name in quotation\n", " marks.\n", "\n", " For example:\n", "\n", " C:\\PS> $hash[\"Number\"]\n", " 1\n", "\n", "\n", " Adding and Removing Keys and Values\n", " To add keys and values to a hash table, use the following command\n", " format. \n", "\n", " $hash[\"\"] = \"\"\n", "\n", " For example, to add a \"Time\" key with a value of \"Now\" to the hash\n", " table, use the following statement format.\n", "\n", " $hash[\"Time\"] = \"Now\"\n", "\n", " You can also add keys and values to a hash table by using the Add\n", " method of the System.Collections.Hashtable object. The Add method \n", " has the following syntax:\n", "\n", " Add(Key, Value)\n", "\n", " For example, to add a \"Time\" key with a value of \"Now\" to the hash\n", " table, use the following statement format.\n", "\n", " $hash = $hash.Add(\"Time\", \"Now\")\n", "\n", " And, you can add keys and values to a hash table by using the addition \n", " operator (+) to add a hash table to an existing hash table. For example,\n", " the following statement adds a \"Time\" key with a value of \"Now\" to the \n", " hash table in the $hash variable.\n", "\n", " $hash = $hash + @{Time=\"Now\"}\n", "\n", "\n", " You can also add values that are stored in variables.\n", " \n", " $t = \"Today\"\n", " $now = (Get-Date)\n", " \n", " $hash.Add($t, $now) \n", "\n", " \n", " You cannot use a subtraction operator to remove a key/value pair from\n", " a hash table, but you can use the Remove method of the Hashtable object.\n", " The Remove method takes the key as its value. \n", "\n", " The Remove method has the following syntax:\n", "\n", " Remove(Key) \n", " \n", "\n", " For example, to remove the Time=Now key/value pair from the hash table in\n", " the value of the $hash variable, type: \n", "\n", " $hash.$Remove(\"Time\")\n", "\n", "\n", " You can use all of the properties and methods of Hashtable objects in\n", " Windows PowerShell, including Contains, Clear, Clone, and CopyTo. For\n", " more information about Hashtable objects, see \n", " \"System.Collections.Hashtable\" on MSDN.\n", "\n", "\n", "\n", " Object Types in HashTables \n", " The keys and values in a hash table can have any .NET object type, \n", " and a single hash table can have keys and values of multiple types.\n", "\n", " The following statement creates a hash table of process name strings\n", " and process object values and saves it in the $p variable.\n", "\n", "\n", " $p = @{\"PowerShell\" = (get-process PowerShell); \n", " \"Notepad\" = (get-process notepad)}\n", "\n", "\n", " You can display the hash table in $p and use the key-name properties\n", " to display the values.\n", "\n", "\n", " C:\\PS> $p\n", "\n", " Name Value\n", " ---- -----\n", " PowerShell System.Diagnostics.Process (PowerShell)\n", " Notepad System.Diagnostics.Process (notepad)\n", "\n", " \n", "\n", " C:\\PS> $p.PowerShell \n", " \n", " Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName\n", " ------- ------ ----- ----- ----- ------ -- -----------\n", " 441 24 54196 54012 571 5.10 1788 PowerShell\n", "\n", "\n", " C:\\PS> $p.keys | foreach {$p.$_.handles}\n", " 441\n", " 251\n", "\n", "\n", " The keys in a hash table can also be any .NET type. The following\n", " statement adds a key/value pair to the hash table in the $p variable.\n", " The key is a Service object that represents the WinRM service, and the\n", " value is the current status of the service.\n", "\n", " \n", " C:\\PS> $p = $p + @{(Get-Service WinRM) = ((Get-Service WinRM).Status)}\n", "\n", "\n", " You can display and access the new key/value pair by using the same\n", " methods that you use for other pairs in the hash table.\n", "\n", " C:\\PS> $p\n", "\n", " Name Value\n", " ---- -----\n", " PowerShell System.Diagnostics.Process (PowerShell)\n", " Notepad System.Diagnostics.Process (notepad)\n", " System.ServiceProcess.Servi... Running\n", "\n", " \n", " C:\\PS> $p.keys\n", " PowerShell\n", " Notepad\n", "\n", " Status Name DisplayName\n", " ------ ---- -----------\n", " Running winrm Windows Remote Management (WS-Manag...\n", "\n", " \n", " C:\\PS> $p.keys | foreach {$_.name}\n", " winrm \n", "\n", "\n", " \n", " The keys and values in a hash table can also be Hashtable objects. The\n", " following statement adds key/value pair to the hash table in the $p \n", " variable in which the key is a string, Hash2, and the value is a hash\n", " table with three key/value pairs.\n", "\n", "\n", " C:\\PS> $p = $p + @{\"Hash2\"= @{a=1; b=2; c=3}}\n", "\n", "\n", " You can display and access the new values by using the same methods.\n", "\n", " C:\\PS> $p\n", "\n", "\n", " Name Value\n", " ---- -----\n", " PowerShell System.Diagnostics.Process (PowerShell)\n", " Notepad System.Diagnostics.Process (notepad)\n", " System.ServiceProcess.Servi... Running\n", " Hash2 {a, b, c}\n", "\n", "\n", " C:\\PS> $p.Hash2\n", "\n", " Name Value\n", " ---- -----\n", " a 1\n", " b 2\n", " c 3\n", "\n", "\n", " C:\\PS> $p.Hash2.b\n", " 2\n", " \n", "\n", "\n", " Sorting Keys and Values\n", " The items in a hash table are intrinsically unordered. The key/value\n", " pairs might appear in a different order each time that you display\n", " them.\n", "\n", " Although you cannot sort a hash table, you can use the GetEnumerator\n", " method of hash tables to enumerate the keys and values, and then use\n", " the Sort-Object cmdlet to sort the enumerated values for display.\n", "\n", " For example, the following commands enumerate the keys and values\n", " in the hash table in the $p variable and then sort the keys in \n", " alphabetical order.\n", "\n", " C:\\PS> $p.GetEnumerator() | Sort-Object -Property key\n", "\n", " Name Value\n", " ---- -----\n", " Notepad System.Diagnostics.Process (notepad)\n", " PowerShell System.Diagnostics.Process (PowerShell)\n", " System.ServiceProcess.Servi... Running\n", "\n", "\n", " \n", " The following command uses the same procedure to sort the hash values in \n", " descending order.\n", "\n", "\n", " C:\\PS> $p.getenumerator() | Sort-Object -Property Value -Descending\n", "\n", " Name Value\n", " ---- -----\n", " PowerShell System.Diagnostics.Process (PowerShell)\n", " Notepad System.Diagnostics.Process (notepad)\n", " System.ServiceProcess.Servi... Running\n", "\n", "\n", " Creating Objects from Hash Tables\n", " Beginning in Windows PowerShell 3.0, you can create an object from a\n", " hash table of properties and property values.\n", "\n", " The syntax is as follows:\n", " []@{=;=}\n", "\n", " This method works only for classes that have a null \n", " constructor, that is, a constructor that has no parameters. \n", " The object properties must be public and settable.\n", "\n", " For more information, see about_Object_Creation.\n", "\n", "\n", " ConvertFrom-StringData\n", " The ConvertFrom-StringData cmdlet converts a string or a here-string of \n", " key/value pairs into a hash table. You can use the \n", " ConvertFrom-StringData cmdlet safely in the Data section of a script, \n", " and you can use it with the Import-LocalizedData cmdlet to display user\n", " messages in the user-interface (UI) culture of the current user.\n", "\n", "\n", " Here-strings are especially useful when the values in the hash table \n", " include quotation marks. (For more information about here-strings, see\n", " about_Quoting_Rules.)\n", "\n", "\n", " The following example shows how to create a here-string of the user \n", " messages in the previous example and how to use ConvertFrom-StringData\n", " to convert them from a string into a hash table.\n", "\n", "\n", " The following command creates a here-string of the key/value pairs and\n", " then saves it in the $string variable.\n", "\n", "\n", " C:\\PS> $string = @\"\n", " Msg1 = Type \"Windows\".\n", " Msg2 = She said, \"Hello, World.\"\n", " Msg3 = Enter an alias (or \"nickname\").\n", " \"@\n", "\n", " \n", " This command uses the ConvertFrom-StringData cmdlet to convert the \n", " here-string into a hash table.\n", "\n", "\n", " C:\\PS> ConvertFrom-StringData $string\n", "\n", " Name Value\n", " ---- -----\n", " Msg3 Enter an alias (or \"nickname\").\n", " Msg2 She said, \"Hello, World.\"\n", " Msg1 Type \"Windows\".\n", "\n", " For more information about here-strings, see about_Quoting_Rules.\n", "\n", "\n", "SEE ALSO\n", " about_Arrays\n", " about_Object_Creation\n", " about_Quoting_Rules\n", " about_Script_Internationalization \n", " ConvertFrom-StringData\n", " Import-LocalizedData\n", " \"System.Collections.Hashtable\" on MSDN\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] } ], "source": [ "Get-Help about_Hash_Tables" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "PowerShell", "language": "powershell", "name": "powershell" }, "language_info": { "codemirror_mode": "shell", "file_extension": ".ps1", "mimetype": "text/x-sh", "name": "powershell" } }, "nbformat": 4, "nbformat_minor": 2 }