0

This seems like a very easy thing to do; however, I don't know anything about WMI. How would I go about determining if a directory is shared over the network on one of my servers (using PowerShell)? Also, is there a quick primer that I can read to get aquainted to WMI? I really want to be able to use it when scripting, but most resources don't show any examples with PowerShell.

Currently, I'm running a check to see if the "Server" service is running; and another check to see if a specific group has permissions on the directory.

    $serversrvc = Get-Service -Name "Server"
    $dirpath = "C:\My\Path\"
    $pathacl = (Get-Acl -path $dirpath).Access
    $group = "Domain\User"

    # Test if the "Server" service is running
    If ($serversrvc.Status -eq "Running")
    {
        echo '"Server" service is running.'
    }
    Else
    {
        echo '"Server" is NOT running.'
    }

    # Test if the $dirpath exists
    If (Test-Path $dirpath)
    {
        echo "$dirpath exists."
    }
    Else
    {
        echo "$dirpath does not exist."
    }

    # Test if $group has permssions on $dirpath
    foreach ($id in $pathacl)
    {
        If ($id.IdentityReference -eq $group)
        {
            echo "$group has access to $dirpath"
        }
    }
EGr
  • 2,072
  • 10
  • 41
  • 61

2 Answers2

5

The Win32_Share WMI class returns all instances of shared folders on a computer. This will give you all shared folders on the local machine:

Get-WmiObject -Class Win32_Share

Notice the Path property, you can use the Filter parameter (equivalent to the WQL WHERE clause) to get back shares witha certain local path but you'd also need to double each slash (slash is a :

Get-WmiObject -Class Win32_Share -ComputerName PC1 -Filter "Path='C:\\Windows'"

If you get back a result the folder is shared, otherwise it isn't. Notice the ComputerName parameter, you can execute the wmi command against remote computers.

To get back a Boolean result, true if the folder is shared and false otherwise, cast the command to bool:

 [bool](Get-WmiObject -Class Win32_Share -ComputerName PC1 -Filter "Path='C:\\Windows'")
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Thanks! This is exactly what I'm looking for. I'll have to read up on WMI more, as I will probably be using it a lot in the future. – EGr Sep 28 '12 at 16:29
1

You can use Get-WmiObject Win32_Share to enumerate the shares on a computer. You could also use net share but then you'd have to parse the output and why bother with that when you have the WMI output. :-)

Keith Hill
  • 194,368
  • 42
  • 353
  • 369