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"
}
}
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:
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 :
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: