Mapping AFS Paths Powershell Example

Powershell: Look up the location of a users AFS home directory in AD then map their J: and K: drives to the appropriate locations with error checking.

[powershell] ## Script to map users K: and J: drives.
## K: drive will map to the users home directory after a uintyid lookup
## J: drive will map to \\afs\all. This is hardcoded.

## Find unityid, establish a connection to the wolftech AD, and build searcher
$user = whoami
$unityid = $user.remove(0,9)
$prop = “cn=$unityid”
$objDomain = New-Object system.directoryservices.directoryentry(“LDAP://ou=people,dc=wolftech,dc=ad,dc=ncsu,dc=edu”)
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = “$prop”
$results = $objSearcher.findone()
$Tokens = $null
$userToken = $null

## Function for creating balloon notifications
## Show-BalloonTip code was taken from following website
### http://powersheller.wordpress.com/2011/09/26/powershell-prompt-user-to-close-running-applications/ ###
Function Show-BalloonTip {
Param(
[Parameter(Mandatory = $true, Position = 0)] [ValidateNotNull()] [String] $BalloonTipText,
[Parameter(Position = 1)] [String] $BalloonTipTitle = ‘PowerShell Event Notificaton’,
[Parameter(Position = 2)] [ValidateSet(‘Error’, ‘Info’, ‘None’, ‘Warning’)] [String] $BalloonTipIcon = ‘Info’,
[Parameter(Position = 3)] [Int] $BalloonTipTime = 1000
)
end {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[Windows.Forms.ToolTipIcon]$BalloonTipIcon = $BalloonTipIcon
$NotifyIcon = New-Object Windows.Forms.NotifyIcon -Property @{
BalloonTipIcon = $BalloonTipIcon
BalloonTipText = $BalloonTipText
BalloonTipTitle = $BalloonTipTitle
Icon = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path)
Text = -join $BalloonTipText[0..62] Visible = $true
}
switch ($BalloonTipIcon) {
Error {[Media.SystemSounds]::Hand.Play()}
Info {[Media.SystemSounds]::Asterisk.Play()}
None {[Media.SystemSounds]::Beep.Play()}
Warning {[Media.SystemSounds]::Exclamation.Play()}
}
$NotifyIcon.ShowBalloonTip($BalloonTipTime)
switch ($Host.Runspace.ApartmentState) {
STA {
$null = Register-ObjectEvent -InputObject $NotifyIcon -EventName BalloonTipClosed -Action {
$Sender.Dispose()
Unregister-Event $EventSubscriber.SourceIdentifier
Remove-Job $EventSubscriber.Action }
}
default {
continue
}
}
}
}

## Begin drive mapping
if ($results -eq $null) {
$balloonText = “This is not a valid Unity ID. Your K: drive was not mapped”
Show-BalloonTip -BalloonTipIcon “Warning” -BalloonTipText $balloonText -BalloonTipTitle “OpenAFS Event Notification” -BalloonTipTime 2000
}

elseif ((Get-Service TransarcAFSDaemon).status -ne “running”) {
$balloonText = “The OpenAFS Service is not running.”
Show-BalloonTip -BalloonTipIcon “Warning” -BalloonTipText $balloonText -BalloonTipTitle “OpenAFS Event Notification” -BalloonTipTime 2000
}

else {
$afsdir = $results.properties.ncsuafspath
$homedir = “\” + $afsdir -replace(“/”,”\”)

$loopTimeout = 0

do {
Clear-Variable Tokens
Clear-Variable userToken
$Tokens = tokens.exe

$Tokens | foreach {
$userToken += @($_.Length)
}

Start-Sleep -Seconds 1
$loopTimeout++
}

until ($userToken[3] -gt 20 -or $loopTimeout -ge 120)

if ( $loopTimeout -lt 120) {
Start-Sleep -Seconds 2
Try {
$netK = $(New-Object -ComObject wscript.Network)
$netK.MapNetworkDrive(“K:”,”$homedir”)
}

Catch {
$balloonText = “The K: drive failed to map.”
Show-BalloonTip -BalloonTipIcon “Warning” -BalloonTipText $balloonText -BalloonTipTitle “OpenAFS Event Notification” -BalloonTipTime 2000
}
Try {
$netJ = $(New-Object -ComObject wscript.Network)
$netJ.MapNetworkDrive(“J:”,”\\afs\all”)
}
Catch {
$balloonText = “The J: drive failed to map.”
Show-BalloonTip -BalloonTipIcon “Warning” -BalloonTipText $balloonText -BalloonTipTitle “OpenAFS Event Notification” -BalloonTipTime 2000
}
}

else {
$balloonText = “OpenAFS was not able to obtain tokens. Your K: drive was not mapped.”
Show-BalloonTip -BalloonTipIcon “Warning” -BalloonTipText $balloonText -BalloonTipTitle “OpenAFS Event Notification” -BalloonTipTime 2000
}
}
[/powershell]