Skip to content

Instantly share code, notes, and snippets.

@midnightfreddie
midnightfreddie / Get-ChildItem-pipelining.ps1
Last active January 7, 2016 07:06
Testing concurrent processing of piped objects and comparing globbing versus match performance in reply to https://www.reddit.com/r/PowerShell/comments/35ua3n/running_into_massive_memory_consumption_using/cr89zuo
Function Get-Fibonacci {
# Function yoinked from http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/07/hey-scripting-guy-march-7-2010.aspx
Param([int]$max)
For($i = $j = 1; $i -lt $max) {
$i
$i,$j = ($i + $j),$i
}
}
Function Get-Stats {
param (
[Parameter(Mandatory=$True,Position=1)] $UserName,
$SharePath = "\\vsnhq1coap01mes\HomeMDB",
$CsvsToSearch = 1
)
# From CSV which lists Exchange server for each store,
# Create a lookup hash table
$StoreServer = @{}
Import-Csv -Path "store-server.csv" |
$Data = 1..10 | ForEach-Object {
New-Object psobject -Property ([ordered]@{
Col1 = (Get-Random -Maximum 1000) + 1
Col2 = (Get-Random -Maximum 1000) + 1
Col3 = (Get-Random -Maximum 1000) + 1
Col4 = (Get-Random -Maximum 1000) + 1
Col5 = (Get-Random -Maximum 1000) + 1
})
}
$data = @{}
Import-Csv namecity.csv |
ForEach-Object {
if (-not $data[$_.name]) { $data[$_.name] = @{} }
$data[$_.name]["city"] = $_.city
}
Import-Csv namestate.csv |
ForEach-Object {
# An illustration of what does and doesn't slow down Powershell and why
function Get-LotsOfData {
param (
$Rows = 10000
)
# $OneK is a 1024-character string that is just the alphabet repeating
$OneK = ( 0..1023 | ForEach-Object { [char](65 + $_ % 26) } ) -join ""
1..$Rows | ForEach-Object {
# Emit the equivalent of one row of a CSV with 5 columns
# This just puts each array in a vertical column of a table.
# The rows are not related data unless the source data is keyed on array index
$Arr1 = @(1, 2, 3, 4, 5)
$Arr2 = @(1, 2, 3)
$Arr3 = @(1, 2, 3, 4, 5, 6)
$Arr4 = @(1, 2, 3, 4, 5, 6, 7)
$Arr5 = @(1, 2)
$Arr6 = @(1, 2, 3, 4)
1..5 | ForEach-Object {
$out2 = New-Object psobject
$out2 | add-member noteproperty Name "Hi"
$out2 | add-member noteproperty Username "There $_"
$out2 | Format-Table -AutoSize
}
# Refactored from https://www.reddit.com/r/PowerShell/comments/44zkff/simple_script_for_looking_through_text_files_for/
# Lots of comments explaining changes removed. Look at the revision history in Gist or the commit messages in git for explanations.
param(
[string] $LogDirectory = "C:\Projects\Patterns\Reports\Run01\logs",
[string] $ResultFileDate = ("Results_{0}.csv" -f (MyDate)),
[string] $ExtractedFilePath = "$($LogDirectory.TrimEnd('\'))\$($ResultFileDate)",
[Array] $patterns= @(
',vsftp ,Connection closed.'
# In reply to https://www.reddit.com/r/PowerShell/comments/46bwtd/help_with_counting_in_csv_files/
# Tested against test csvs generated by the other script in this gist, and it worked without modification!
param (
$Path = ".",
$OutFile = "C:\temp\countlines.csv"
)
Get-ChildItem -Filter *.csv -Path $Path -Recurse |
# In reply to comment https://www.reddit.com/r/PowerShell/comments/47wc3x/ive_been_asked_to_come_up_with_35_questions_to/d0g8bgd
$Hash = @{
"a" = 5
"b" = 4
"c" = 3
"d" = 2
"e" = 1
}