0

I have two powershell scripts in each of which I need to write some information into a log file. The log file is stored on a network share. In the first script everything works as it should, the information is written to a file. After that, the computer reboots and the second script starts. And now the same code that worked in the first script causes the error 'could not map network drive'. Please help me to understand what the problem is and how to fix it.

#Variables
$Global:logInfo = "...some data..."
$pass = ConvertTo-SecureString "myPassword" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("myUsername", $pass)

#Writing...
if (!(Test-Path O:)) { New-PSDrive -Name O -PSProvider FileSystem -Root \\192.168.1.21\SharedData$ -Credential $creds }
Out-File -FilePath "O:\Logs\myLog.log" -InputObject $Global:logInfo -Encoding UTF8 -Append
  • are you sure that the target share is available at the point when the 2nd script runs? is the 2nd script run with the same account as the 1st? – Lee_Dailey May 20 '19 at 09:44
  • Unfortunately, it turned out that I made a typo in the name of the resource and this was the reason. Thanks for your help! – Alf of Melmac May 20 '19 at 10:37
  • ah! that makes sense ... and i have done the same more than once. glad you got it working! [*grin*] – Lee_Dailey May 20 '19 at 11:23

1 Answers1

0

Try adding the switch -persist to the New-PSDrive cmdlet.

Sid
  • 2,586
  • 1
  • 11
  • 22