Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for login_connect_network_drives_with_logging.ps

(view source code of login_connect_network_drives_with_logging.ps as plain text)

  1. # It doesn't hurt to make sure the C:\temp folder exists
  2. if ( !( Test-Path -Path 'C:\Temp' -PathType 'Container' ) ) {
  3. 	New-Item -Path 'C:\' -Name 'Temp' -ItemType 'directory'
  4. }
  5.  
  6. # Delete an existing log file if necessary
  7. if ( Test-Path -Path 'C:\Temp\login.log' -PathType 'Any' ) {
  8. 	Remove-Item -LiteralPath 'C:\Temp\login.log' -Force
  9. }
  10.  
  11. # Start with a clean slate
  12. $Error.Clear( )
  13.  
  14. # Map drive G: to the department share
  15. try {
  16. 	New-SmbMapping -LocalPath 'G:' -RemotePath '\\CompanyServer\Dept'
  17. }
  18. catch {
  19. 	"Error mapping drive G:`n$_" | Out-File -FilePath 'C:\Temp\login.log' -Append
  20. }
  21.  
  22. # Map drive H: to the user's home share
  23. try {
  24. 	New-SmbMapping -LocalPath 'H:' -RemotePath "\\CompanyServer\$Env:UserName"
  25. }
  26. catch {
  27. 	"Error mapping drive H:`n$_" | Out-File -FilePath 'C:\Temp\login.log' -Append
  28. }
  29.  
  30. # List all mappings
  31. Get-SmbMapping | Out-File -FilePath 'C:\Temp\login.log' -Append
  32.  
  33. # Warn the user if (an) error(s) occurred
  34. if ( $Error ) {
  35. 	$Msg  = "Errors occurred during login.`n"
  36. 	$Msg += "The errors are logged to be reviewed by the helpdesk staff.`n"
  37. 	$Msg += "Please notify the helpdesk."
  38. 	[void] [System.Windows.MessageBox]::Show( $Msg, "Login Error", "OK", "Warning" )
  39. 	$Host.SetShouldExit( -1 )
  40. }

page last modified: 2024-04-16; loaded in 0.0225 seconds