Last active
December 26, 2015 02:49
-
-
Save lholman/7081718 to your computer and use it in GitHub Desktop.
A Powershell Module to removes a given parentPath or selective child paths based on a supplied pattern, optionally accepts a PSSession object to support execution on a remote server using PSRemoting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Remove-ChildFolders{ | |
<# | |
.SYNOPSIS | |
Removes a parentPath or selective child paths based on a supplied pattern. | |
.DESCRIPTION | |
Removes a parentPath or selective child paths based on a supplied pattern, optionally accepts a PSSession object to support execution on a remote server using PSRemoting. | |
.NOTES | |
Author: Lloyd Holman | |
DateCreated: 21/10/2013 | |
Requirements: Copy this module to any location found in $env:PSModulePath | |
.PARAMETER parentPath | |
Required. The parent (root) path that all removal (deletion) should be confined to. | |
.PARAMETER removePattern | |
Optional. A regular expression defining the child folders to remove from under the parentPath. This defaults to empty which will result in the parentPath being deleted in its entirety. | |
.PARAMETER serverName | |
Optional. | |
.PARAMETER runAsUserName | |
Optional. | |
.PARAMETER runAsUserPassword | |
Optional. | |
.PARAMETER psSession | |
Optional. | |
.EXAMPLE | |
Import-Module Remove-ChildFolders | |
Import the module | |
.EXAMPLE | |
Get-Command -Module Remove-ChildFolders | |
List available functions | |
.EXAMPLE | |
Remove-ChildFolders -parentPath "C:\Websites\" | |
.EXAMPLE | |
Remove-ChildFolders -parentPath "C:\Websites\" -removePattern "_2012" | |
#> | |
[cmdletbinding()] | |
Param( | |
[Parameter( | |
Position = 0, | |
Mandatory = $True )] | |
[string]$parentPath, | |
[Parameter( | |
Position = 1, | |
Mandatory = $False )] | |
[string]$removePattern, | |
[Parameter( | |
Position = 2, | |
Mandatory = $False )] | |
[string]$serverName, | |
[Parameter( | |
Position = 3, | |
Mandatory = $False )] | |
[string]$runAsUserName, | |
[Parameter( | |
Position = 4, | |
Mandatory = $False )] | |
[string]$runAsUserPassword, | |
[Parameter( | |
Position = 5, | |
Mandatory = $False )] | |
[system.management.automation.runspaces.PSSession]$psSession = $null | |
) | |
Begin { | |
$DebugPreference = "Continue" | |
} | |
Process { | |
Try | |
{ | |
$localPath = $False | |
if (($serverName -eq "") -and ($runAsUserName -ne "" -or $runAsUserPassword -ne "") -or ($psSession -ne $null)) | |
{ | |
Write-Host "It looks like you want to remove from a remove server but the serverName is missing, please provide a serverName to execute the command against." | |
break | |
} | |
if (($runAsUserName -eq "" ) -and ($psSession -eq $null) -and ($parentPath | Select-String -Pattern ":\" -SimpleMatch -Quiet)) | |
{ | |
Write-Host "Warning: No Windows user name supplied (via -user) or PSSession object supplied (via -psSession) and value of parentPath assumes we're removing from a local path." | |
$localPath = $True | |
} | |
if ($psSession -ne $null) | |
{ | |
#Lets check the supplied serverName against the PSSession ComputerName | |
if ($psSession.ComputerName -ne $serverName) | |
{ | |
Write-Host "The supplied serverName ($serverName) doesn't match the ComputerName ($psSession.ComputerName) of the supplied PSSession object." | |
break | |
} | |
} | |
$s = $null | |
if ($psSession -ne $null) | |
{ | |
#We've been passed a valid PSSession object so let's use that | |
$s = $psSession | |
} | |
elseif ($psSession -eq $null -and $localPath -eq $False) | |
{ | |
#Request a PSSession object, module validates runAsUserName and runAsUserPassword so no need to check again | |
Import-Module ".\Get-PSSessionSilent.psm1" | |
$s = Get-PSSessionSilent -runAsUserName $runAsUserName -runAsUserPassword $runAsUserPassword -serverName $serverName | |
Remove-Module Get-PSSessionSilent | |
} | |
if ($localPath -eq $True) | |
{ | |
#Local Implementation | |
if ($removePattern -eq "") | |
{ | |
#Remove the parentPath | |
if (Test-Path $parentPath) | |
{ | |
Remove-Item -force -recurse $parentPath -ErrorAction SilentlyContinue | |
Write-Host "Path: $parentPath removed" | |
} | |
else | |
{ | |
Write-Host "WARNING path: $parentPath not found" | |
} | |
return | |
} | |
else | |
{ | |
#Remove based on removePattern, maybe use Select-String methodology with -Pattern and -SimpleMatch and -Quiet | |
return | |
} | |
} | |
} | |
catch [Exception] { | |
$result = " Error: Woh!, wasn't expecting to get this exception. `r`n $_.Exception.ToString()" | |
} | |
} | |
End { | |
return $result | Format-Table | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment