Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

Get-Childitem logic

Dear experts

Script below list all path inside folderlist.csv this works fine, however I stuck with if I would only show those path which got more than 0, basicly I want to ignore those path with no files in the folder. when I see the $result. How do I do here? Thanks alot.

$servers = Import-Csv -Path c:\folderlist.csv
$7daysago = (get-date).AddDays(-7)
$result = $servers | ForEach-Object {

$count = (Get-ChildItem -Path $_.Path -Exclude keep*, testfilnamn*, tilltestfil* |
Where-Object { !($_.PSIsContainer) -and $_.LastWriteTime -le $7daysago -or $_.Name -like '*failed*'}).count
"$($count) Files in '$($_.Path)'"
}
$result

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi
WeTi

ASKER

Didn't return any result... tried ran the $count and it shows 0...
Sorry, my bad. Fixed above, just download again.
Avatar of WeTi

ASKER

worked after removed, -path and -File thanks alot again
$count= Get-ChildItem $_.Path -Exclude 'keep*', 'testfilnamn*', 'tilltestfil*' |
-Path is the first named parameter, that shouldn't make any issues.
I used the -File to replace the PSIsContainer in the filter, because it's better practice to tell Get-ChildItem to not even return directories than to have it return directories and filter them out in PS.
The only way this would pose any problem is if you're still running PS 2.0 (from 2009)?
Avatar of WeTi

ASKER

Orchestrator use that, and yes its stupid...
I feel your pain.
In a case like this, you should mention that you're restricted to PS2.0/Orchestrator.
PS 2.0 has several bugs that potentially need a workaround, and the host used by Orchestrator doesn't support Write-Host, for example.
Also, you might want to have a look at this:
Orchestrator: How to use the Windows native powershell version
https://social.technet.microsoft.com/wiki/contents/articles/51591.orchestrator-how-to-use-the-windows-native-powershell-version.aspx
And here's the version with the PSIsContainer check back:
$servers = Import-Csv -Path c:\folderlist.csv
$7daysago = (Get-Date).AddDays(-7)
$result = $servers | ForEach-Object {
	$count = Get-ChildItem -Path $_.Path -Exclude 'keep*', 'testfilnamn*', 'tilltestfil*' |
		Where-Object {(-not $_.PSIsContainer) -and (($_.LastWriteTime -le $7daysago) -or ($_.Name -like '*failed*'))} |
		Measure-Object |
		Select-Object -ExpandProperty Count
	If ($count -gt 0) {
		"$($count) Files in '$($_.Path)'"
	}
}
$result

Open in new window