• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

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
0
techdrive
Asked:
techdrive
  • 2
1 Solution
 
soostibiCommented:
think you would like to see something like this. In your script pipelines are completely mixed up.
dir \\servername\share | Where-Object { $_.PSisContainer } | 
ForEach-Object { 
    Write-Progress 'file is processing' ($_.FullName)
    $Size = Dir $_.FullName -recurse -ea SilentlyContinue | Measure-Object length -sum -ea SilentlyContinue 
    New-Object -TypeName PSObject -Property @{
        Path = $_.fullname
        Size = $size.sum
        Count = $size.count
    }
} | Select-Object @{n='Name';e={ $_.path }},
        @{n='Size';e={
                        If      (($_.Size / 1Gb) -ge 1) { "$('{0:N2}' -f ($_.Size / 1Gb)) Gb" }
                        ElseIf (($_.Size / 1Mb) -ge 1) { "$('{0:N2}' -f ($_.Size / 1Mb)) Mb" }
                        ElseIf (($_.Size / 1kb) -ge 1) { "$('{0:N2}' -f ($_.Size / 1Kb)) Kb" }
                        Else   { "$($_.Size) b" } }
                    } | 
Export-Csv "reportt.csv" -NoTypeInformation

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:

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

Open in new window

HTH

Chris
0
 
techdriveAuthor Commented:
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.
0
 
Chris DentPowerShell DeveloperCommented:
If you're approaching it as a sys admin I heartily recommend the PowerShell Cookbook (2nd Edition):

http://oreilly.com/catalog/9780596801519

It's one of my favourites and quite highly regarded.

Beyond that, there's a nice long list of resources here:

http://social.technet.microsoft.com/wiki/contents/articles/windows-powershell-survival-guide.aspx

Which includes a link to this one:

http://powershell.com/cs/blogs/ebook/

HTH

Chris
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now