#requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Get-ShareResource.ps1 # Version: 1.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # http://www.ScriptingGeek.com # Date: 7/9/2011 # Keywords: WMI, Win32_Share,Get-WMIObject # Comments: # # MSDN class documentation can be found at # http://msdn.microsoft.com/en-us/library/aa394435(v=vs.85).aspx # # # "Those who forget to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- Function Get-ShareResource { <# .SYNOPSIS Get a computer's shared resources .DESCRIPTION Using WMI, return all shared resources on a computer. This function returns a custom object that shows what type of resource is being shared. Name : print$ Path : C:\Windows\system32\spool\drivers Description : Printer Drivers Status : OK Type : File Hidden : True Computername : SERENITY You can connect to a remote computer but you must be running with admin credentials. .PARAMETER Computername The name of the computer to query. The default is the local host. .EXAMPLE PS C:\> Get-ShareResource Name : ADMIN$ Path : C:\Windows Description : Remote Admin Status : OK Type : AdminDisk Hidden : True Computername : SERENITY Name : C$ Path : C:\ Description : Default share Status : OK Type : AdminDisk Hidden : True Computername : SERENITY Name : Download$ Path : c:\users\jeff\downloads Description : Status : OK Type : File Hidden : True Computername : SERENITY Return all shared resouces .EXAMPLE PS C:\> Get-ShareResource -computername SBC02 | where {! $_.Hidden} | Select Name,Path Name Path ---- ---- Users C:\Users Get all non-hidden shares on computer SBC02. .EXAMPLE PS C:\> Get-Content c:\work\computers.txt | Get-ShareResource | Export-CSV c:\work\ShareAudit.csv Go through the list of computers and pipe each name to Get-ShareResource. The results are then saved to a CSV file. .NOTES NAME : Get-ShareResource VERSION : 1.0 LAST UPDATED: 7/11/2011 AUTHOR : Jeffery Hicks .LINK http://jdhitsolutions.com/blog/2011/07/get-shared-resource/ .LINK Get-WmiObject .INPUTS String .OUTPUTS Custom #> [cmdletbinding()] Param( [Parameter(Position=0,ValuefromPipeline=$True)] [Alias("Name")] [string[]]$Computername=$env:computername ) Begin { Write-Verbose "Starting $($myinvocation.mycommand)" } Process { Foreach ($computer in $computername) { Write-Verbose "Querying $($computer.ToUpper())" Try { Get-WmiObject -Class Win32_Share -ComputerName $Computername -erroraction "Stop" | Select Name,Path,Description,Status, @{Name="Type";Expression={ Switch ($_.Type) { 0 {"File"} 1 {"Printer"} 2 {"Device"} 3 {"IPC"} 2147483648 {"AdminDisk"} 2147483649 {"AdminPrint"} 2147483650 {"AdminDevice"} 2147483651 {"AdminIPC"} Default {$_.Type} } }}, @{Name="Hidden";Expression={ #use a regular expression and check if the name ends in a $ #which indicates a hidden share if ($_.Name -match "[\$]$") { $True } else { $False } }}, @{Name="Computername";Expression={$_.__SERVER}} } Catch { $msg="Failed to get share information from {0}. {1}" -f $($computer.ToUpper()),$_.Exception.Message Write-Warning -Message $msg } }#foreach } #process End { Write-Verbose "Ending $($myinvocation.mycommand)" } } #end function #optional - create an alias #Set-Alias -name gsr -value Get-ShareResource