WellingtonIS
asked on
Powershell script to map a drive.
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%
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%
ASKER
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
Then your drive mapping command could look like this:
New-PSDrive –Name 'N' –PSProvider FileSystem -Credential $myCredential `
–Root "\\wrmsfs01\users\$($myCredential.UserName.substring(3))"
ASKER
OK I will try thanks.
ASKER
Not doing it. Somehow I need to copy the user name and take off the wrm and map it
ASKER
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
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
I cannot see how the code posted can lead to those errors. Please repost what you used.
ASKER
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:u sername.su bstring(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.Netw ork")
Set WshNetwork = CreateObject("WScript.Netw ork")
strPassword = "{VAR SSOPWD}"
strUser = "corp\" & "{VAR SSOUSR}"
strHomeDrivePath = "\\wrmsfs01\users$$\" & "{VAR SSOUSR}"
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:u
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.Netw
Set WshNetwork = CreateObject("WScript.Netw
strPassword = "{VAR SSOPWD}"
strUser = "corp\" & "{VAR SSOUSR}"
strHomeDrivePath = "\\wrmsfs01\users$$\" & "{VAR SSOUSR}"
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.
* 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
}
ASKER
PS C:\Users\wrmns2west\Deskto p> $drive = 'N'
$myCredential = Get-Credential
If ($myCredential) {
Remove-PSDrive -Name $drive -ErrorAction SilentlyContinue
New-PSDrive -Name $drive -PSProvider FileSystem -Root "\\wrmsfs01\users\$($env:u sername -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,Mi crosoft.Po werShell.C ommands.Ne wPSDriveCo mmand
$myCredential = Get-Credential
If ($myCredential) {
Remove-PSDrive -Name $drive -ErrorAction SilentlyContinue
New-PSDrive -Name $drive -PSProvider FileSystem -Root "\\wrmsfs01\users\$($env:u
}
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,Mi
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much!!!!
Cutting off the wrm prefix is done by using
Open in new window
Is the password required? That is, username and/or password do not match between local and remote PC?