Hi, I have some files that I have to copy daily in a network drive for backup. The network drive is already mapped and I can access it with the credentials on Windows Explorer, but the Powershell script is giving me errors.
First thing to point out is that if I log in via Windows Explorer first Copy-Item works fine, the problem is when is Powershell who has to manage the login.
Instead of tinkering around with New-PsDrive and credentials Iâd grant the needed rights to the account running the script and avoid all that clutter. Thatâll make your life much much easier.
BTW: Do you really store the password in your code in plain text?
The drive I need to access requires a username and password that cannot be stored (I donât care much if they are in plain sight, the code is in a secure server anyway and the destination folder is on a NAS in the office).
I see something that could be the issue. Your share on the server has a space in the name. This could be confusing the commands. From my admin days, I always avoided spaces in share names specifically because they made my life more difficult. Might not be the issue, but it raises a red flag for me.
Also, I would strongly encourage you to not get into the habit of storing plain text passwords in code. Itâs too easy to slip up and put important credentials somewhere insecure.
This can be set up without a password - so itâs secured by your Windows authentication, which is far more secure than plain text and still usable by automation.
I agree with the other comments, there are many better alternatives then storing the password in plain text. One thing to consider is any powershell code ran on a system is logged in eventviewer meaning your plain text password would show in there as well. So you may have the script and server locked down but the risk for that password being exposed through other means goes up exponentially by storing the password in plain text. If you must use username/password, look into the secret management that Darwin suggested or look at Export-Clixml.
As far as the issue goes, I donât have any problems with New-PsDrive and based on the error it appears the path only has one leading slash
That could be caused by the formatting of the forum software. @Darkveemon1 used âQuoteâ instead of âpreformatted textâ (code) ⌠usually that swallows one of the backslashs. As you can see in the code formatted correctly there are 2 leading backslashs.
Then my next suspicion is maybe OP has improper quotes around the path. If any code was copy/pasted this can occur easily, as I know youâre aware of Olaf. OP, try retyping all your quotes manually in your code and see if you get the same error. If youâre sure the path exists, then there has to be some explanation for the error.
When you encounter errors in freshly written code, itâs a good idea to insert Set-StrictMode -Version Latest at the beginning of the code.
Earlier versions of PowerShell used Set-PSDebug -Strict.