Mar
02
2007

PowerShell Script: Map All Client Drives

One weakness of the Citrix client drive mapping is that you can’t automatically connect a client’s network drives. For some reason, Citrix don’t supply an option to do this, even if you want to - you’re supposed to write a script.

I’ve written the below script in PowerShell that will go through and map all client drive letters that aren’t already mapped. To run it, you’ll need PowerShell installed on your Citrix Servers. Get it here.

Save the below script as “mapClientDrives.ps1″ and execute it via PowerShell. Wordpress is being a pain and not letting me upload the script.

# mapClientDrives.ps1
#
# Maps all client drives in Citrix, including network drives,
# which are not mapped by default.
#
# Author: James Kahn, http://liet.net/blog
# Version: 1.0

$target = $args

if ($target.length -eq 0) {
Write-Host “Maps all client drives that aren’t currently mapped.”
Write-Host “Usage: mapClientDrives.ps1 ”
Write-Host “On Citrix servers, you should use ‘mapClientDrives.ps1 CLIENT’”
}

$defaultDrives = “C:”,”D:”,”E:”,”F:”,”G:”,”H:”,”I:”,”J:”,”K:”,”L:”,”M:”,”N:”,”O:”,”P:”,”Q:”,”R:”,”S:”,”T:”,”U:”,”V:”,”W:”,”X:”,”Y:”,”Z:”

# Get a list of the current logical drives
Get-WmiObject Win32_LogicalDisk | % {$drives = @{}}{$drives.add($_.DeviceID,$_.ProviderName)}

# Build list of available drives
$availDrives = @()
$i = 0
do {
if ($drives.Contains($defaultDrives[$i])) {
# Do Nothing
}
else {
$availDrives += ,($defaultDrives[$i])
}
$i++
}
while ($defaultDrives[$i])

# Map Client Drives

$i = 0
$j = 0
$erroractionpreference = “SilentlyContinue”
do {
$curTarget = “\\” + $target + “\” + $defaultDrives[$i][0] + “$”
$error.Clear()
$oldErrCount = $error.Count
if ($drives.ContainsValue($curTarget)) {
$i++
continue
}
$net = $(New-Object -Com WScript.Network)
$net.MapNetworkDrive($availDrives[$j], $curTarget)
$i++
if ($error.Count -eq $oldErrCount) {
Write-Host “Mapped” $availDrives[$j] “to” $curTarget
$j++
}
}
while ($defaultDrives[$i])

Written by James Kahn in: tech |

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes