File share info script

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 3 months, 3 weeks ago.

  • Author
    Posts
  • #51216
    Profile photo of Blake Fletcher
    Blake Fletcher
    Participant

    Hello, I've been tasked with getting file share info such as name, actual path, and space used in MB or GB. We have two many servers for me to continue to do this manually so I started writing a powershell script but have run into some issues.

    Here's the code so far:

    $shares = gwmi Win32_Share | ? {$_.Description -eq "$null"}
    foreach($share in $shares){
    $size = (Get-childItem -Path $share.Path | Measure-object -Property length -sum)
    write-host $share.Name $share.Path ($size.sum / 1GB -as [int])
    }
    

    I don't want the default shares hence the {$_.Description -eq "$null"}. It works just fine if there's only one share, but if there are multiple I get this:

    Measure-object : Property "length" cannot be found in any object(s) input

    Any ideas how to fix this?

  • #51223
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Add -File to your Get-ChildItem. The problem is that you're getting Files and Directories, and Directory objects don't have a length.

  • #51225
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Also... and perhaps obviously... expect this to not be fast. And to possibly fail in any instance where you don't have permission to read the file. Or when the server just gets tired of sending file data to you and drops the connection. This is something I'd probably write as a workflow, ship to the server via Remoting, and let each server run locally on its own. They'd then ship the results back to you however many hours later.

    But that's just me :).

  • #51227
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Oh, you'll need -Recurse on Get-ChildItem, too, or you're only going to get the files in the root of the share. Remember, directories don't have sizes – only files.

  • #51235
    Profile photo of Blake Fletcher
    Blake Fletcher
    Participant

    Hm, can't seem to get it working with -File and -Recurse either. We're running different versions of Windows Server so I don't think the Powershell is the same across them. I tried Get-ChildItem with -File and it said that wasn't a parameter. Grr this is getting frustrating, anything besides powershell to do this? haha.

  • #51239
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Oh. You have a VOVOPS, or Very Old Version of PowerShell. You'll have to pipe Get-ChildItem to Where-Object to remove all but the files.

    gci . *.* -rec | where { $_.GetType().Name -eq "FileInfo" }

    -Recurse (-rec) should work regardless. It's been there since 1.0.

    And yes, there are lots of tools that do what you're after. Mostly commercial ones that are really fast at it, too.

  • #51243
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Before the -File parameter became available on the Get-Childitem cmdlet I've used the property PSIsContainer to filter for files only.

    gci . *.* -rec | where { -not $_.PSIsContainer }

You must be logged in to reply to this topic.