Functions/Remove-OldPowerShellModule.ps1
|
function Remove-OldPowerShellModule { <# .NOTES Author: Skyler Hart Created: 2021-08-31 23:58:29 Last Edit: 2021-08-31 23:58:29 Keywords: Other: Requires: -RunAsAdministrator .LINK https://wanderingstag.github.io #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute( "PSAvoidGlobalVars", "", Justification = "Have tried other methods and they do not work consistently." )] [CmdletBinding()] Param ( [Parameter( Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true )] [Alias('Host','Name','Computer','CN')] [string[]]$ComputerName = $env:COMPUTERNAME, [Parameter()] [int32]$MaxThreads = 5, [Parameter()] $SleepTimer = 250, [Parameter()] $MaxResultTime = 14400 ) Begin { $ISS = [system.management.automation.runspaces.initialsessionstate]::CreateDefault() $RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxThreads, $ISS, $Host) $RunspacePool.Open() $Code = { [CmdletBinding()] Param ( [Parameter( Mandatory=$true, Position=0 )] [string]$comp ) Import-Module WSTools $ops = ($Global:WSToolsConfig).OldPSModule foreach ($o in $ops) { $p = "\\$comp\c$\Program Files\WindowsPowerShell\Modules\" + $o if (Test-Path $p) { Remove-Item -Path $p -Recurse -Force } } }#end code block $Jobs = @() } Process { Write-Progress -Activity "Preloading threads" -Status "Starting Job $($jobs.count)" ForEach ($Object in $ComputerName){ $PowershellThread = [powershell]::Create().AddScript($Code) $PowershellThread.AddArgument($Object.ToString()) | out-null $PowershellThread.RunspacePool = $RunspacePool $Handle = $PowershellThread.BeginInvoke() $Job = "" | Select-Object Handle, Thread, object $Job.Handle = $Handle $Job.Thread = $PowershellThread $Job.Object = $Object.ToString() $Jobs += $Job } } End { $ResultTimer = Get-Date While (@($Jobs | Where-Object {$Null -ne $_.Handle}).count -gt 0) { $Remaining = "$($($Jobs | Where-Object {$_.Handle.IsCompleted -eq $False}).object)" If ($Remaining.Length -gt 60){ $Remaining = $Remaining.Substring(0,60) + "..." } Write-Progress ` -Activity "Waiting for Jobs - $($MaxThreads - $($RunspacePool.GetAvailableRunspaces())) of $MaxThreads threads running" ` -PercentComplete (($Jobs.count - $($($Jobs | Where-Object {$_.Handle.IsCompleted -eq $False}).count)) / $Jobs.Count * 100) ` -Status "$(@($($Jobs | Where-Object {$_.Handle.IsCompleted -eq $False})).count) remaining - $remaining" ForEach ($Job in $($Jobs | Where-Object {$_.Handle.IsCompleted -eq $True})){ $Job.Thread.EndInvoke($Job.Handle) $Job.Thread.Dispose() $Job.Thread = $Null $Job.Handle = $Null $ResultTimer = Get-Date } If (($(Get-Date) - $ResultTimer).totalseconds -gt $MaxResultTime){ Write-Error "Child script appears to be frozen, try increasing MaxResultTime" Exit } Start-Sleep -Milliseconds $SleepTimer } $RunspacePool.Close() | Out-Null $RunspacePool.Dispose() | Out-Null } } |