Advertisement
Sweetening

Untitled

Dec 18th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. # PowerShell script to automate network drive mapping without manual user interaction
  2.  
  3. # Set network path and default drive letter
  4. $networkPath = "\\192.168.1.3\Documents" # Change this to the correct network path
  5. $driveLetter = "Z:" # Drive letter to map
  6.  
  7. # Function to check if a drive letter is already in use
  8. function Test-DriveLetter {
  9. param (
  10. [string]$driveLetter
  11. )
  12. $drive = Get-PSDrive -Name $driveLetter -ErrorAction SilentlyContinue
  13. return $drive -ne $null
  14. }
  15.  
  16. # Function to create and securely handle user credentials
  17. function Get-UserCredentials {
  18. # Prompt user for credentials if needed
  19. $username = Read-Host "Enter your network username"
  20. $password = Read-Host "Enter your network password" -AsSecureString
  21. return New-Object System.Management.Automation.PSCredential ($username, $password)
  22. }
  23.  
  24. # Function to test network connection to the remote server
  25. function Test-NetworkConnection {
  26. param (
  27. [string]$networkPath
  28. )
  29. $hostname = $networkPath.Split("\")[2]
  30. try {
  31. $pingResult = Test-Connection -ComputerName $hostname -Count 1 -Quiet
  32. if ($pingResult) {
  33. Write-Host "Network connection to $hostname is successful."
  34. return $true
  35. } else {
  36. Write-Host "Error: Cannot reach $hostname."
  37. return $false
  38. }
  39. } catch {
  40. Write-Host "Error: $($_.Exception.Message)"
  41. return $false
  42. }
  43. }
  44.  
  45. # Function to map the network drive
  46. function Map-NetworkDrive {
  47. param (
  48. [string]$driveLetter,
  49. [string]$networkPath,
  50. [System.Management.Automation.PSCredential]$credential
  51. )
  52. # Check if drive letter is already in use
  53. if (Test-DriveLetter -driveLetter $driveLetter) {
  54. Write-Host "Error: Drive letter $driveLetter is already in use. Please choose another one."
  55. return
  56. }
  57.  
  58. # Check network connection
  59. if (Test-NetworkConnection -networkPath $networkPath) {
  60. try {
  61. # Attempt to map the network drive
  62. New-PSDrive -Name "MappedDrive" -PSProvider FileSystem -Root $networkPath -Persist -Scope Global -Credential $credential
  63. Write-Host "Network drive mapped successfully to $driveLetter."
  64. } catch {
  65. Write-Host "Error mapping network drive: $($_.Exception.Message)"
  66. }
  67. } else {
  68. Write-Host "Error: Unable to reach the network path $networkPath."
  69. }
  70. }
  71.  
  72. # Main script execution
  73. Write-Host "Starting network drive mapping process..."
  74.  
  75. # Prompt for credentials
  76. $credential = Get-UserCredentials
  77.  
  78. # Call the function to map the network drive
  79. Map-NetworkDrive -driveLetter $driveLetter -networkPath $networkPath -credential $credential
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement