I am so confused. I can't understand why a foreach in Powershell behaves so differently than C#.
This simple code produces no output:
#{a=1; b=2} | %{ $_.key }
An approximation of this I did in Linqpad using C# gives the output I'd expect:
var ht = new Hashtable();
ht["a"] = 1;
ht["b"] = 2;
foreach (DictionaryEntry kvp in ht)
{
kvp.Key.Dump();
}
// output
a
b
If I enumerate the Keys member of the hashtable, I can do what I need. But I don't understand why enumerating the hashtable itself doesn't return the key-value pairs. In fact it seems to return the object itself:
> #{a=1; b=2} | %{ $_.gettype() }
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Hashtable System.Object
What's Powershell doing here?
The PowerShell pipeline will "unroll" arrays and collections (one level), but not hash tables. If you want to enumerate the key-value pairs of a hashtable in PowerShell, call the .getenumerator() method:
$ht = #{a=1;b=2}
$ht.GetEnumerator()
Name Value
---- -----
a 1
b 2
Related
I am using RestAPI access to azure. I am getting a list of changesets for a build ID.
I would like to sort the object type with increasing Changeset number.
The type I receive for $changeset.GetType():
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSCustomObject System.Object
This is the output $changeset:
count value
----- -----
50 {#{id=68.......
Therefore, I checked the type of value $changeset.value.GetType():
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
And afterwards I checked the type of an element:
$changeset.value[0].GetType():
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSCustomObject System.Object
I did try Sort-Object with ascending and descending, but it does not change the order:
$changeset = $changeset | sort-object { $_.value.id } -desc
I would like to keep the structure as it is to not break something. There are lots of properties within the component.
Without an example working dataset, it's hard create an absolute working solution for your use case, however you could sort the data using the below:
$changeset | Select-Object -ExpandProperty value | Sort-Object -Property id
This would return all of the objects under the immediate property value and sorted on the property id.
If you want to retain the object as a whole with its present structure:
$changeset.value = #($changeset.value | Sort-Object -Descending -Property Id)
That is:
You must apply the Sort-Object call to the .value property, which contains the array of [pscustomobject] instances you want to sort by their .Id property values.
Enclosing the command in #(...), the array-subexpression operator, ensures that the sort objects are treated as an array, even if situationally only one object may be present.
Assigning the results back to $changeset.value replaces the original array with a new, sorted array.
I'm trying to save values between instances of the script running.
I'm doing that by reading a text file at the beginning of the script, and then overwriting that file with the new values at the end of the script.
tracker.txt:
x=1
y=4
z=3
I'm reading the script using:
Get-Content "$Root\tracker.txt" | Foreach-Object{
$Position = $_.Split("=")
New-Variable -Name $Position[0] -Value $Position[1]
}
Unfortunately my $x $y and $z variables are being interpreted as a string instead of an integer.
Looking up New-Variable parameters, it doesn't seem like I can specify the value type.
Also I tried:
New-Variable -Name $Position[0] -Value [int]$Position[1]
And:
New-Variable -Name $Position[0] -Value ($Position[1] + 0)
But both did not work as expected.
How can I set these variables as an integer? I'm trying to use them later in a loop and that keeps failing because the variable can't be a string.
In order to save values, consider using Powershell's own object serialization. That is, Export-Clixml and Import-Clixml cmdlets. When an object is serialized, its contents are written on a file. In addition to values, data types are there too.
Handling multiple variables is easier, if those are stored in a collection such as a hash table. Like so,
# Save some values in a hash table
$myKeys =#{ "a" = 1; "b" = 2 }
$myKeys["a"]
1
# Check variable a's type. Int32 is as expected
$myKeys["a"].gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
# Serialize the hash table
Export-Clixml -Path keys.xml -InputObject $myKeys
# Create a new hash table by deserializing
$newKeys = Import-Clixml .\keys.xml
# Check contents
$newkeys["a"]
1
# Is the new a also an int32? Yes, it is
$newkeys["a"].gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
The keys.xml is, as expected an XML file. Check out its contents with, say, Notepad to see how the objects are stored. When working with complex objects, -Depth switch needs to be used. Otherwise, serialization saves only two levels of nesting, and that'll break complex objects.
I will use this:
(Get-Content tracker.txt) | foreach {
invoke-expression "[int]`$$($_.split("=")[0])=$($_.split("=")[1])"
}
It will create three variables with type int32.
To answer your specific question, you can force it like this.
#'
var1=1
var2=2
'# | out-file "$Root\tracker.txt"
Get-Content "$Root\tracker.txt" | Foreach-Object{
$Position = $_.Split("=")
New-Variable -Name $Position[0] -Value ([int]$Position[1])
}
$var1.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
However I really like this approach.
Create a template
$template = #'
{name*:var1}={[int]value:1}
{name*:var2}={[int]value:2}
'#
Now use ConvertFrom-String and assign to $data
#'
test1=1
test2=2
test3=3
test4=4
test5=5
'# | ConvertFrom-String -TemplateContent $template -OutVariable data
Let's create our variables now. Very easy to read.
$data | foreach {New-Variable -Name $_.name -Value $_.value}
Get-Variable test*
Name Value
---- -----
test1 1
test2 2
test3 3
test4 4
test5 5
And most importantly, they are int.
$test1.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
Putting it all together and pulling from a file
$template = #'
{name*:var1}={[int]value:1}
{name*:var2}={[int]value:2}
'#
Get-Content "$Root\tracker.txt" -raw |
ConvertFrom-String -TemplateContent $template |
foreach {New-Variable -Name $_.name -Value $_.value}
I am refactoring some old(ish) code, and I have used two approaches for creating some arrays and ordered dictionaries, for example...
[Collections.Specialized.OrderedDictionary]#{}
and
New-Object Collections.Specialized.OrderedDictionary
I wonder which of these two approaches to instantiating arrays and hashtables/dictionaries is better? FWIW, I need code that that is version agnostic from PS2.0 thru current. Performance is a secondary concern.
FWIW, it looks like the former is MUCH better from a performance standpoint. I tried
Measure-Command {
foreach ($i in 1..10000) {
$array1 = [Collections.Specialized.OrderedDictionary]#{}
}
}
Measure-Command {
foreach ($i in 1..10000) {
$array2 = New-Object Collections.Specialized.OrderedDictionary
}
}
and got 34 milliseconds vs 278 milliseconds. Of course I am not creating 10K instances, nor is performance the main priority, not is 278 milliseconds poor performance even if that was a priority. But it sure shows there is a big difference in the process, even if the end result is actually the same.
When I do the following:
cls
write-host "array1:"
$array1 = [System.Collections.Specialized.OrderedDictionary]#{}
$array1.GetType()
(Measure-Command -Expression {1..10000 | ForEach-Object { $array1.Add($_,"") }}).Milliseconds
write-host
write-host
write-host "array2:"
$array2 = New-Object System.Collections.Specialized.OrderedDictionary
$array2.GetType()
(Measure-Command -Expression {1..10000 | ForEach-Object { $array2.Add($_,"") }}).Milliseconds
I get, e.g., the following, i.e., I think they are the same type so I wouldn't expect any difference, performance or otherwise:
array1:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True OrderedDictionary System.Object
492
array2:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True OrderedDictionary System.Object
500
BTW, as of PS 3.0, $array3=[ordered]#{} gives you the same type+++:
array3
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True OrderedDictionary System.Object
+++
About Hash Tables
Beginning in PowerShell 3.0, you can use the [ordered] attribute to
create an ordered dictionary
(System.Collections.Specialized.OrderedDictionary) in PowerShell.
Consider this code:
$a = '[{"a":"b"},{"c":"d"}]'
"Test1"
$a | ConvertFrom-Json | ForEach-Object { $_.GetType() }
"Test2"
$b = $a | ConvertFrom-Json
$b | ForEach-Object { $_.GetType() }
This produces the following output:
Test1
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
Test2
True False PSCustomObject System.Object
True False PSCustomObject System.Object
Evidently, if we use a temporary variable, whatever is passed down the pipeline is not the same thing that is passed if we do not use one.
I would like to know what the rules that powershell uses for automatic array wrapping / unwrapping are, and if the using a temp var the best course of action if we need to iterate through a json array.
Update 1
Logically ConvertFrom-Json should return an array with the input given and ForEach-Object should iterated on the said array. However in the first test this does not happen. Why?
Update 2
Is it possible that it's ConvertFrom-Json specific? Like bug/issue?
There is only one rule with regard to pipeline items' unwrapping: all arrays and collections written to the pipeline are always getting unwrapped to the items ("unwrapped one level down" or "unwrapped in a non-recursive fashion" would be more correct statement but for the sake of simplicity we are not going to consider nested arrays so far).
There is still a possibility to override this behavior by using unary comma operator:
$a = 1,2
$a | ForEach-Object{ $_.GetType() }
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
True True Int32 System.ValueType
,$a | ForEach-Object{ $_.GetType() }
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
In the second case Powershell pipeline engine unwrapped $a but afterwards the result got wrapped back to array by , operator.
As to ConvertFrom-Json case I personally believe that its observed behavior is more predictable as it allows you to capture JSON arrays as a whole by default.
If you are interested in the details, the function Get-WrappedArray in the code below imitates ConvertFrom-Json's behavior:
function Get-WrappedArray {
Begin { $result = #() }
Process { $result += $_ }
End { ,$result }
}
$a | Get-WrappedArray | ForEach-Object{ $_.GetType() }
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
$b = $a | Get-WrappedArray
$b | ForEach-Object{ $_.GetType() }
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
True True Int32 System.ValueType
What is the proper way to create an array, hashtable and dictionary?
$array = [System.Collections.ArrayList]#()
$array.GetType() returns ArrayList, OK.
$hashtable = [System.Collections.Hashtable]
$hashtable.GetType() returns RuntimeType, Not OK.
$dictionary = ?
How to create a dictionary using this .NET way?
What is the difference between dictionary and hashtable? I am not sure when I should use one of them.
The proper way (i.e. the PowerShell way) is:
Array:
> $a = #()
> $a.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
Hashtable / Dictionary:
> $h = #{}
> $h.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Hashtable System.Object
The above should suffice for most dictionary-like scenarios, but if you did explicitly want the type from Systems.Collections.Generic, you could initialise like:
> $d = New-Object 'system.collections.generic.dictionary[string,string]'
> $d.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Dictionary`2 System.Object
> $d["foo"] = "bar"
> $d | Format-Table -auto
Key Value
--- -----
foo bar
If you want to initialize an array you can use the following code:
$array = #() # empty array
$array2 = #('one', 'two', 'three') # array with 3 values
If you want to initialize hashtable use the following code:
$hashtable = #{} # empty hashtable
$hashtable2 = #{One='one'; Two='two';Three='three'} # hashtable with 3 values
Hashtable and dictionary in Powershell is pretty much the same, so I suggest using hashtable in almost all cases (unless you need to do something in .NET where Dictionary is required)