troubleshooting Question

Map Network Drive with Powershell

Avatar of Tim Williams
Tim WilliamsFlag for United States of America asked on
PowershellWindows OSNetworking
4 Comments1 Solution22 ViewsLast Modified:
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.
ASKER CERTIFIED SOLUTION
kevinhsieh
Network Engineer
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 4 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 4 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004