Saturday, September 7, 2013

SortedList Collection and IP Address generation

I spent this morning working with the SortedList Collection and IP Address generation. SortedList maintains an IDictionary interface to a Key/Value pair collection (see  Krivayakov ) The advantage is a simple and direct reference to the last octet for Class C subnet generation and reference: 


rv -ea 0 SN
$SN = new-object System.Collections.SortedList
foreach ($i in (0..254)) {$SN.add($i,[IPAddress]"192.168.0.$i")}

foreach ($i in (0..254)) {$SN.add($i,[IPAddress]"192.168.0.$i")}
PS C:\> $SN

Name Value
---- -----
0 192.168.0.0
1 192.168.0.1
2 192.168.0.2
3 192.168.0.3
...

PS C:\> ($SN[0])

Address : 43200
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False
IsIPv6Teredo : False
IsIPv4MappedToIPv6 : False
IPAddressToString : 192.168.0.0


This makes collecting arbitrary IP ranges a simple reference to their Name/Key:

PS C:\Powershell> $b = ($SN[0,8,23]).IPAddressToString + ($SN[23..27]).IPAddressToString
PS C:\Powershell> $b
192.168.0.0
192.168.0.8
192.168.0.23
192.168.0.23
192.168.0.24
192.168.0.25
192.168.0.26
192.168.0.27

A little more complicated for multiple subnets:


rv -EA 0 SN0;rv -EA 0 SN1;rv -EA 0 SN2;
$SN0 = new-object System.Collections.SortedList
$SN1 = new-object System.Collections.SortedList
$SN2 = new-object System.Collections.SortedList
for ($i = 0; $i -ile 254;$i++){$SN0.add($i,[IPAddress]"192.168.0.$i")}
for ($i = 0; $i -ile 254;$i++){$SN1.add($i,[IPAddress]"192.168.1.$i")}
for ($i = 0; $i -ile 254;$i++){$SN2.add($i,[IPAddress]"192.168.2.$i")}

$c = ($SN0[0,8,23]).IPAddressToString + ($SN1[23..27]).IPAddressToString + ($SN2[148..154]).IPAddressToString

PS C:\> $c
192.168.0.0
192.168.0.8
192.168.0.23
192.168.1.23
192.168.1.24
192.168.1.25
192.168.1.26
192.168.1.27
192.168.2.148
192.168.2.149
192.168.2.150
192.168.2.151
192.168.2.152
192.168.2.153
192.168.2.154

Wednesday, August 7, 2013

Processing Snort Logs with Powershell 3.0



I had a group of snort logs (total about 144 MB) I wanted to process in batch. To do this I embedded a a new PSOBJECT inside a foreach loop. 'Select-string' ('sls') isn't the speediest search, but fast enough.

Function Process-SnortLogs {
$SelectPorts = foreach ($i in ($(ls snort.log.*)))
{
$a=.\snort -qr $i.Name;
New-Object PSObject -Property  @{
LogName = $i.Name;
LastWrite = $i.LastWriteTime;
Total = $($a.count);
P445 = (($a | sls -allmatches ':445').matches).count;
P443 = (($a | sls -allmatches ':443').matches).count;
P80 =   (($a | sls -allmatches ':80').matches).count;
P53 =   (($a | sls -allmatches ':53').matches).count;
}
}
}

Process-SnortLogs
$SelectPorts | Select LogName,LastWrite,P53,P80,P443,P445,Total | ft -auto

LogName              LastWrite               P53    P80  P443 P445  Total
-------              ---------               ---    ---  ---- ----  -----
snort.log.1304098553 4/29/2011 10:36:45 AM     0      0     2    0     10
snort.log.1304098783 4/29/2011 10:39:45 AM     0      0     3    0     15
snort.log.1304098850 4/29/2011 10:43:57 AM     0      4    17    0    105
snort.log.1339265058 6/9/2012 11:44:35 AM   2706  10429  7052  110 114395
snort.log.1339278740 6/9/2012 3:10:55 PM    1415    898  1232    7  26019
snort.log.1349466038 10/5/2012 1:57:33 PM   2489   6365  9465   70 149126
snort.log.1349554671 10/6/2012 1:18:00 PM     25      0     0    0    325
snort.log.1349554686 10/6/2012 3:52:18 PM   9820  39956 27617  175 482248
snort.log.1370643770 6/7/2013 5:20:12 PM    2138      8  6482    0  43290
snort.log.1373764041 7/13/2013 7:08:13 PM  30095 101367 26708  239 683162
snort.log.1373770657 7/13/2013 8:01:32 PM     21      0     5    0    545

Here I get the percentage of ports 53,80,443,445 for each snort log:

$SelectPorts | Select LogName, LastWrite, Total,`
 @{Label='%P53';Expression={[LONG]([float]($_.P53 /$_.Total) * 100)}},`
 @{Label='%P80';Expression={[LONG]([float]($_.P80 /$_.Total) * 100)}},`
 @{Label='%P443';Expression={[LONG]([float]($_.P443 /$_.Total) * 100)}},`
 @{Label='%P445';Expression={[LONG]([float]($_.P445 /$_.Total) * 100)}} |  sort -desc Total |  ft * -auto

LogName              LastWrite              Total %P53 %P80 %P443 %P445
-------              ---------              ----- ---- ---- ----- -----
snort.log.1373764041 7/13/2013 7:08:13 PM  683162    4   15     4     0
snort.log.1349554686 10/6/2012 3:52:18 PM  482248    2    8     6     0
snort.log.1349466038 10/5/2012 1:57:33 PM  149126    2    4     6     0
snort.log.1339265058 6/9/2012 11:44:35 AM  114395    2    9     6     0
snort.log.1370643770 6/7/2013 5:20:12 PM    43290    5    0    15     0
snort.log.1339278740 6/9/2012 3:10:55 PM    26019    5    3     5     0
snort.log.1373770657 7/13/2013 8:01:32 PM     545    4    0     1     0
snort.log.1349554671 10/6/2012 1:18:00 PM     325    8    0     0     0
snort.log.1304098850 4/29/2011 10:43:57 AM    105    0    4    16     0
snort.log.1304098783 4/29/2011 10:39:45 AM     15    0    0    20     0
snort.log.1304098553 4/29/2011 10:36:45 AM     10    0    0    20     0


Monday, July 29, 2013

Current Process Memory

updated  08/04/2013 -RMF

Below is an example of the (clumsy) scripting workaround I will use because I always have a difficult time getting 'add-member' to work with membertype ScriptMethod. What I want here is WS, PM, VM for each current process, but also the differences (e.g. VM - PM, PM - WS):

Function CurrentMem {
[PSObject]$Memory=
ps * | Select Name, ID,`
@{Name='WS_MB';Expression={[INT](($_.WorkingSet)/1MB)}},`
@{Name='PriMemMB';Expression={[INT](($_.PrivateMemorySize64)/1MB)}}, `
@{Name='VirMemMB';Expression={[INT](($_.VirtualMemorySize64)/1MB)}}
$Memory | Sort -desc VirMemMB | 
Select *,  @{Name='VM-PM';Expression={[INT]($_.VirMemMB - $_.PriMemMB)}}, `
@{Name='PM-WS';Expression={[INT]($_.PriMemMB - $_.WS_MB)}}
}

So then  I have:

PS C:\> $CurrentMem=CurrentMem
PS C:\> ($CurrentMem | sort -desc VM-PM)[0..10]  | ft -auto

Name         Id WS_MB PriMemMB VirMemMB VM-PM PM-WS
----         -- ----- -------- -------- ----- -----
powershell 7300    74       66      608   542    -8
powershell 5408     3       63      603   540    60
svchost     536    39       39      575   536     0
chrome     4748   192      194      658   464     2
bash       6348     0        8      451   443     8
powershell 1536   176      185      620   435     9
syslogd    2112     1        5      432   427     4
cygrunsrv  1948     0        6      427   421     6
chrome     2956   265      323      742   419    58
taskhost   3896     6       15      394   379     9
svchost    1288    23       26      386   360     3

A more advanced version allows for a more granular look at processes:


Function Get-CM {
[PSObject]$Memory=
ps * | Select Name, ID, Company, HandleCount,`
@{Name='WorkSetMB';Expression={[LONG](($_.WorkingSet)/1MB)}},`
@{Name='PMS64Bytes';Expression={[LONG](($_.PagedMemorySize64))}},`
@{Name='NPMS64Bytes';Expression={[LONG](($_.NonpagedSystemMemorySize64))}},`
@{Name='PriMem64MB';Expression={[LONG](($_.PrivateMemorySize64)/1MB)}},`
@{Name='VirMem64MB';Expression={[LONG](($_.VirtualMemorySize64)/1MB)}}
$Memory | Sort -desc VirMem64MB | 
Select *,  @{Name='VMem-PMem.MB';Expression={[LONG]($_.VirMem64MB - $_.PriMem64MB)}},`
@{Name='PMem-NPMem.MB';Expression={[LONG](($_.PMS64Bytes - $_.NPMS64Bytes)/1MB)}},`
@{Name='PriMem-WorkSet.MB';Expression={[LONG]($_.PriMem64MB - $_.WorkSetMB)}}
}

So that here I can look at all process with over 500 handles sorted by a decreasing amount of PagedMemorySystem64 :

PS C:\ps1> get-cm | where HandleCount -gt 500 | sort -desc PMS64Bytes | fl *


Name              : chrome
Id                : 6636
Company           : The Chromium Authors
HandleCount       : 2769
WorkSetMB         : 180
PMS64Bytes        : 268677120
NPMS64Bytes       : 141712
PriMem64MB        : 256
VirMem64MB        : 672
VMem-PMem.MB      : 416
PMem-NPMem.MB     : 256
PriMem-WorkSet.MB : 76

Name              : chrome
Id                : 3532
Company           : The Chromium Authors
HandleCount       : 796
WorkSetMB         : 63
PMS64Bytes        : 220286976
NPMS64Bytes       : 69632
PriMem64MB        : 210
VirMem64MB        : 699
VMem-PMem.MB      : 489
PMem-NPMem.MB     : 210
PriMem-WorkSet.MB : 147

Name              : svchost
Id                : 456
Company           : Microsoft Corporation
HandleCount       : 727
WorkSetMB         : 137
PMS64Bytes        : 162627584
NPMS64Bytes       : 31784
PriMem64MB        : 155
VirMem64MB        : 315
VMem-PMem.MB      : 160
PMem-NPMem.MB     : 155
PriMem-WorkSet.MB : 18

...

or (click to enlarge)






Monday, June 17, 2013

Powershell 3.0 CIM snippet : Properties of IPEnabled Adapters

$count = ((Get-CimInstance -class Win32_NetworkAdapterConfiguration).count - 1)

$Adapters = 0..$count | % {(Get-CimInstance -class Win32_NetworkAdapterConfiguration)[$PSItem]}
$Adapters
$IPEnabled=($Adapters | ? {$_.IPEnabled -eq "True"})
$IPEnabled | fl *

Monday, April 15, 2013

Functions for sum of directory or file size


# file
function file.sum($x) {
rv -ea 0 total;
foreach ($i in ((ls $x -Force) | Select Length)) {[int64]$total+=$i[0].Length};
$total /1MB;
}

#dir
function dir.sum {
rv -ea 0 total;
foreach ($i in ((ls -Force) | Select Length)) {[int64]$total+=$i[0].Length};
$total /1GB;
}

#dir recurse
# really chews through memory for directories of any size
function dir.sum.recurse {
rv -ea 0 total;
foreach ($i in ((ls -Force -Recurse) | Select Length)) {[int64]$total+=$i[0].Length};
$total /1GB;
}

Monday, January 21, 2013

Can you see the pattern....?


Function global:gen-num_array_hash
{
[array[]]$num_array=1..10000
$global:num_array_hash=$num_array | %  {
[array[]]$rand_out=$(get-random); foreach ($i in $rand_out) {
[array]@{$_.getvalue(0)=$i.item(0)}}};break
}
rv -ea 0 hashdata
$hashdata=$num_array_hash
Chart-Hashdata fastpoint


Friday, January 4, 2013

[math]::IEEERemainder((100/$PSItem),($PSItem/100))

Tonight's intellectual curiosity produces the charts below.

function XYZ {
-100..100 | % {
try {foreach ($i in [array[]]([math]::IEEERemainder((100/$PSItem),($PSItem/100)))) {[ordered]@{$PSItem=$($i.item(0))}}} `
catch [System.Management.Automation.RuntimeException] {}}
}

Monday, December 3, 2012

A defect for 'get-random'

In PS 3.0, 'Get-random' doesn't return an error value for a $null variable without the use of  '-inputobject'.  I came across this writing a  function so my third grader could pick up her arithmetic skills. The function has '$Mixed' as such:

[array]$Mixed= % {
0..($questions_per_worksheet - 1) | % {write "2 $operator1 $(get-random -max $random_max -min $random_min) = "} 
0..($questions_per_worksheet - 1) | % {write "4 $operator2 $(get-random -max $random_max -min $random_min) = "} 
0..($questions_per_worksheet - 1) | % {write "5 $operator3 $(get-random -max $random_max -min $random_min) = "} 
0..($questions_per_worksheet - 1) | % {write "10 $operator4 $(get-random -max $random_max -min $random_min) = "} 


Wednesday, October 24, 2012

Data Analytics with Powershell Part I

Below is some cruft I am using to analyze PDC data for candidates in my local WA elections. This is part of a larger project I am working on to use Powershell 3.0 as a data analytic solution.  I find working with data in Powershell 3.0 somewhat tricky,  sometimes limited, but also no less straightforward than SQL or R.  Although, Powershell can sometimes be a little frustrating to work with , I rather like being able to craft my own 'data analytic'  solution from the console. I found myself close to the data while working with PS 3.0. Certain techniques produced surprisingly rapid and illuminating results. Take the query below. After having imported to a variable ('$NM') a candidates data from CSV, in one line of code I am able to exclude all WA state contributions; then use 'group-object' to list all 'out of state' contributors,state,amounts in a sort.


$NM | Where State -ne WA | group -property Contributor,State,Amount -noelement | sort -desc Count,Name | ft -auto -wrap

Count Name
----- ----
    1 WEBB LISA, MT, 100
    1 VASKAS JANET, PA, 100
    1 VASKAS ALAN, PA, 100
    1 TURNER ZACHARY M, CO, 900
    1 TERESA JUDITH, WV, 50
    1 SOTO JLEANA, CA, 100
    1 MCCLENDON SUSAN, GA, 500
    1 ATU, DC, 900


Friday, August 31, 2012

Memory Stats in the Title Bar (Part II)


Two different attempts to update memory stats in the title bar in the console background. The 'start-job' script won't work because it updates the new Powershell instance the job is running in. The 'runspace' script works because it updates the existing console.

# with Start-Job
# Doesn't Work because it updates a hidden Powershell Job window
start-job -InitializationScript {

function Global:Set-title {
$PSID=([System.Diagnostics.Process]::GetCurrentProcess()).Id
$MemStats=ps -id $PSID | Select `
@{Name='ThreadCount';Expression={($_.Threads).count}}, `
@{Name='WorkSetMB';Expression={[int](($_.WorkingSet64)/1MB)}}, `
@{Name='VirMemMB';Expression={[int](($_.VirtualMemorySize64)/1MB)}}, `
@{Name='PriMemMB';Expression={[int](($_.PrivateMemorySize64)/1MB)}}, `
@{Name='PagedMemMB';Expression={[int](($_.PagedMemorySize64)/1MB)}}, `
@{Name='NonPagedMemKB';Expression={[int](($_.NonPagedSystemMemorySize64)/1KB)}}

$Title=write "Last_Title_Stats: Time: $([datetime]::now) Version: $((get-host).Version.Major) SessionHours: $([int]([datetime]::now - (ps -id $PSID).Starttime).totalhours) Memory: $($Memstats) GC_MB: $([int]([GC]::gettotalmemory(1)/1MB))"
[console]::set_title($Title)
}

} `

-scriptblock {while(1) {set-title;sleep -s 5}}


# With Runspace
# Works because the new runspace refers to the existing Powershell window
$set_title=

{

function Global:Set-title {
$PSID=([System.Diagnostics.Process]::GetCurrentProcess()).Id
$MemStats=ps -id $PSID | Select `
@{Name='ThreadCount';Expression={($_.Threads).count}}, `
@{Name='WorkSetMB';Expression={[int](($_.WorkingSet64)/1MB)}}, `
@{Name='VirMemMB';Expression={[int](($_.VirtualMemorySize64)/1MB)}}, `
@{Name='PriMemMB';Expression={[int](($_.PrivateMemorySize64)/1MB)}}, `
@{Name='PagedMemMB';Expression={[int](($_.PagedMemorySize64)/1MB)}}, `
@{Name='NonPagedMemKB';Expression={[int](($_.NonPagedSystemMemorySize64)/1KB)}}

$Title=write "Last_Title_Stats: Time: $([datetime]::now) Version: $((get-host).Version.Major) SessionHours: $([int]([datetime]::now - (ps -id $psid).Starttime).totalhours) Memory: $($Memstats) GC_MB: $([int]([GC]::gettotalmemory(1)/1MB))"
[console]::set_title($Title)
}

while(1) {set-title;sleep -s 5}

}

$ST_Runspace = [PowerShell]::Create().AddScript($set_title)
$Begin_Set_Title = $ST_Runspace.BeginInvoke()

# Commands to query,stop,dispose the titlebar update:
# $ST_Runspace.runspace
# $Begin_Set_Title
# $Stop_Set_Title = $ST_Runspace.Stop()
# $Dispose_Set_Title = $ST_Runspace.Dispose()

Monday, August 27, 2012

Memory Stats in the Title Bar (Part I)

#This function puts memory stats for the first instance of Powershell in your title bar.
function Global:Set-title {
$MemStats=(ps powershell)[0] | Select `
@{Name='ThreadCount';Expression={($_.Threads).count}}, `
@{Name='WorkSetMB';Expression={[int](($_.WorkingSet64)/1MB)}}, `
@{Name='VirMemMB';Expression={[int](($_.VirtualMemorySize64)/1MB)}}, `
@{Name='PriMemMB';Expression={[int](($_.PrivateMemorySize64)/1MB)}}, `
@{Name='PagedMemMB';Expression={[int](($_.PagedMemorySize64)/1MB)}}, `
@{Name='NonPagedMemKB';Expression={[int](($_.NonPagedSystemMemorySize64)/1KB)}}
# one line below...
$Title=write "Last_Title_Stats: Time: $([datetime]::now) Version: $((get-host).Version.Major) SessionHours: $([int]([datetime]::now - (ps powershell)[0].Starttime).totalhours) Memory: $($Memstats) GC_MB: $([int]([GC]::gettotalmemory(1)/1MB))"
[console]::set_title($Title)
}

# To see...click on this image to enlarge.:


Monday, August 20, 2012

Clearing up variables and memory in Powershell 3.0

Some GC lines can help you check and clear up memory  in Powershell 3.0:

# Find out how much memory is being consumed by your Sesssion:
[System.gc]::gettotalmemory("forcefullcollection") /1MB
# Force a collection of memory by the garbage collector:
[System.gc]::collect()
# Dump all variables not locked by the system:
foreach ($i in (ls variable:/*)) {rv -ea 0 -verbose $i.Name}
#Check memory usage again and force another collection:
[System.gc]::gettotalmemory("forcefullcollection") /1MB
[System.gc]::collect()
#Check Memory once more:
[System.gc]::gettotalmemory("forcefullcollection") /1MB

#You can examine the difference before and after with:
ps powershell* | Select *memory*

or with something more complicated:

ps powershell* | Select *memory* | ft -auto `
@{Name='VirMemMB';Expression={($_.VirtualMemorySize64)/1MB}}, `
@{Name='PriMemMB';Expression={($_.PrivateMemorySize64)/1MB}}


    VirMemMB     PriMemMB
    --------     --------
1548.3671875 432.43359375
 603.9765625   58.5234375

Sunday, August 5, 2012

Some notes on querying variables in Powershell 3.0


PS C:\ps1> ls variable:/*


Name                           Value
----                           -----
null
false                          False
true                           True
MaximumErrorCount              256
MaximumVariableCount           4096
MaximumFunctionCount           4096
MaximumAliasCount              4096
MaximumDriveCount              4096
Error                          {Cannot find drive. A drive with the name 'variablwe' does not exist., System.Management.Automation.P...
PSDefaultParameterValues       {}
$                              variablwe:/*
^                              ls
StackTrace                        at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object inpu...
ErrorView                      NormalView
LASTEXITCODE                   0
PSEmailServer
....


PS C:\Windows\system32> ((ls variable:/) | gm *).typename | get-unique
System.Management.Automation.PSVariable
System.Management.Automation.QuestionMarkVariable
System.Management.Automation.LocalVariable
System.Management.Automation.SessionStateCapacityVariable
System.Management.Automation.NullVariable



Thursday, July 26, 2012

"Big Math"[1] : Solver Foundation and F#

It would be powerful for 'big math' users of Powershell if we could integrate F# and Microsoft's Solver Foundation into Powershell 3.0. Microsoft's Solver Foundation provides for "Programming in the Optimization Modeling Language (OML), in C# imperatively, in F# functionally, or in any .NET language".  
So will it work with a DLR language like Powershell 3.0 Beta?  I don't know yet but here is some exploratory script.:


Thursday, July 19, 2012

"Big Math"[0] : [BigInt] to the rescue!


 BigInt to the  rescue.  Only with .NET 4.0. From PS 3.0 Beta:


Normally:


${2^1023}=[math]::POW(2,1023)
${2^1023}
8.98846567431158E+307
${2^1024}=[math]::POW(2,1024)
${2^1024}
Infinity
${2^1023} * 1.99999999999999988
1.79769313486232E+308
$AMAX=${2^1023} * 1.99999999999999988
[Double]::MaxValue
1.79769313486232E+308
[Double]::MaxValue - $AMAX
0


...and thus integer factorial is limited to 170:

Friday, June 29, 2012

Switch Array Automation



I am working with processing IP Address arrays for some automation I am doing with procmon exported results in Powershell.

#arrays
[array[]]$IParray="RMFHOPE","239.255.255.250"
[array[]]$IPAddress = "239.255.255.250"

# works as expected
$IPAddress = "239.255.255.250"
switch ($IPAddress){
RMFHOPE  {write "0"}
239.255.255.250 {write "1"}
}

# now working; required string conversion
[array[]]$IParray="RMFHOPE","239.255.255.250"
[array[]]$IPAddress = "239.255.255.250"
$IPC= ($IParray.count)- 1

0..$IPC | % -process {
[string]$IP=$IPArray[$_] #needed string conversion here
switch ($IPAddress){
($IP) {write $IPAddress}
}
}

# prints IP Address
foreach ($i in (0..$IPC)) {switch ($IPAddress) {$IPArray[$i] {write $IPAddress}}}

# prints array item place
foreach ($i in (0..$IPC)) {switch ($IPAddress) {$IPArray[$i] {$i}}}

#stores and prints all $IPArray then all $IPArray item place
#arrays
[array[]]$IParray="RMFHOPE","239.255.255.250"
[array[]]$IPAddress = "239.255.255.250"

$IPC= ($IParray.count) - 1

#All $IPArray
$IPArray | % -process {
[array[]]$IPAddress = $_
$a=foreach ($i in (0..$IPC)) {switch ($IPAddress) {$IPArray[$i] {$IPArray[$i]}}}
$a
}

#All $IPArray item place
$IPArray | % -process {
[array[]]$IPAddress = $_
$a=foreach ($i in (0..$IPC)) {switch ($IPAddress) {$IPArray[$i] {$i}}}
$a
}

Friday, June 1, 2012

Subtraction worksheet

A somewhat irrelevant post. I had to quickly produce a sheet of 100 random subtraction drills for my 2nd grader. I can think of 10 or 20 ways to make this more compact and useful.:


0..19 | % {write "$(get-random -max 21 -min 14) - $(get-random -max 6 -min 0)"} | out-file mixed100.txt
0..19 | % {write "$(get-random -max 16 -min 9) - $(get-random -max 6 -min 0) = "} | out-file -append mixed100.txt
0..19 | % {write "$(get-random -max 11 -min 6) - $(get-random -max 6 -min 0) = "} | out-file -append mixed100.txt
0..19 | % {write "$(get-random -max 21 -min 14) - $(get-random -max 11 -min 6) = "} | out-file -append mixed100.txt
0..19 | % {write "$(get-random -max 15 -min 9) - $(get-random -max 9 -min 3) = "} | out-file -append mixed100.txt



PS C:\Windows\system32> more mixed100.txt
19 - 3 =
16 - 5 =
19 - 1 =
18 - 5 =
18 - 2 =
17 - 1 =
16 - 0 =
14 - 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
}

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...

Monday, April 23, 2012

On using BinarySearch and IComparer in Powershell:Part I

The Binary Search algorithm is exposed in Powershell v3.0 . It is not clear to me how to implement System.Collections.IComparer  as of yet. However, even without IComparer, Powershell's BinarySearch  has some usefulness.  Here are the overloads: 

 [Array]::BinarySearch.OverloadDefinitions
static int BinarySearch(array array, System.Object value)
static int BinarySearch(array array, int index, int length, System.Object value)
static int BinarySearch(array array, System.Object value, System.Collections.IComparer comparer)
static int BinarySearch(array array, int index, int length, System.Object value, System.Collections.IComparer comparer)
static int BinarySearch[T](T[] array, T value)
static int BinarySearch[T](T[] array, T value, System.Collections.Generic.IComparer[T] comparer)
static int BinarySearch[T](T[] array, int index, int length, T value)
static int BinarySearch[T](T[] array, int index, int length, T value, System.Collections.Generic.IComparer[T] comparer)