Forum Discussion

JamesA1005's avatar
JamesA1005
Copper Contributor
Oct 07, 2024

Azure scheduled task powershell script not running on remote app but runs on full desktop

Hello,

Just wondering if anyone has any thoughts or can help. I am running a powershell script to map a drive on an azure virtual desktop. The script will map the drive if I log in to a full desktop. If I login to a remote app the script does not map the drive. I can see the script window pop-up, but the drive does not get mapped. I've tried both Azure authentication and storage account keys.

It is an old app and trying to use the locally mapped drives (provided by the RDP App) will cause the session to hang for around 15 minutes as I believe it's trying to use local storage to create the report. Local storage takes seconds.

Thanks

6 Replies

  • JamesA1005's avatar
    JamesA1005
    Copper Contributor
    Hello,

    I just wanted to post that I have now resolved this issue. Thanks for all your help. It ended up being an issue with the privileges on the scheduled task. I am still unsure why this would work differently from app to desktop. It may have been down to myself, but thanks for helping me.
  • JamesA1005's avatar
    JamesA1005
    Copper Contributor
    I am using this for the scheduled task
    -executionpolicy bypass -noprofile -file drivemap.ps1

    Thanks
  • JamesA1005 

     

    Try below script includes logging, please make sure fully understand the script before apply:

     

    $logFile = "C:\logfile.txt"
    try {
    Add-Content -Path $logFile -Value "Starting drive mapping at $(Get-Date)"
    New-PSDrive -Name "X" -PSProvider FileSystem -Root "\\yourstorageaccount.file.core.windows.net\yourfileshare" -Persist -Credential (Get-Credential)
    Add-Content -Path $logFile -Value "Drive mapping successful at $(Get-Date)"
    } catch {
    Add-Content -Path $logFile -Value "Error during drive mapping: $_"
    }

    • JamesA1005's avatar
      JamesA1005
      Copper Contributor
      Hi I am currently running the script that MS provides. I have tried both the AD and key scripts, They both work for the full desktop but not the remote app. I cant see much difference in running the other script, maybe just for error logs?

      $connectTestResult = Test-NetConnection -ComputerName fiesharename.file.core.windows.net -Port 445
      if ($connectTestResult.TcpTestSucceeded) {
      # Save the password so the drive will persist on reboot
      cmd.exe /C "cmdkey /add:`"filesharename.file.core.windows.net`" /user:`"localhost\oscarexports`" /pass:`"Cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==`""
      # Mount the drive
      New-PSDrive -Name Z -PSProvider FileSystem -Root "\\filesshare.file.core.windows.net\oscarexports" -Persist
      } else {
      Write-Error -Message "Unable to reach the Azure storage account via port 445. Check to make sure your organization or ISP is not blocking port 445, or use Azure P2S VPN, Azure S2S VPN, or Express Route to tunnel SMB traffic over a different port."
      }
  • JamesA1005 

     

    Please try these option to resolve the issue


    1. Execution Context: RemoteApp may not run scripts in the same user context. Try setting up the script to run using Task Scheduler when the user logs in.

    2. Use New-PSDrive: Change your script to use New-PSDrive with the -Persist option. This can help with drive visibility in RemoteApp.

    3. Check Execution Policy: Make sure PowerShell’s execution policy allows your script to run.

    4. Log Errors: Add logging to your script to capture any errors

     

    I hope this helps.

    Many Thanks

    Balasubramani.M

    • JamesA1005's avatar
      JamesA1005
      Copper Contributor
      Hi Thanks for the reply. In reposonse to your points

      1. Execution Context: RemoteApp may not run scripts in the same user context. Try setting up the script to run using Task Scheduler when the user logs in.

      This is how I am running the script

      2. Use New-PSDrive: Change your script to use New-PSDrive with the -Persist option. This can help with drive visibility in RemoteApp.

      I am using the -persist option. If the user logs in to the desktop the mapping stays and works for future login's


      3. Check Execution Policy: Make sure PowerShell’s execution policy allows your script to run.

      The script works fine for the full desktop and I've tested with a storage account key which still doesn't work

      4. Log Errors: Add logging to your script to capture any errors
      I will try setting this up

Resources