2

I am trying to access a domain network share in my PowerShell script that is currently running as NETWORK SERVICE. I have a domain user credential configured below.

$secStringPassword = ConvertTo-SecureString "password" -AsPlainText -Force
$shareCredential = New-Object System.Management.Automation.PSCredential ("DOMAIN\Username", $secStringPassword)

I would like to be able to run the following commands in the PowerShell script as the user specified above.

New-Item -Path "\\SERVER\Share\Folder" -ItemType Directory
Get-ChildItem "\\SERVER\Share\Folder"

Running the below is showing as not supported:

New-Item -Path "\\SERVER\Share\Folder" -ItemType Directory -Credential $shareCredential 
2
  • Do you intend to leave the $secStringPassword var equal to System.Security.SecureString or do you wish to decrypt it in another section of your script Commented May 23, 2020 at 6:38
  • @NekoMusume I'm just going to leave it as is. I only need it to access the network share. Commented May 23, 2020 at 7:26

1 Answer 1

1

"The New-Item cmdlet creates a new item and sets its value" If you're trying to connect to a share as a different user I suggest using new-psdrive first to create a mount as that user. This mounts that share as a drive so that it behaves more like a local location than a UNC path

New-PSDrive -Name "ShareNAME" -PSProvider "FileSystem" -Root "\\Server\Share" -Credential $shareCredential
New-Item -Path ShareNAME:\Folder -ItemType Directory -Credential $shareCredential
Get-ChildItem ShareNAME:\Folder -Credential $shareCredential
Sign up to request clarification or add additional context in comments.

4 Comments

I tried using the below, but I am getting the following error. New-PSDrive -Name "PSDriveShare" -PSProvider "FileSystem" -Root "\\SERVER.sub.domain.com\Share\Folder" -Credential $shareCredential New-PSDrive : 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
This is exactly what it says. Unfortunately as far as I have been able to find, it is not possible to connect to a remote server share with multiple credentials simultaneously. If you know what share you have mapped with your standard user you can disconnect that share then connect with these credentials. After you can reconnect with your standard user.
There is a GPO that is adding the Network Share on some machines, I am currently on a machine where this mapping is not present now so I can test further. Will report back soon.
Since GPO maps as part of the startup /login process you should be able to unmap then remap after script execution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.