File share info script

This topic contains 6 replies, has 3 voices, and was last updated by  Daniel Krebs 1 year, 2 months ago.

  • Author
    Posts
  • #51216

    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

    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

    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

    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

    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

    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

    Daniel Krebs
    Moderator

    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.