Skip to content

Instantly share code, notes, and snippets.

Created October 30, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8b7ad2b8d882591c5384 to your computer and use it in GitHub Desktop.
Save anonymous/8b7ad2b8d882591c5384 to your computer and use it in GitHub Desktop.
#get list of folders, change the path variable to your target
$path = "\\fileserver\share\"
$Folders = dir -directory $path -Force | select name,fullname #-first 25
foreach ($folder in $Folders) {
#use robocopy to get folder statistics
$roboResult = .\Get-Windows-RobocopyFolderSize.ps1 $($folder.fullname)
#use get-aduser to check if matching ad account exists (and if the AD account has a matching P: path, but this is commented out for now)
Try {
$user = get-aduser -identity $folder.name -erroraction stop -properties HomeDirectory, LastLogonTimeStamp | select samaccountname,enabled,homedirectory,@{N='LastLogonTimeStamp'; E={[DateTime]::FromFileTime($_.LastLogonTimeStamp)}}
$ADaccountExist = $true
$AccountEnabled = $user.enabled
#if ($($folder.fullname) -eq $($user.homedirectory)){$pathMatch = $true}
#else{$pathMatch = $false}
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
$ADaccountExist = $false
$AccountEnabled = $null
}
catch{Write-Warning "Unknown Error. Script failed on foldername $folder.name"}
#build output object
$folderInfo = New-Object PSObject
$folderInfo | Add-member NoteProperty FolderName $folder.name
$folderInfo | Add-member NoteProperty FolderPath $folder.fullname
$folderInfo | Add-member NoteProperty ADaccountExist $ADaccountExist
$folderInfo | Add-member NoteProperty AccountEnabled $AccountEnabled
#$folderInfo | Add-member NoteProperty PathMatch $pathMatch
$folderInfo | Add-member NoteProperty LastLogonTimeStamp $user.LastLogonTimeStamp
$folderInfo | Add-member NoteProperty FolderUsageMB $roboResult.TotalMBytes
$folderInfo | Add-member NoteProperty DirFailed $roboResult.DirFailed
$folderInfo | Add-member NoteProperty FileFailed $roboResult.FileFailed
$folderInfo | Add-member NoteProperty DirCount $roboResult.DirCount
$folderInfo | Add-member NoteProperty FileCount $roboResult.FileCount
[array]$folderOutput += $folderInfo
}$folderOutput | Export-Csv -Delimiter ";" -Encoding Default -NoTypeInformation QuotaReportP3.csv
#################################################
#script nr2:
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)] [string[]] $Path
)
begin {
if (-not (Get-Command -Name robocopy -ErrorAction SilentlyContinue)) {
throw "I need robocopy. Exiting."
}
}
process {
foreach ($p in $Path) {
Write-Verbose -Message "Processing path: $p. $(Get-Date)"
if (-not (Test-Path -Path $p -PathType Container)) {
Write-Warning -Message "$p does not exist or is a file and not a directory. Skipping."
continue
}
$RoboCopyArgs = @("/L","/S","/NJH","/BYTES","/FP","/NC","/NDL","/TS","/XJ","/R:0","/W:0")
[datetime] $StartedTime = Get-Date
[string] $Summary = robocopy $p NULL $RoboCopyArgs | Select-Object -Last 8
[regex] $HeaderRegex = '\s+Total\s+Copied\s+Skipped\s+Mismatch\s+FAILED\s+Extras'
[regex] $DirLineRegex = 'Dirs\s:\s+(?<DirCount>\d+)(?:\s+\d+){3}\s+(?<DirFailed>\d+)\s+\d+'
[regex] $FileLineRegex = 'Files\s:\s+(?<FileCount>\d+)(?:\s+\d+){3}\s+(?<FileFailed>\d+)\s+\d+'
[regex] $BytesLineRegex = 'Bytes\s:\s+(?<ByteCount>\d+)(?:\s+\d+){3}\s+(?<ByteFailed>\d+)\s+\d+'
[regex] $TimeLineRegex = 'Times\s:\s+(?<TimeElapsed>\d+).*'
[regex] $EndedLineRegex = 'Ended\s:\s+(?<EndedTime>.+)'
if ($Summary -match "$HeaderRegex\s+$DirLineRegex\s+$FileLineRegex\s+$BytesLineRegex\s+$TimeLineRegex\s+$EndedLineRegex") {
$ErrorActionPreference = 'Stop'
try {
$EndedTime = [datetime]::ParseExact($Matches['EndedTime'], 'ddd MMM dd HH:mm:ss yyyy', [Globalization.CultureInfo]::InvariantCulture)
}
catch {
try {
$EndedTime = [datetime] $Matches['EndedTime']
}
catch {
$EndedTime = $Matches['EndedTime'] + ' (string)'
}
}
$ErrorActionPreference = 'Continue'
New-Object PSObject -Property @{
Path = $p
TotalBytes = [int64] $Matches['ByteCount']
TotalMBytes = [math]::Round(([int64] $Matches['ByteCount'] / 1MB), 4)
TotalGBytes = [math]::Round(([int64] $Matches['ByteCount'] / 1GB), 4)
BytesFailed = [int64] $Matches['ByteFailed']
DirCount = [int64] $Matches['DirCount']
FileCount = [int64] $Matches['FileCount']
DirFailed = [int64] $Matches['DirFailed']
FileFailed = [int64] $Matches['FileFailed']
StartedTime = $StartedTime
EndedTime = $EndedTime
} | Select Path, TotalBytes, TotalMBytes, TotalGBytes, DirCount, FileCount, DirFailed, FileFailed, StartedTime, EndedTime
}
else {
Write-Warning -Message "$p's output from robocopy was not in an expected format."
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment