OOP PowerShell Map Drive

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
dragu_stelian
Posts: 4
Last visit: Thu Apr 06, 2023 11:40 am

OOP PowerShell Map Drive

Post by dragu_stelian »

Hi, everyone!
I made a script for drive mapping in powershell.
  1. $UserName = "sdrxxxx"
  2. $Password = "aaaaxxxx"
  3. $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
  4. $Path = "\\192.168.1.10\D"
  5. $Credential = New-Object  System.Management.Automation.PSCredential ($UserName, $SecurePassword)
  6. New-PSDrive -Name "Y" -PSProvider FileSystem -Root $Path -Persist -Credential $Credential
I tried to pass it in OOP but the script does not run.
Can you help me ?
  1. Class MAPDrive
  2. {
  3.     [String]$UserName
  4.     [String]$Password
  5.     [string][ValidateNotNullOrEmpty()] $SecurePassword
  6.     [String]$Path
  7.     [Object]$Credential
  8.  
  9.     MAPDrive([String]$UserName, [String]$Password, [String]$Path)
  10.     {
  11.         $this.UserName = $UserName
  12.         $this.Password = $Password
  13.         $this.Path = $Path
  14.     }
  15.  
  16.     [void] Drive()
  17.     {
  18.         $this.SecurePassword = ConvertTo-SecureString $this.Password -AsPlainText -Force
  19.         $this.Credential = New-Object  System.Management.Automation.PSCredential ($this.UserName, $this.SecurePassword)
  20.         New-PSDrive -Name "Y" -PSProvider FileSystem -Root $this.Path -Persist -Credential (Get-Credential)
  21.     }
  22.  
  23. }
  24.  
  25. $box = [MAPDrive]::new("sdrxxxx", "aaaaxxxx", "\\192.168.1.10\D")
  26. $box.Drive()
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: OOP PowerShell Map Drive

Post by jvierra »

Normally we would not use a class for such a simple thing as mapping a drive. This only adds unnecessary complexity. Classes are used to simplify and generalize methods and most useful when you need multiple methods to operate on an object. You will also have "scope" issues with classes which will be lesser with a function.

As for why this code won't work, that depends on what errors you are getting. Also, not that running a mapping under a different account or under a different security context will make the drive, even if persisted, unavailable outside of the context it is created under.

Note also that you are creating a credential and not even using it. There are too many issues with this code to determine what is causing your problem or even what the problem is. My best guess is that you copied the code and have tried to modify it for some reason. Try fixing the credentials and maybe it will work. If you have errors, then post them.
dragu_stelian
Posts: 4
Last visit: Thu Apr 06, 2023 11:40 am

Re: OOP PowerShell Map Drive

Post by dragu_stelian »

I did not copy the code, I want to use classes simply to learn even if this leads to a complexity of the script.
  1. Class MAPDrive
  2. {
  3.     [String]$UserName
  4.     [String]$Password
  5.     [String]$Path
  6.     [securestring]$SecurePassword
  7.     [pscredential]$Credential
  8.  
  9.     MAPDrive([String]$UserName, [String]$Password, [String]$Path)
  10.     {
  11.         $this.UserName = $UserName
  12.         $this.Password = $Password
  13.         $this.Path = $Path
  14.     }
  15.  
  16.     [void] Drive()
  17.     {
  18.         $this.SecurePassword = ConvertTo-SecureString $this.Password -AsPlainText -Force
  19.         $this.Credential = New-Object System.Management.Automation.PSCredential ($this.UserName, $this.SecurePassword)
  20.         New-PSDrive -Name "Y" -PSProvider FileSystem -Root $this.Path -Persist -Credential $this.Credential
  21.         # (Get-Credential)
  22.     }
  23.  
  24. }
  25.  
  26. $box = [MAPDrive]::new("sdrxxxx", "aaaaxxxx", "\\192.168.1.10\D")
  27. $box.Drive()
I made some changes but I still get errors although I have disconnected all previous connections to the server or shared.
Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared
resource and try again
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: OOP PowerShell Map Drive

Post by jvierra »

If you have any connection, then you cannot map that resource just like the error says. If any resource, you use is on that drive then that constitutes a connection.

If that share is displayed anywhere in your session, then it is already being accessed by one set of credentials. Any attempt to access that share with any other set of credentials will cause that error.
dragu_stelian
Posts: 4
Last visit: Thu Apr 06, 2023 11:40 am

Re: OOP PowerShell Map Drive

Post by dragu_stelian »

After restarting and a few checks, the script runs without errors but does not map the drive.
I ran the net use command and indeed the drive is not mapped
Does anyone know why?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: OOP PowerShell Map Drive

Post by jvierra »

I posted all of the reasons why it would not be visible when mapped. If you use an admin account to map you will have to elevate a prompt to use NET USE to see the mapping.

Again, what you are trying to do we would never do in a production environment as it is unnecessary and also the method is not secure.

We would delegate the rights and permissions to the resource then use a GPO to map the drive.
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked