Nathan Vanderwyst
asked on
Powershell Loop Folders to List Files Not Working
Why are the files not being returned when I run this Powershell script? What do I need to correct to get the files to list? Thank you.
Write-Host "Start " $(Get-Date -Format HH:mm:ss.fff)
$startFolders = New-Object string[] 9
$serverTypes = New-Object string[] 9
$startFolders[0] = "\\SRV01\X\data\tabsvc\logs"
$startFolders[1] = "\\SRV01\X\logs"
$startFolders[2] = "\\SRV02\X\data\tabsvc\vizqlserver\logs"
$startFolders[3] = "\\SRV03\X\logs"
$startFolders[4] = "\\SRV04\X\data\tabsvc\logs"
$startFolders[5] = "\\SRV05\X\data\tabsvc\vizqlserver\logs"
$startFolders[6] = "\\SRV06\X\logs"
$startFolders[7] = "\\SRV07\X\data\tabsvc\logs"
$startFolders[8] = "\\SRV08\X\data\tabsvc\vizqlserver\logs"
$serverTypes[0] = "DEV Primary"
$serverTypes[1] = "DEV Primary"
$serverTypes[2] = "DEV Worker 1"
$serverTypes[3] = "PROD Primary"
$serverTypes[4] = "PROD Worker 1"
$serverTypes[5] = "PROD Worker 2"
$serverTypes[6] = "QA Primary"
$serverTypes[7] = "QA Worker 1"
$serverTypes[8] = "QA Worker 2"
$logPath = "D:\Resource\Install\PowerShellScripts\LogFileSize " + $(get-date -f yyyyMMdd.HHmm) + ".txt"
$dt = get-date
Add-Content $logPath -value "SVR_TYPE|LOG_PATH|LOG_SIZE|SIZE_UOM|DATETIME|OBJTYPE"
foreach ($startFolder in $startFolders)
{
#$ErrorActionPreference = "SilentlyContinue"
#Starting folder
$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
$val = $serverTypes[$startFolders.IndexOf($startFolder)] + "|" + $startFolder + "|" + "{0:f2}" -f ($colItems.sum / 1KB) + "|KB|" + $dt + "|FOLDER"
Add-Content $logPath -value $val
#Subfolders
#$colItems = (Get-ChildItem $startFolder -Recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
$colItems = (Get-ChildItem $startFolder -Recurse -Directory | Sort-Object)
foreach ($i in $colItems)
{
$subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
$val = $serverTypes[$startFolders.IndexOf($startFolder)] + "|" + $i.FullName + "|" + "{0:f2}" -f ($subFolderItems.sum / 1KB) + "|KB|" + $dt + "|FOLDER"
Add-Content $logPath -value $val
#Files
$dir = $i.FullName
$files = (Get-ChildItem -Path $dir -File | Select-Object FullName,Length)
foreach ($f in $files)
{
$val = $serverTypes[$startFolders.IndexOf($startFolder)] + "|" + $f.FullName + "|" + "{0:f2}" -f ($f.Length / 1KB) + "|KB|" + $dt + "|FILE" |
Add-Content $logPath -value $val
}
}
}
Write-Host "Finish " $(Get-Date -Format HH:mm:ss.fff)
exit
ASKER
Wow, you took this to a new level. I got an error message and I tried to figure it out but I'm at a loss. Some of the folders have no files but I don't know if that is the issue. Thank you so much.
At D:\Resource\Install\PowerS hellScript s\LogFileF olderAndFi leSizes.ps 1:43 char:3
+ } | Export-Csv -NoTypeInformation -Path $LogPath
+ ~
An empty pipe element is not allowed.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordE xception
+ FullyQualifiedErrorId : EmptyPipeElement
At D:\Resource\Install\PowerS
+ } | Export-Csv -NoTypeInformation -Path $LogPath
+ ~
An empty pipe element is not allowed.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordE
+ FullyQualifiedErrorId : EmptyPipeElement
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked, I see you had to encapsulate the foreach with $(...) to make it work. The only issue that I still have is that some folders that have files, are not returning the files names and size, only the folder name itself is listed. I find that odd really odd. Any ideas?
Are those files maybe hidden? Then you need to add "-Force" to Get-ChildItem; by default, it doesn't return hidden files or folders.
ASKER
They are not hidden but since they are log files some are actively being written to so I wonder if that has anything to do with it. Just for kicks I tried adding "-Force " but no joy. Any other ideas?
Do you get errors for these files (without $ErrorActionPreference)?
Do you get any other files in these folders?
Do you have permissions for these folders, and if so, did you run the script from an elevated prompt?
Do you get any other files in these folders?
Do you have permissions for these folders, and if so, did you run the script from an elevated prompt?
ASKER
No I do not get any errors when I remove that line.
Yes, I get files from other folders but not all of them.
Yes I have permissions, and yes I ran from elevated prompt.
I don't have a way to know, but maybe they could be a virtualized folders like I read here It seems unlikely but I don't know why else some folders list the files and others do not.
Yes, I get files from other folders but not all of them.
Yes I have permissions, and yes I ran from elevated prompt.
I don't have a way to know, but maybe they could be a virtualized folders like I read here It seems unlikely but I don't know why else some folders list the files and others do not.
ASKER
Thank you!
Open in new window