Jump to content

Recommended Posts

Posted (edited)

I'm currently working on a script to map network drives on local laptop accounts, where it prompts the user for their credentials and maps the drive using them. I have it working, where it maps the drive and it is browsable...but it stops working if I log off and back on again. The mapped drive actually stays, but it has a red X and states the username or password is incorrect and prompts for a password (with the username pre-filled). I am using the NET USE command with the /persistent:yes switch, but it's not working as expected.

 

Update: It's actually happening when I use NET USE in the traditional way, without any extra weirdness. It seems to be related to the fact that I'm using credentials that do not match the user I'm logged on with.

 

$Credentials = Get-Credential -Message "Please enter your computer credentials" #Get user's AD credentials
$Username = "DomainName\" + $Credentials.Username #Get the username from specified credentials and append the domain name at the start
$SecurePassword = New-Object System.Management.Automation.PSCredential ($Username, $Credentials.Password) #Get password from saved credentials in a secure string
$UnsecurePassword = $SecurePassword.GetNetworkCredential().Password #Convert password to plain text
$HomeFolder = "\\domain.fqdn\staffhome$\" + $Credentials.Username + "\Documents" #Specify the path for the home folder, using the username from the AD credentials entered
$StaffShare = "\\domain.fqdn\staffshare$" #Specify the path for the staff shared area

If (!(Test-Path "C:\Program Files (x86)\Sophos\Connect\GUI\scgui.exe")) {
   Write-Output "Sophos Connect is not installed. Please install the software and try again."}
Else {
   Write-Output "Copying Sophos configuration file..."
   xcopy "C:\Program Files (x86)\Sophos\Connect\VPN.scx" "C:\Program Files (x86)\Sophos\Connect\Import\" /Y #Copy Sophos Connect configuration file to the Import folder in Program Files
   
   Write-Output "Mapping H: drive..."
   Write-Output $HomeFolder
   Net Use H: $HomeFolder /user:$Username $UnsecurePassword /persistent:yes #Map H: drive Documents folder using the user's credentials to authenticate
   
   Write-Output "Mapping J: drive..."
   Write-Output $StaffShare
   Net Use J: $StaffShare /user:$Username $UnsecurePassword /persistent:yes} #Map J: drive Staff Share folder using the user's credentials to authenticate

The reason I'm not using New-PSDrive is because the -persist flag doesn't save the mapped drive and it disappears after log off, even with the -Scope Global flag. It seems this is a quirk of mapping drives with a different user's credentials than the one who is logged on. Also, the reason for the weird credentials bit is because I want the user to be prompted to enter it...but it saves it as a secure string and NET USE fails to map it, so I need to convert it to plain text.

Edited by CHiLL
Posted (edited)

Try changing /persistent:yes to /persistent:no - that shouldn't then retain the shared drive and will remap when you log back on.

 

Alternatively add a net use /delete H: and net use /delete J: to the top of your script. :)

 

Or have I misunderstood - do you only want to run the script once? If so, try not prompting for the credentials and using /savecred in your script or a combination of commands from here: https://stackoverflow.com/questions/22680957/mapping-a-network-drive-and-having-trouble-saving-password

Edited by Cache

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...