Powershell – Liste des partages réseau sur un serveur de fichier avec détails

Hello,

Un petit script permettant de lister tous les shared folders sur un serveur de fichier Microsoft, et de générer un fichier CSV et HTML avec les détails. Utile pour avoir une vue globale de l’utilisation des ressources par share, ou pour inventaire.

# Reset the variables to start with something clean
$array = $allShares = $count = $objFldr = $counter = $share = $body = $outCSV = $outHTML = $null

# This is the Windows file server (EDIT THIS)
$fileServer = "fs01.domain.com"

# This variable will contain all shared folders hosted on the File Server
$allShares = Get-WmiObject Win32_Share -ComputerName $fileServer | select Caption, Name, Path

# Counting how many shares are hosted on that server
$count = $allShares.count

# Creating an object that will be used to get fodler sizes later
$objFldr = New-Object -com Scripting.FileSystemObject

# Creating an empty array that will be populated with our findings
$array = @()

# Starting a counter, just for console logging
[int]$counter = "0"

foreach ($share in $allShares)
{
    Write-host "Now processing share number $counter on a total of $count shares."

    $shareName = $share.Name # Get the share name
    $shareCaption = $share.Caption # Get the share caption (decription)
    $sharePath = "$fileServer" + $share.Path # Construct the share path
    $sharePath = $sharePath -replace ":","$"
    $shareSize = $objFldr.GetFolder($sharePath).Size /1GB # Get the folder size and make it readeable
    $shareSizeGB = [math]::Round($shareSize,3) # Round the result to a reasonable number
    $Object = New-Object PSObject # Create a custom PSObject that will be used to organize our findings
    $Object | Add-Member -MemberType NoteProperty -Name "Share Name" -Value $shareName # Feed with the name
    $Object | Add-Member -MemberType NoteProperty -Name "Caption" -Value $shareCaption # Feed with the caption
    $Object | Add-Member -MemberType NoteProperty -Name "Path" -Value $sharePath # Feed with the path
    $Object | Add-Member -MemberType NoteProperty -Name "Size in GB" -Value $shareSizeGB # Feed with the size
    $array += $Object # Append the result to the array

    $counter ++ # Increment the counter for console logging
}

$outCSV = "c:\$fileServer_shares.csv" # Set the path to the output CSV file
$array | ConvertTo-Csv -Delimiter "," -NoTypeInformation | out-file $outCSV # Export the result (array) to the CSV file

$outHTML = "c:\$fileServer_shares.html" # Set the path to the output HTML file
# Export the result (array) to the HTML file with a bit of formating/styling
$array | ConvertTo-Html -Head "<style>table {border-collapse: collapse;padding:5px;}table, th, td {border: 1px solid black;}th{white-space: nowrap;background-color:lightgrey;}</style>" -PreContent "<h1>$fileserver Shared Folders Report</h1><p>$count shared folders found :" -PostContent '<p><br />Should you have any concern regarding this report, please contact your awsome scripter.<br /><br />Regards,<br /><br />Your IT Server Team</p>' | Out-File $outHTML

# Send an email notification (EDIT ACCORDING TO YOUR NEED)
[string]$body = get-content $outHTML
Send-MailMessage -Attachments $outcsv,$outHTML -to "whoever@domain.com" -Body $body -BodyAsHtml -From "no-reply@domain.com" -SmtpServer "mail.domain.com" -Subject "[$fileserver] Shares Report" -Priority High

N’hésitez pas à commenter les parties dont vous n’avez pas besoin, telle que l’envoi des fichiers générés par mail.

Voici un exemple de notification mail envoyée :

Hope this helps !

Bye 😉

Laisser un commentaire