(view source code of login_connect_network_drives_with_logging.ps as plain text)
# It doesn't hurt to make sure the C:\temp folder existsif ( !( Test-Path -Path 'C:\Temp' -PathType 'Container' ) ) {
New-Item -Path 'C:\' -Name 'Temp' -ItemType 'directory'
}# Delete an existing log file if necessaryif ( Test-Path -Path 'C:\Temp\login.log' -PathType 'Any' ) {
Remove-Item -LiteralPath 'C:\Temp\login.log' -Force
}# Start with a clean slate$Error.Clear( )
# Map drive G: to the department sharetry {New-SmbMapping -LocalPath 'G:' -RemotePath '\\CompanyServer\Dept'
}catch {"Error mapping drive G:`n$_" | Out-File -FilePath 'C:\Temp\login.log' -Append
}# Map drive H: to the user's home sharetry {New-SmbMapping -LocalPath 'H:' -RemotePath "\\CompanyServer\$Env:UserName"
}catch {"Error mapping drive H:`n$_" | Out-File -FilePath 'C:\Temp\login.log' -Append
}# List all mappingsGet-SmbMapping | Out-File -FilePath 'C:\Temp\login.log' -Append
# Warn the user if (an) error(s) occurredif ( $Error ) {
$Msg = "Errors occurred during login.`n"
$Msg += "The errors are logged to be reviewed by the helpdesk staff.`n"
$Msg += "Please notify the helpdesk."
[void] [System.Windows.MessageBox]::Show( $Msg, "Login Error", "OK", "Warning" )
$Host.SetShouldExit( -1 )
}page last modified: 2025-10-11; loaded in 0.0084 seconds