Avatar of Tim Williams
Tim Williams
Flag for United States of America

asked on 

Map Network Drive with Powershell

Originally I put this together (See Below).  It works to map a network drive and copy contents from one source to the other receiving so they stay in sync.  However, it doesn't work unless a user is logged into the machine.  I use a server based agent (Domino) to both create the .ps1 file and execute it via a shell command.  It doesn't work unless I run it locally or someone is actually logged into the server.  Here's the original code:

net use L: \\serverFrom\docs
net use M: \\serverTo1\DOCS_Test
net use N: \\serverTo2\DOCS_PubTest
net use O: \\serverTo3\DOCS
#robocopy L:\ M:\ /MIR /FFT /R:3 /W:10 /Z /NP /NDL /log:C:\Temp\Scripts\PSlog1.log
#robocopy L:\ N:\ /MIR /FFT /R:3 /W:10 /Z /NP /NDL /log:C:\Temp\Scripts\PSlog2.log
#robocopy L:\ O:\ /MIR /FFT /R:3 /W:10 /Z /NP /NDL /log:C:\Temp\Scripts\PSlog3.log
#net use L: /delete
#net use M: /delete
#net use N: /delete
#net use O: /delete

Open in new window

I used this code to create an encrypted credential file to use for login:
$userPassword = 'PasswordExample'
$secureString = $userPassword | ConvertTo-SecureString -AsPlainText -Force
$stringObject = ConvertFrom-SecureString
$stringObject | Set-Content -Path 'C:\Temp\Scripts\Secure.txt'

Open in new window

Then I used this script for login.  It appears to create the mapping, but not sure if there's something I'm missing here.
$userName = 'My User Name For Login'
$passwordText = 'C:\Temp\Scripts\Secure.txt'
$securePwd = $passwordText | ConvertTo-SecureString
$credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $userName, $securePwd
$cred = Get-Credential -Credential "My User Name For Login"
New-PSDrive -Name "L" -PSProvider FileSystem -Root \\serverFrom\docs -Credential $credObject -Persist

Open in new window

Is there a way to have the windows server schedule to run a particular .ps1 file?  Would that work instead of my approach?  Since my Domino server creates the .ps1 file, perhaps windows could be scheduled to run it?  As you can tell I'm not a PowerShell guy and not very comfortable with how it works in relation to windows, etc.
Thank you for your help and time and any ideas you may have.
PowershellWindows OSNetworking

Avatar of undefined
Last Comment
Seth Simmons

8/22/2022 - Mon