Wednesday, October 26, 2011

PowerShell - Output File Share Info to Excel

Need to audit file shares on your network? Here's a quick and easy PowerShell script that will gather all the share names and paths. Note that you need to run this from a computer that has Microsoft Excel installed.

masterservers.txt is a file containing all the servers you want to run the script against. As always, you could also feed an AD query into the $servers array instead of pulling from a file.

#get all file shares and output to Excel

#Set Server Name
$servers= get-content masterservers.txt

#create new Excel WorkBook
$Excel = new-Object -comobject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
$Sheet.Cells.Item(1,1) = "Server Name"
$Sheet.Cells.Item(1,2) = "Share Name"
$Sheet.Cells.Item(1,3) = "Share Path"
$intRow = 2
$WorkBook = $Sheet.UsedRange
$WorkBook.Font.Bold = $True

#Get File Share Information
ForEach ($server in $servers)
{
$Shares = Get-WmiObject -Class win32_share -ComputerName $server | where-object {$_.type -eq 0}
foreach ($Share in $Shares)
{
$Sheet.Cells.Item($intRow, 1) = $Server
$Sheet.Cells.Item($intRow, 2) = $Share.Name
$Sheet.Cells.Item($intRow, 3) = $Share.Path
$intRow = $intRow + 1
}
}

$WorkBook.EntireColumn.AutoFit()
$intRow = $intRow + 1
$Sheet.Cells.Item($intRow,1).Font.Bold = $True

No comments: