PerformJob.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
param( [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)] [System.String] $Name, [Parameter(Mandatory=$True, Position=1, ValueFromPipeline=$false)] [System.String] $StartsWith ) function Remove-ModulesFromDirectory() { [cmdletbinding()] param( [Parameter(Mandatory=$true)] [string]$Directory, [Parameter(Mandatory=$true)] [string]$Name, [Parameter(Mandatory=$false)] [String]$StartsWith ) Write-Host $Directory if (Test-Path $directory) { $modules=Get-ChildItem -Path $Directory foreach($module in $modules) { if($StartsWith -eq "True") { if ($module.Name.StartsWith($Name)) { Remove-Item -Path $module.FullName -Force -Recurse Write-Host "Removing path $($module.FullName)" } } else { if ($module.Name -eq $Name ) { Remove-Item -Path $module.FullName -Force -Recurse Write-Host "Removing path $($module.FullName)" } } } } } function RemoveModules { [cmdletbinding()] param( [Parameter(Mandatory=$true)] [string]$Name, [Parameter(Mandatory=$false)] [String]$StartsWith ) Write-Output "Looking for the $($Name) Modules" $moduelsPath=$env:PSModulePath $pathArray=$moduelsPath.Split(';') foreach($dir in $pathArray) { Write-Host $dir Remove-ModulesFromDirectory -Directory $dir -Name $Name -StartsWith $StartsWith } } function StopPowershellInstances() { Write-Host "Getting powershell processes" $processes=Get-Process -Name pwsh* Write-Host "Found $($processes.Length) powershell processes" foreach($proces in $processes) { if ($proces.id -ne $pid) { Write-Host "Stopping process $($proces.Id)" Stop-Process $proces } } } Write-Host "Hello" Write-host "Name $Name" Write-host "StartsWith $StartsWith" StopPowershellInstances RemoveModules $Name $StartsWith Write-Host "Removal finished" Read-Host |