Created
November 26, 2018 15:09
-
-
Save mczerniawski/7f4cfb5123cb9e90f51e1d4dd7addd0b to your computer and use it in GitHub Desktop.
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 Get-EmptyFolder { | |
<# | |
.SYNOPSIS | |
Returns if there are empty folders in given path. | |
.DESCRIPTION | |
Will check for empty folders in given path. If ComputerName is provided will connect to remote computer. Credential parameter works in combination with ComputerName only | |
.PARAMETER Path | |
Path to check for empty folders | |
.PARAMETER ComputerName | |
Remote computer name | |
.PARAMETER Credential | |
Credentials for remote computer | |
.EXAMPLE | |
Get-EmptyFolder -path 'c:\AdminTools','C:\Program Files' -ComputerName Server1 -Verbose | |
VERBOSE: Processing with remote computer {Server1} | |
VERBOSE: Processing with default credentials of user {mczerniawski_admin} | |
Directory: C:\AdminTools | |
Mode LastWriteTime Length Name PSComputerName | |
---- ------------- ------ ---- -------------- | |
d----- 05.11.2018 14:35 VMs Server1 | |
.EXAMPLE | |
Get-EmptyFolder -path 'c:\AdminTools','C:\Program Files' -Verbose | |
VERBOSE: Processing local computer {MyMachine1} | |
Directory: C:\AdminTools | |
Mode LastWriteTime Length Name | |
---- ------------- ------ ---- | |
d----- 26.11.2018 15:49 1 | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory, HelpMessage = 'Path to check', Position = 0, | |
ValueFromPipelineByPropertyName)] | |
[string[]] | |
$Path, | |
[Parameter(Mandatory = $false, HelpMessage = 'Provide Computer Name', | |
ValueFromPipelineByPropertyName)] | |
[ValidateNotNullOrEmpty()] | |
[System.String] | |
$ComputerName, | |
[Parameter(Mandatory = $false, HelpMessage = 'Provide Credentials for ComputerName', | |
ValueFromPipelineByPropertyName)] | |
[System.Management.Automation.PSCredential] | |
$Credential | |
) | |
begin { | |
$localExecution = $true | |
#region PSSession parameters | |
if ($PSBoundParameters.ContainsKey('ComputerName')) { | |
$connectionParams = @{ | |
ComputerName = $ComputerName | |
} | |
Write-Verbose -Message "Processing with remote computer {$ComputerName}" | |
if ($PSBoundParameters.ContainsKey('Credential')) { | |
$connectionParams.Credential = $Credential | |
Write-Verbose -Message "Processing with provided credentials {$($Credential.UserName)}" | |
} | |
else { | |
Write-Verbose -Message "Processing with default credentials of user {$($env:USERNAME)}" | |
} | |
$localExecution = $false | |
} | |
else { | |
Write-Verbose -Message "Processing local computer {$($env:COMPUTERNAME)}" | |
} | |
#endregion | |
} | |
process { | |
if ($localExecution) { | |
Get-ChildItem -Path $Path | Where-Object -FilterScript {$PSItem.PSIsContainer -eq $True} | Where-Object -FilterScript {-NOT $_.GetFiles("*", "AllDirectories")} | |
} | |
else { | |
Invoke-Command @connectionParams -ScriptBlock { | |
Get-ChildItem -Path $USING:Path | Where-Object -FilterScript {$PSItem.PSIsContainer -eq $True} | Where-Object -FilterScript {-NOT $_.GetFiles("*", "AllDirectories")} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment