How to setup PowerShell script that checks the server status and maps or unmaps the shared folder accordingly:

Share this post on:
# Define server and shared folder details
$serverName = "ServerName"
$sharePath = "\\$serverName\SharedFolder"
$localDriveLetter = "Z:"


# Function to check if server is online
function Test-ServerOnline {
    param (
        [string]$server
    )
    try {
        $ping = Test-Connection -ComputerName $server -Count 1 -Quiet
        return $ping
    } catch {
        return $false
    }
}


# Function to map the shared folder

function Map-SharedFolder {
    param (
        [string]$path,
        [string]$driveLetter
    )
    if (!(Get-PSDrive -Name $driveLetter -ErrorAction SilentlyContinue)) {
        New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $path -Persist
        Write-Host "Shared folder mapped to $driveLetter"
    } else {
        Write-Host "Shared folder is already mapped to $driveLetter"
    }
}


# Function to unmap the shared folder
function Unmap-SharedFolder {
    param (
        [string]$driveLetter
    )
    if (Get-PSDrive -Name $driveLetter -ErrorAction SilentlyContinue) {
        Remove-PSDrive -Name $driveLetter -Force
        Write-Host "Shared folder unmapped from $driveLetter"
    } else {
        Write-Host "Shared folder is not mapped to $driveLetter"
    }
}


# Main script logic
if (Test-ServerOnline -server $serverName) {
    Map-SharedFolder -path $sharePath -driveLetter $localDriveLetter
} else {
    Unmap-SharedFolder -driveLetter $localDriveLetter
}

Setting Up the Task Scheduler

  1. Open Task Scheduler:

    • Press Win + R, type taskschd.msc, and press Enter.
  2. Create a New Task:

    • In the right pane, click on Create Task.
  3. General Tab:

    • Name the task (e.g., “Manage Shared Folder Mapping”).
    • Set it to run whether the user is logged on or not.
    • Run with highest privileges.
  4. Triggers Tab:

    • Add a new trigger.
    • Set it to begin the task At startup.
    • Add another trigger for On an event, select Log: System, Source: User32, and Event ID: 1074 (shutdown event).
  5. Actions Tab:

    • Add a new action.
    • Action: Start a program.
    • Program/script: powershell.exe.
    • Add arguments: -File "C:\Path\To\YourScript.ps1" (replace with the actual path to your PowerShell script).
  6. Conditions Tab:

    • You can set additional conditions if needed, but the default should be fine.
  7. Settings Tab:

    • Ensure the task is allowed to be run on demand and the other default settings are appropriate.

Script Deployment

  • Save the PowerShell script to a location accessible by the Task Scheduler, e.g., C:\Scripts\ManageSharedFolder.ps1.
  • Ensure the script file has appropriate permissions to be executed by the Task Scheduler.

Final Steps

  • Test the task by manually running it from the Task Scheduler to ensure it works as expected.
  • Monitor the task to ensure it behaves correctly during actual server reboots and shutdowns.

Loading