(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 exists
if ( !( Test-Path -Path 'C:\Temp' -PathType 'Container' ) ) {
New-Item -Path 'C:\' -Name 'Temp' -ItemType 'directory'
}
# Delete an existing log file if necessary
if ( 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 share
try {
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 share
try {
New-SmbMapping -LocalPath 'H:' -RemotePath "\\CompanyServer\$Env:UserName"
}
catch {
"Error mapping drive H:`n$_" | Out-File -FilePath 'C:\Temp\login.log' -Append
}
# List all mappings
Get-SmbMapping | Out-File -FilePath 'C:\Temp\login.log' -Append
# Warn the user if (an) error(s) occurred
if ( $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: 2024-04-16; loaded in 0.0119 seconds