techdrive
asked on
poweshell script problem
I am trying to get the MB OR KB or GB info to print out but for some reason this is not working. I am trying to get the size and names of folders on our network. ANy help would be appreciative, thanks.
ForEach-Object { dir \\servername\share | Where-Object { $_.PSisContainer } } |
ForEach-Object { Write-Progress 'file is processing' ($_.FullName); $_ } |
ForEach-Object { $result = '' | Select-Object Path, Count, Size; $result.path = $_.FullName;
$temp = Dir $_.FullName -recurse -ea SilentlyContinue | Measure-Object length -sum -ea SilentlyContinue ;
Select-Object @{n='Name';e={ $_ }},@{n='Size';e={
If (($result.Size / 1Gb) -ge 1) { "$('{0:D2}' -f ($result.Size / 1Gb)) Gb" }
ElseIf (($result.Size / 1Mb) -ge 1) { "$('{0:N2}' -f ($result.Size / 1Mb)) Mb" }
ElseIf (($result.Size / 1kb) -ge 1) { "$('{0:N2}' -f ($result.Size / 1Kb)) Kb" }
Else { "$($result.Size) b" } }}} | | Export-Csv "reportt.csv" -NoTypeInformation
ForEach-Object { dir \\servername\share | Where-Object { $_.PSisContainer } } |
ForEach-Object { Write-Progress 'file is processing' ($_.FullName); $_ } |
ForEach-Object { $result = '' | Select-Object Path, Count, Size; $result.path = $_.FullName;
$temp = Dir $_.FullName -recurse -ea SilentlyContinue | Measure-Object length -sum -ea SilentlyContinue ;
Select-Object @{n='Name';e={ $_ }},@{n='Size';e={
If (($result.Size / 1Gb) -ge 1) { "$('{0:D2}' -f ($result.Size / 1Gb)) Gb" }
ElseIf (($result.Size / 1Mb) -ge 1) { "$('{0:N2}' -f ($result.Size / 1Mb)) Mb" }
ElseIf (($result.Size / 1kb) -ge 1) { "$('{0:N2}' -f ($result.Size / 1Kb)) Kb" }
Else { "$($result.Size) b" } }}} | | Export-Csv "reportt.csv" -NoTypeInformation
That's a lot of loops... The first is not necessary, so lets drop that one right away. You only actually need one.
You make $result, and give no value for size, which is why it can't show any.
dir \\servername\share | Where-Object { $_.PSisContainer } | ForEach-Object {
Write-Progress "File is processing" -Status $_.FullName
# This is more efficient than Measure-Object (uses less RAM / should be faster)
$Size = 0; $Count = 0
dir $_.FullName -Recurse -ErrorAction SilentlyContinue | ForEach-Object { $Size += $_.Length; $Count++ }
$_ | Select-Object Name, @{n='Count';e={ $Count }}, @{n='Size';e={
If ($Size -gt 1Gb) { "$('{0:N2}' -f ($Size / 1Gb)) Gb" }
ElseIf ($Size -gt 1Mb) { "$('{0:N2}' -f ($Size / 1Mb)) Mb" }
ElseIf ($Size -gt 1Kb) { "$('{0:N2}' -f ($Size / 1Kb)) Kb" }
Else { "$Size b" } }}
} | Export-Csv "report.csv" -NoTypeInformation
HTHChris
ASKER
Chris,
You are truly sir the master of powershell. Is it any book or resource you can point me to in order for me to advance my knowledge.
You are truly sir the master of powershell. Is it any book or resource you can point me to in order for me to advance my knowledge.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window