I’m trying to map a file share to a service account I created with a Powershell script. I created a scheduled task to run a batch file on start up on the SYSTEM account, but the file share is not accessible when I log in as the service account. When I switch the scheduled task to Run Only When User is Logged On and execute the task manually, the network map is created. If I run the batch file as the user I just created, the file share is mapped successfully. Is there anything else I can try?
$password = ConvertTo-SecureString "ServiceAccountPassword" -AsPlainText -Force
New-LocalUser "ServiceAccount" -Password $password -FullName "ServiceAccount"
Add-LocalGroupMember -Group "Administrators" -Member "ServiceAccount"
"net use Z: \\storageaccount.file.core.windows.net\share azurestorageaccesskey /user:Azure\storageaccount /persistent:yes" | Out-File -FilePath "C:\MapAzureFileShare.bat" -Encoding "ASCII"
$action = New-ScheduledTaskAction -Execute "C:\MapAzureFileShare.bat"
$trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:30
$settings = New-ScheduledTaskSettingsSet -Compatibility "Win8"
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType "ServiceAccount" -RunLevel "Highest"
$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settings -Description "Map Azure file share at startup"
Register-ScheduledTask -TaskName "MapAzureFileShare" -InputObject $task