impact/Connect-ImpactLegacyDrives.ps1

function Connect-ImpactLegacyDrives {

$curUser = whoami

[int]$DrivesMapped = 0
#Setup for renaming the mapped drives in Explorer.
$shell = New-Object -ComObject Shell.Application

#Array for all drives that the script will attempt to map. Edit as needed.
$ImpactDrives = @(
    ("I","\\Rackstation1\IT","IT"),
    ("Q","\\IMPACT007\Finance","Finance"),
    ("B","\\IMPACT007\Radio_Billing","Radio Billing"),
    ("S","\\IMPACT007\AdvMkts","AdvMkts"),
    ("M","\\IMPACT007\AnnuityNewBiz","Annuity New Business"),
    ("L","\\IMPACT007\Licensing","Licensing"),
    ("O","\\IMPACT007\Life","Life"),
    ("N","\\IMPACT007\LifeNewBiz","Life New Business"),
    ("G","\\IMPACT007\Marketing","Marketing"),
    ("H","\\IMPACT007\OpsManagement","Operations Management"),
    ("P","\\IMPACT007\Playbook","Playbook"),
    ("R","\\IMPACT007\Radio","Radio")
    )

#Are we on the Impact Network?
try 
    {
    Test-Connection -ComputerName IMPACT001.impact.com -ErrorAction Stop -Count 1 >$null
    Write-Host "Connected to IMPACT network. Attempting to Impact Drives.`n" -ForegroundColor Green
    }
catch
    {
    Write-Host "Not on IMPACT network. Exiting" -ForegroundColor Red
    return
    }

#Begin mapping drives if so.
foreach ($row in $ImpactDrives) 
    {
    #For this drive, is it one we have permissions to access?
    try
        {
        Get-ChildItem $row[1] -ErrorAction Stop >$null
        }
    catch [System.UnauthorizedAccessException]
        {
        Write-Host $curUser "doesn't have permission to access" $row[1] -ForegroundColor Red
        Continue
        }
    catch 
        {
        Write-Host "Unable to map" $row[1] -ForegroundColor Red
        Continue
        }
    #Map the Drive
    try
        {
        New-PSDrive –Name $row[0] –PSProvider FileSystem –Root $row[1] –Persist -ErrorAction Stop -Scope global >$null
        }
    catch [System.ComponentModel.Win32Exception]
        {
        $CurrentDrive = Get-PSDrive $row[0]
        If ($CurrentDrive.DisplayRoot -eq $row[1])
            {
            Write-Host $row[1] "is already mapped to" $row[0] -ForegroundColor Yellow
            }
        Else
            {
            Write-Host $row[0] "is already taken in Windows Explorer." -ForegroundColor Yellow
            }
        Continue
        }        
    catch
        {
        Write-Host "Something went wrong mapping" $row[1]"." -ForegroundColor Red
        Continue
        }
    #Rename the drive
    $shell.NameSpace($row[0]+":").Self.Name = $row[2]
    Write-Host $row[2] "has been be mapped." -ForegroundColor Green
    $DrivesMapped += 1
    }

    #Check if ANY drives got mapped.
    If ($DrivesMapped -eq 0)
        {
        Write-Host "`nNo Drives Mapped" -ForegroundColor Yellow
        }
    Else
        {
        Write-Host "`n"$DrivesMapped "drives mapped." -ForegroundColor Green
        }
}