We help IT Professionals succeed at work.
Troubleshooting Question

Powershell script to map a drive.

47 Views
Last Modified: 2020-09-17
I have a bat file to map a drive however it's not secure.  I have 2 problems, 1st problem is there is SSO on the machine that runs over a generic account and the second problem is the user name has a wrm in front of it but the map drive does not.
I have this so far
Get-PSDrive N |Remove-PS-Drive
$MyCredential = Get-Credential
New-PSDrive –Name “N” –PSProvider FileSystem –Root “\\wrmsfs01\users\$env:username from the get-credential?”

I need a variable that can take off the wrm from the user name and map the drive
This is how I have it in my bat file but I'm trying to convert it to powershell because you will not see the password with $mycredential...
set name=@%var%:~3,20%
net use N: \\wrmsfs01\users\%var:~3,20%
Comment
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
First, the target folder seems to start with a @ - is that correct?
Cutting off the wrm prefix is done by using
$myCredential.UserName.substring(3)
Is the password required? That is, username and/or password do not match between local and remote PC?

Author

Commented:
Right.  The local PC is a generic account with Single Sign on.  The user logs into Single Sign On but doesn't have access to their files.  I'm trying to map their user drives
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Then your drive mapping command could look like this:
New-PSDrive –Name 'N' –PSProvider FileSystem -Credential $myCredential `
            –Root "\\wrmsfs01\users\$($myCredential.UserName.substring(3))"

Author

Commented:
OK I will try thanks.

Author

Commented:
Not doing it.  Somehow I need to copy the user name and take off the wrm and map it

Author

Commented:
This is where I'm at

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
New-PSDrive : A positional parameter cannot be found that accepts argument '\\wrmsfs01\users\rosnei'.
At C:\Users\wrmrosnei\Desktop\LoginNDrive.ps1:3 char:1
+ New-PSDrive –Name 'N' –PSProvider FileSystem -Credential $myCredentia ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-PSDrive], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewPSDriveComma
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
This is what should map -  New-PSDrive : A positional parameter cannot be found that accepts argument '\\wrmsfs01\users\rosnei'.
At C:\Users\wrmrosnei\Desktop\LoginNDrive.ps1:3 char:1
+ New-PSDrive –Name 'N' –PSProvider FileSystem -Credential $myCredentia ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-PSDrive], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewPSDriveCommand
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
I cannot see how the code posted can lead to those errors. Please repost what you used.

Author

Commented:
I'm trying to use this.  It works on a machine that the actual use is logged into. however, with the generic account and SSO it's trying to use the generic account to map.  I'm trying to get it to copy the Crediental provided and remove the 1st 3 letters. I got it to work with a bat file and a %var but you can't encrypt the password so i can't use it.

Get-PSDrive N |Remove-PS-Drive
$MyCredential = Get-Credential
Get-PSDrive –Name 'N' –PSProvider FileSystem -Credential $myCredential
New-PSDrive -Name "N" -PSProvider FileSystem -Root "\\wrmsfs01\users\$($env:username.substring(3))"

This is the actual script I started with but it doesn't cut the user name - the user name starts with wrm and the home drive does not.
On Error Resume Next
Dim strPassword, strHomeDrivePath, objNetwork, colDrives, i, WshNetwork, strUser
Set objNetwork = CreateObject("Wscript.Network")
Set WshNetwork = CreateObject("WScript.Network")
strPassword = "{VAR SSOPWD}"
strUser = "corp\" & "{VAR SSOUSR}"
strHomeDrivePath = "\\wrmsfs01\users$$\" & "{VAR SSOUSR}"
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
There are several issues.
* Look closely at the error message: the "-" in front of "Name" and "PSProvider" are not the required "minus" (ASCII 45), but long dashes (compare with the one in "New-PSDrive").
* The cmdlet to remove a mapped drive is Remove-PSDrive, not Remove-PS-Drive
* The Get-PSDrive will fail, since there is no drive named N, and is not needed at all.
* The credentials need to be passed in New-PSDrive.
* Assuming you want to use the drive in Explorer as well, you need the "-Persist" and "-Scope Global" arguments.
$drive = 'N'
$myCredential = Get-Credential
If ($myCredential) {
	Remove-PSDrive -Name $drive -ErrorAction SilentlyContinue
	New-PSDrive -Name $drive -PSProvider FileSystem -Root "\\wrmsfs01\users\$($env:username -replace '^wrm')" -Scope Global -Persist -Credential $myCredential
}

Open in new window

Author

Commented:
PS C:\Users\wrmns2west\Desktop> $drive = 'N'
$myCredential = Get-Credential
If ($myCredential) {
      Remove-PSDrive -Name $drive -ErrorAction SilentlyContinue
      New-PSDrive -Name $drive -PSProvider FileSystem -Root "\\wrmsfs01\users\$($env:username -replace '^wrm')" -Scope Global -Persist -Credential $myCredential
}
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
New-PSDrive : The specified network resource or device is no longer available
At line:5 char:2
+     New-PSDrive -Name $drive -PSProvider FileSystem -Root "\\wrmsfs01 ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (N:PSDriveInfo) [New-PSDrive], Win32Exception
    + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
As I posted already, you need to use
$myCredential.UserName
instead of
$env:username
because otherwise you map the "SSO" user's folder instead of individual ones.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you so much!!!!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.