Monday, September 14, 2009

GetShare

The previous post inspired me.  How about a function that is a bit more robust?  With this new function you can filter for share names by excluding and/or including specificic text within the share name.  You can even use variables such as * or !.

USAGE
getshare %NAME_OR_IP% [-exclude string, -include string, -csv file]

FUNCTION

function getshare {
  param (
    [string]$name, #Machine to gather shares from
    [string]$exclude, #Text to filter out
    [string]$include, #Text to search for
    [string]$csv #Output to csv
  )
  if ($exclude -and $include) { $list = GWMI win32_share -computer $name | ($_.name -notlike $exclude) -and ($_.name -like $include)} }
  elseif ($exclude) { $list = GWMI win32_share -computer $name | ? {$_.name -notlike $exclude} }
  elseif ($include) { $list = GWMI win32_share -computer $name | ? {$_.name -like $include} }
  else { $list = GWMI win32_share -computer $name }

  if ($csv) { $list | Export-Csv $csv -NoTypeInformation ; Import-Csv $csv | select Name,Path,Description | fl}
  else { $list | fl }
}
 code: copy : expand : collapse

0 comments: