Historic Blog. No longer active. A script repository that stopped at Powershell 4.0 is at www.rmfdevelopment.com/PowerShell_Scripts . My historic blog (no longer active) on Network Security ( http://thinking-about-network-security.blogspot.com ) is also Powershell heavy. AS of 2/27/2014 all Scripts are PS 4.0.
Monday, May 14, 2012
Creating hashtables with a timer for Key
Powershell 3.0 lets you unroll the elapsed seconds from a current start time in three lines:
$start = [datetime]::Now
function Time {([datetime]::Now - $start)}
function Timer {(Time).TotalSeconds}
while(1) {Timer;sleep 1}
PS C:\Windows\system32> while(1) {Timer;sleep 1}
1.6690955
2.6851536
3.7002116
4.7132696
5.7273276
6.7423856
You can use a new notation to create hash table with the elapsed time as the Key to almost any assigned Value. The notation is [object type]@{Name=Value}. I use the [array] object type below:
PS C:\Windows\system32> while(1) {[array]@{$(Timer)=$(get-random)};sleep 1}
Name Value
---- -----
0.2260129 1128651864
1.2440711 883153077
2.2581291 181563121
3.2711871 609183359
4.2852451 1733733473
5.2993031 1678081962
6.3133611 504757582
(Look Ma! No Value):
PS C:\Windows\system32> while(1) {[array]@{$(Timer)=$()};sleep 1}
Name Value
---- -----
24.6504099
25.6584675
26.6705254
27.7005843
28.7146423
29.7297004
30.7427583
31.7668169
PS C:\Windows\system32> while(1) {[array]@{$(Timer)=$((ps -ea 0 -module).count)};sleep 10}
Name Value
---- -----
43.4584856 3876
53.8810818 3876
64.811707 3876
75.2513041 3876
85.7329036 3876
96.1705006 3876
106.6060975 3876
PS C:\Windows\system32> while(1) {[array]@{$(Timer)=$((Timer)-(Timer))};sleep 1}
Name Value
---- -----
121.6359571 0
122.6440148 0
123.6580728 0
124.6721308 0
125.7001896 -0.000999999999990564
126.7142476 -0.00100000000000477
127.7283056 0
128.7433637 0
129.7574217 -0.00100009999999884
130.7864805 0
Wednesday, May 2, 2012
Automated Hash Generation in Powershell 3.0
After reading Shay Levy's excellent post on casting hashtables from object types in Powershell 3.0, I came up with some automated array/hashtable generation routines:
Function global:gen-num_array_hash
{
[array[]]$num_array=1..1000
$global:num_array_hash=$num_array | % {
[array[]]$rand_out=$(get-random); foreach ($i in $rand_out)` {[array]@{$_.getvalue(0)=$i.item(0)}}};break
}
Function global:gen-alpha_array_hash
{
[array[]]$alpha_array="A","B","C","D","E"
$global:alpha_array_hash=$alpha_array | % {
[array[]]$rand_out=$(get-random); foreach ($i in $rand_out)` {[array]@{$_.getvalue(0)=$i.item(0)}}};break
}
Function global:gen-num_array_hash
{
[array[]]$num_array=1..1000
$global:num_array_hash=$num_array | % {
[array[]]$rand_out=$(get-random); foreach ($i in $rand_out)` {[array]@{$_.getvalue(0)=$i.item(0)}}};break
}
Function global:gen-alpha_array_hash
{
[array[]]$alpha_array="A","B","C","D","E"
$global:alpha_array_hash=$alpha_array | % {
[array[]]$rand_out=$(get-random); foreach ($i in $rand_out)` {[array]@{$_.getvalue(0)=$i.item(0)}}};break
}
Results look like this:
The demonstration run is:
PS C:>[array[]]$names="Peter","Paul","Mary"
PS C:\> foreach ($i in $names) {[array]@{$i.getvalue(0)=1}}
Name Value
---- -----
Peter 1
Paul 1
Mary 1
PS C:\> foreach ($i in $names) {[hashtable]@{$i.getvalue(0)=1}}
Name Value
---- -----
Peter 1
Paul 1
Mary 1
More notes on how I got there...
Subscribe to:
Posts (Atom)