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