Skip to content

Instantly share code, notes, and snippets.

@Luxter77
Created August 25, 2025 03:31
Show Gist options
  • Select an option

  • Save Luxter77/38d7ed5beed01ddfe735e19a96ecbac3 to your computer and use it in GitHub Desktop.

Select an option

Save Luxter77/38d7ed5beed01ddfe735e19a96ecbac3 to your computer and use it in GitHub Desktop.
Clear-Host
function Chunk-Objects {
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[psobject]$InputObject,
[ValidateRange(1, 100000)]
[int]$ChunkSize = 255
)
begin {
$counter = 0
# Set up array that will act as buffer
$chunk = [object[]]::new($ChunkSize)
}
process {
# Add input object to next available slot in array
$chunk[$counter++] = $InputObject
if($counter -eq $ChunkSize){
# If we've filled the buffer, output it as a new chunk
Write-Output $chunk -NoEnumerate
# Reset counter and buffer
$counter = 0
$chunk = [object[]]::new($ChunkSize)
}
}
end {
if($counter){
# There's no more input but we have some data left over still, output it
Write-Output $chunk[0..($counter-1)] -NoEnumerate
}
}
}
Import-Module ActiveDirectory
$name = "OUT"
Write-Host "Pulling targets from IPPY.csv"
$Computers = Import-Csv "IPPY.csv" | ForEach-Object { $_.IP }
Write-Host "Done pulling."
$cc = $Computers.count
$bs = 300
$to = 15
$t = [Math]::ceiling($cc / $bs)
$ft = (Get-Date).AddSeconds($cc * 14 / $bs)
$st = Get-Date
$no = Get-Date
$c = 0
$d = 0
[PSCustomObject]@{
"PercentComplete" = $p
"NowIts" = $no
"CurrentBatch" = $c
"Done" = $d
"InbatchesOf" = $bs
"TotalBatches" = $t
"FinalTime" = $ft
"Total" = $cc
"OriginalEstimatedFinalTime" = $ft
"EstimatedFinalTime" = ($no + (New-TimeSpan -Seconds (($cc - $c) * (($no - $st).TotalSeconds / 1)))).ToString()
} | Format-List
New-Item $name -itemType Directory -ErrorAction 'silentlycontinue'
Remove-Item "$name\*.json" -ErrorAction 'silentlycontinue'
Set-Location $name
Clear-Host
$Computers | Chunk-Objects -ChunkSize $bs | ForEach-Object {
$c = $c + 1
$p = $c / $t
$d = ($c * $bs)
$no = Get-Date
Clear-Host
Clear-Host
[PSCustomObject]@{
"PercentComplete" = $p
"NowIts" = $no
"CurrentBatch" = $c
"Done" = $d
"InbatchesOf" = $bs
"TotalBatches" = $t
"FinalTime" = $ft
"Total" = $cc
"OriginalEstimatedFinalTime" = $ft
"EstimatedFinalTime" = ($no + (New-TimeSpan -Seconds (($cc - $c) * (($no - $st).TotalSeconds / $c)))).ToString()
} | Format-List
$_ | ForEach-Object -UseNewRunspace -ThrottleLimit $bs -Parallel {
$rname = $_.ToUpper()
if (Test-Connection -TargetName "$rname" -Quiet -TimeoutSeconds 5 -TcpPort 139) {
"InitConn $rname" | Write-Host
$netview = (net.exe view "\\$rname" /ALL) #2> nul)
if ( ($netview -gt 0) -and ( ( $netview -notlike "System Error*") -or ($netview -notlike "No Entries*") ) ) {
$netview[0] = $null; $netview[-1] = $null;
$result = $netview -match '\w' | ForEach-Object { ConvertFrom-String $_.trim() -delim '\s{2,}' -propertynames 'Share','Type','Comment' }
$result[0] = $null
$result = $result | Select-Object Share, Type, Comment, @{Name="Server"; Expression={ $rname }} | ForEach-Object {
IF ( ($_.Type -eq "Disco") -and ($_.Share -ne "ADMIN$") -and ($_.Share -ne "ADMIN$") ) {
$share = $_.Share
$server = $_.Server
try { $Sub = Get-ChildItem -ErrorAction 'silentlycontinue' "Microsoft.PowerShell.Core\FileSystem::\\$server\$share" | ForEach-Object { [PSCustomObject]$t = $_ | Select-Object *; $t } }
catch { [PSCustomObject]$Sub = $null }
$_ | Select-Object Share, Type, Comment, @{Name="SubDirs"; Expression={ $Sub }}
} ELSE {
$_ | Select-Object Share, Type, Comment
}
}
$result = [PSCustomObject]@{
location = Resolve-DnsName -Name $rname.Server
Shares = $result
}
$result = ConvertTo-Json -Depth 4
$result | Out-File "$rname.json"
$result | Format-List
} else {
"RejectConn $rname" | Write-Host
}
} else {
#"ErrConn $rname" | Write-Host
}
}
}
Clear-Host
Write-Host "Total : $cc"
Write-Host "Done"
Start-Sleep 5
cd ..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment