Skip to content

Instantly share code, notes, and snippets.

View santisq's full-sized avatar

Santiago Squarzon santisq

View GitHub Profile
# from https://stackoverflow.com/a/71040823/15339544
function Tee-Host {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)] $InputObject
)
begin
{
$scriptCmd = { Out-Host }
using namespace System.Windows.Forms
using namespace System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$mainForm = [Form]::new()
$mainForm.StartPosition = 'CenterScreen'
$mainForm.Text = 'Test'
$mainForm.WindowState = 'Normal'
$mainForm.Size = [Size]::new(100, 400)
@santisq
santisq / unroll.ps1
Last active April 2, 2022 21:02
ways to unroll a nested array
$toUnroll = @(@(0,1),@(2,3),@(@(4,@(5,6)),@(7,8),9),10)
# With a Queue, unordered unrolling
function QueueUnroll {
[cmdletbinding()]
param(
[parameter(Mandatory, ValueFromPipeline)]
[object[]]$Unroll
)
(Get-Counter '\Processor(*)\% Processor Time').CounterSamples | Select-Object @(
@{
Name = 'Path'
Expression = { $_.Path.TrimStart('\').ToUpper() }
}
'InstanceName'
@{
Name = 'Load'
Expression = { ($_.CookedValue / 100).ToString('P') }
}
using namespace System.Threading
try {
$threads = 10
$RunspacePool = [runspacefactory]::CreateRunspacePool(1, $threads)
$RunspacePool.Open()
$runspace = foreach($i in 1..10) {
$ps = [powershell]::Create().AddScript({
param($i)
"Hello from runspace $i"
Get-CimInstance Win32_UserAccount | ForEach-Object {
$membership = Get-CimAssociatedInstance $_ -ResultClassName Win32_Group
[pscustomobject]@{
Computername = $Env:COMPUTERNAME
UserName = $_.Name
Memberof = $membership.Name -join ';'
Status = $membership.Name -contains 'Administrators'
}
}
[Int[]]$arrA = 1..1000
[Int[]]$arrB = 500..1500
Measure-Command {&{
$a = $arrA
$b = $arrB
Compare-Object -ReferenceObject $a -DifferenceObject $b -PassThru
}} |Select-Object @{N='Test';E={'Compare-Object'}}, TotalMilliseconds
Measure-Command {&{
$a = $arrA

A small collection specialised scripts for Active Directory.

Includes:

  • Compare-ADMemberOf
  • Get-ADSystemInfo
  • Get-GroupMemberTree
  • Get-LdapObject
  • Get-MemberOfTree
  • Test-LdapSslConnection
@santisq
santisq / yetanotherbench.ps1
Created May 7, 2022 23:21
comparing or vs chained if vs switch
$props = 0..4 | ForEach-Object { "Prop$_"}
$i = 0
$dataset = foreach($z in 0..10000) {
$tmp = @{}
$props.foreach{ $tmp[$_] = $null }
$tmp[$props[$i++ % $props.Count]] = $z
[pscustomobject] $tmp
}
$psi = [System.Diagnostics.ProcessStartInfo]@{
UseShellExecute = $false
RedirectStandardError = $true
RedirectStandardOutput = $true
FileName = 'powershell.exe'
Arguments = '$host.UI.WriteLine(''stdout''); $host.UI.WriteErrorLine(''stderr''); Start-Sleep -Seconds 5'
}
$proc = [System.Diagnostics.Process]::new()
$proc.StartInfo = $psi