I'm trying to figure out how to map a file share to a particular user that I created using a Powershell script that is meant to be a service account. The end result is that my service account should be able to access the UNC path at "\\storageaccount.file.core.windows.net\share"
Below is how I create a service account through Packer's powershell provisioner.
$password = ConvertTo-SecureString "ServiceAccountPassword" -AsPlainText -Force
New-LocalUser "ServiceAccount" -Password $password -FullName "ServiceAccount"
Add-LocalGroupMember -Group "Administrators" -Member "ServiceAccount"
Because Packer executes Powershell code using the Packer generated user, I create a scheduled task to run a batch file on start up on the SYSTEM account.
"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
The below script results in a network drive created for all users, unforunately, the network drive is a disconnected drive and inaccessible when I login as the service account I just created. It would say "The username or password is incorrect."
I also tried to create the scheduled task to run as the created user.
$action = New-ScheduledTaskAction -Execute "C:\MapAzureFileShare.bat"
$trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:30
$settings = New-ScheduledTaskSettingsSet -Compatibility "Win8"
$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings
$settings -Description "Map Azure file share at startup"
Register-ScheduledTask -TaskName "MapAzureFileShare" -InputObject $task -User "ServiceAccount" -Password "ServiceAccountPassword"
Nothing gets mapped and it doesn't seem like anything happens if I manually run this scheduled task. BUT if I switch the above scheduled task to Run Only When User is Logged On and execute the task manually, the network map does get created.
If I try to run the batch file as the user I just created, the file share is mapped just fine.
Is there anything else I can try?