Solved

Powershell Loop Folders to List Files Not Working

Posted on 2016-11-22
9
16 Views
Last Modified: 2016-11-23
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

Open in new window

0
Comment
Question by:Nathan Vanderwyst
  • 5
  • 4
9 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 41897760
Try it like this:
$dt = Get-Date
$LogPath = "D:\Resource\Install\PowerShellScripts\LogFileSize $($dt.ToString('yyyyMMdd.HHmm')).txt"

Function New-Row($SVR_TYPE, $LOG_PATH, $LOG_SIZE, $SIZE_UOM, $DATETIME, $OBJTYPE) {
	New-Object -TypeName PSObject -Property ([ordered]@{
		'SVR_TYPE' = $SVR_TYPE
		'LOG_PATH' = $LOG_PATH
		'LOG_SIZE' = [math]::Round($LOG_SIZE, 2)
		'SIZE_UOM' = $SIZE_UOM
		'DATETIME' = $DATETIME
		'OBJTYPE' = $OBJTYPE
	})
}

$Sources = @'
	"ServerType",	"StartFolder"
	"DEV Primary",	"\\SRV01\X\data\tabsvc\logs"
	"DEV Primary",	"\\SRV01\X\logs"
	"DEV Worker 1",	"\\SRV02\X\data\tabsvc\vizqlserver\logs"
	"PROD Primary",	"\\SRV03\X\logs"
	"PROD Worker 1","\\SRV04\X\data\tabsvc\logs"
	"PROD Worker 2","\\SRV05\X\data\tabsvc\vizqlserver\logs"
	"QA Primary",	"\\SRV06\X\logs"
	"QA Worker 1",	"\\SRV07\X\data\tabsvc\logs"
	"QA Worker 2",	"\\SRV08\X\data\tabsvc\vizqlserver\logs"
'@ | ConvertFrom-Csv

Write-Host "Start  " $(Get-Date -Format HH:mm:ss.fff)
ForEach ($Source In $Sources) {
	#$ErrorActionPreference = "SilentlyContinue"  
	#Starting folder
	$Size = (Get-ChildItem $Source.StartFolder -File | Measure-Object -Property Length -Sum).Sum
	New-Row -SVR_TYPE $Source.ServerType -LOG_PATH $Source.StartFolder -LOG_SIZE ($Size / 1KB) -SIZE_UOM 'KB' -DATETIME $dt -OBJTYPE 'FOLDER'
	#Subfolders
	Get-ChildItem $Source.StartFolder -Recurse -Directory | Sort-Object -Property FullName | ForEach-Object {
		$Size = (Get-ChildItem $_.FullName -File | Measure-Object -Property Length -Sum).Sum
		New-Row -SVR_TYPE $Source.ServerType -LOG_PATH $_.FullName -LOG_SIZE ($Size / 1KB) -SIZE_UOM 'KB' -DATETIME $dt -OBJTYPE 'FOLDER'
		#Files
		Get-ChildItem -Path $_.FullName -File | Sort-Object -Property Name | ForEach-Object {
			New-Row -SVR_TYPE $Source.ServerType -LOG_PATH $_.FullName -LOG_SIZE ($_.Length / 1KB) -SIZE_UOM 'KB' -DATETIME $dt -OBJTYPE 'FILE'
		}
	}
} | Export-Csv -NoTypeInformation -Path $LogPath
Write-Host "Finish " $(Get-Date -Format HH:mm:ss.fff)

Open in new window

0
 

Author Comment

by:Nathan Vanderwyst
ID: 41897844
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\PowerShellScripts\LogFileFolderAndFileSizes.ps1:43 char:3
+ } | Export-Csv -NoTypeInformation -Path $LogPath
+   ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement

11-22-2016-11-44-37-AM.png
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41897852
Sorry, tested without the export:
$dt = Get-Date
$LogPath = "D:\Resource\Install\PowerShellScripts\LogFileSize $($dt.ToString('yyyyMMdd.HHmm')).txt"

Function New-Row($SVR_TYPE, $LOG_PATH, $LOG_SIZE, $SIZE_UOM, $DATETIME, $OBJTYPE) {
	New-Object -TypeName PSObject -Property ([ordered]@{
		'SVR_TYPE' = $SVR_TYPE
		'LOG_PATH' = $LOG_PATH
		'LOG_SIZE' = [math]::Round($LOG_SIZE, 2)
		'SIZE_UOM' = $SIZE_UOM
		'DATETIME' = $DATETIME
		'OBJTYPE' = $OBJTYPE
	})
}

$Sources = @'
	"ServerType",	"StartFolder"
	"DEV Primary",	"\\SRV01\X\data\tabsvc\logs"
	"DEV Primary",	"\\SRV01\X\logs"
	"DEV Worker 1",	"\\SRV02\X\data\tabsvc\vizqlserver\logs"
	"PROD Primary",	"\\SRV03\X\logs"
	"PROD Worker 1","\\SRV04\X\data\tabsvc\logs"
	"PROD Worker 2","\\SRV05\X\data\tabsvc\vizqlserver\logs"
	"QA Primary",	"\\SRV06\X\logs"
	"QA Worker 1",	"\\SRV07\X\data\tabsvc\logs"
	"QA Worker 2",	"\\SRV08\X\data\tabsvc\vizqlserver\logs"
'@ | ConvertFrom-Csv

Write-Host "Start  " $(Get-Date -Format HH:mm:ss.fff)
$(ForEach ($Source In $Sources) {
	#$ErrorActionPreference = "SilentlyContinue"  
	#Starting folder
	$Size = (Get-ChildItem $Source.StartFolder -File | Measure-Object -Property Length -Sum).Sum
	New-Row -SVR_TYPE $Source.ServerType -LOG_PATH $Source.StartFolder -LOG_SIZE ($Size / 1KB) -SIZE_UOM 'KB' -DATETIME $dt -OBJTYPE 'FOLDER'
	#Subfolders
	Get-ChildItem $Source.StartFolder -Recurse -Directory | Sort-Object -Property FullName | ForEach-Object {
		$Size = (Get-ChildItem $_.FullName -File | Measure-Object -Property Length -Sum).Sum
		New-Row -SVR_TYPE $Source.ServerType -LOG_PATH $_.FullName -LOG_SIZE ($Size / 1KB) -SIZE_UOM 'KB' -DATETIME $dt -OBJTYPE 'FOLDER'
		#Files
		Get-ChildItem -Path $_.FullName -File | Sort-Object -Property Name | ForEach-Object {
			New-Row -SVR_TYPE $Source.ServerType -LOG_PATH $_.FullName -LOG_SIZE ($_.Length / 1KB) -SIZE_UOM 'KB' -DATETIME $dt -OBJTYPE 'FILE'
		}
	}
}) | Export-Csv -NoTypeInformation -Path $LogPath
Write-Host "Finish " $(Get-Date -Format HH:mm:ss.fff)

Open in new window

1
 

Author Comment

by:Nathan Vanderwyst
ID: 41897863
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?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 83

Expert Comment

by:oBdA
ID: 41897951
Are those files maybe hidden? Then you need to add "-Force" to Get-ChildItem; by default, it doesn't return hidden files or folders.
0
 

Author Comment

by:Nathan Vanderwyst
ID: 41897976
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?
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41898084
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?
0
 

Author Comment

by:Nathan Vanderwyst
ID: 41898149
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.
0
 

Author Closing Comment

by:Nathan Vanderwyst
ID: 41899534
Thank you!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now