none
Search All Empty Shares RRS feed

  • Question

  • Hello Folks, I been using a PS script to find empty Directories in a given path. I would now like to add the functionality to scan all my Shares on my server. Currently, my Old File Server has over 100 shares and I found some of them to be empty. This So called File Server is a 2003 box and will need to be decommissioned.

    It will make my job easier to have the script scan all my shares and tell me what "Parent" directories are empty, don't really need it to tell me what paths are not empty. Of course, I can always click on 100 shares to find the same results, but why do that??

    Any help is greatly appreciated!!

    I have tried to modify the code several ways without success with adding "(get-wmiobject win32_share -cn FileServer).path" to have the script output the path, but instead outputs my directory in my own computer, not the file share.

    Can anyone tell me what I'm missing?  Its really not critical, just thought it would make my job easier.

    $SearchRoot = \\FileServer\
    
    Get-ChildItem -Path $SearchRoot | ForEach-Object {
        if ($_.PSIsContainer -eq $true) {
            if ((Get-ChildItem -Path $_.FullName) -eq $null) {
                Write-Host "$($_.FullName) is empty."  -ForegroundColor Yellow
            } else {
                Write-Host "$($_.FullName) is not empty."
            }
        }
    } 

    Tuesday, December 1, 2015 4:39 PM

Answers

  • Here's an example that lists shares on a server (as \\server\share name):


    $computerName = "myserver"
    Get-WmiObject Win32_Share -ComputerName $computerName -Filter "Type=0" | ForEach-Object {
      "\\{0}\{1}" -f $_.__SERVER,$_.Name
    }


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by prisoner107 Tuesday, December 1, 2015 5:40 PM
    Tuesday, December 1, 2015 5:05 PM
    Moderator

All replies

  • Here's an example that lists shares on a server (as \\server\share name):


    $computerName = "myserver"
    Get-WmiObject Win32_Share -ComputerName $computerName -Filter "Type=0" | ForEach-Object {
      "\\{0}\{1}" -f $_.__SERVER,$_.Name
    }


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by prisoner107 Tuesday, December 1, 2015 5:40 PM
    Tuesday, December 1, 2015 5:05 PM
    Moderator
  • That works! Thank you.

    Now I just need to figure out what this part of the code is doing: "\\{0}\{1}" 

    Tuesday, December 1, 2015 5:41 PM
  • Now I just need to figure out what this part of the code is doing: "\\{0}\{1}"

    PS C:\> help about_Operators
    ...
          -f Format operator
              Formats strings by using the format method of string
              objects. Enter the format string on the left side of the operator
              and the objects to be formatted on the right side of the operator.
    
    
                 C:\PS> "{0} {1,-10} {2:N}" -f 1,"hello",[math]::pi
                 1 hello      3.14
    
              For more information, see the String.Format method
              (http://go.microsoft.com/fwlink/?LinkID=166450) and
              Composite Formatting (http://go.microsoft.com/fwlink/?LinkID=166451).
    ...
    


    -- Bill Stewart [Bill_Stewart]


    Tuesday, December 1, 2015 6:08 PM
    Moderator
  • That works! Thank you.

    Now I just need to figure out what this part of the code is doing: "\\{0}\{1}" 

    Here's some background:

    http://ss64.com/ps/syntax-f-operator.html


    Tuesday, December 1, 2015 6:20 PM
  • Thanks for the assist Mike! That helped.
    Wednesday, December 2, 2015 12:48 AM
  • Cheers, you're very welcome.

    Wednesday, December 2, 2015 4:16 AM