Checking Active Network Shares within Domain using PowerShell Script
Network administration is an essential aspect of any organization relying on a Local Area Network (LAN) to share resources among its users. Network shares, in particular, are crucial as they enable users to access and collaborate on files stored in central locations. However, managing these shares can be challenging, especially when dealing with a large number of shares across the domain.
Introduction
This article presents a PowerShell script for checking the active status of network shares listed in a CSV file within a domain environment. By active, we mean that the share path should be available, accessible, and not have any permission issues when queried using the script.
Requirements
To execute the script successfully, ensure the following:
- The computer running the script has Active Directory (AD) PowerShell Module installed for domain-related functions.
- The CSV file containing the list of shares is formatted with two columns: ShareName and SharePath.
Script Overview
The script will perform the following tasks:
- Import the list of shares from a CSV file.
- Iterate through the list to check the status of each share.
- Perform error handling if a share is inaccessible or unavailable.
Script Walkthrough
Importing the CSV file
The following lines of code import the CSV file and create two arrays $ShareName and $SharePath, which will be used throughout the script:
$csv = Import-Csv -Path <CSV file path>
[System.Collections.ArrayList]$ShareName = @()
[System.Collections.ArrayList]$SharePath = @()
foreach ($line in $csv)
{
$ShareName.Add($line.ShareName)
$SharePath.Add($line.SharePath)
}
Iterating through the list of shares and checking their status
The script then iterates through the arrays using a for loop, where the Get-Share cmdlet is executed over each share path:
for ($i=0; $i -lt $ShareName.Count; $i++)
{
try
{
Get-Share -Path $SharePath[$i]
$ActiveShares = $ActiveShares + $SharePath[$i]
}
catch
{
< Handle exception for inactive or unavailable SharePath >
}
}
This script can considerably ease network administrators' burden by providing them with an accurate inventory of active shares within the domain. By examining the list of shares, administrators can quickly identify potential problematic shares for further review or modification as needed. Additionally, the script can be used as a foundation for more complex inventory management tools that can help maintain network shares' health at scale.
References
-
PowerShell for Sysadmins: A Hands-On Guide to Automating Your Workflow
Adam Bertram