Marc Rufer bio photo

Marc Rufer

Software engineer
@d-fens GmbH

Twitter LinkedIn Github Stackoverflow
RSS Feed

HOWTO Access Azure File Share in PowerShell Script executed by a Scheduled Task

Marc Rufer 14 Nov 2017 Azure PowerShell Scheduled-Task

Last week I had to set up an Azure File Share for data transfer purposes. Clients will upload data to the Azure File Share and the data will then be moved from the share to a local drive by a PowerShell script. The PowerShell script gets invoked every 15 minutes by a Scheduled Task.

The setup worked as expected but during the first tests a problem occurred. The script could not access the Azure File Share, that got mounted before with PowerShell. Opening a new PowerShell console and accessing Azure File Share worked fine; even if starting PowerShell as administrator. It seems that the Scheduled Task gets executed under a different security context even if the specified user account gets used when running the task. I solved the problem by mounting the Azure File Share (if not yet mounted) in the script using credentials from a configuration file.

In the next sections I’ll explain how I set up the Azure File Share and the Scheduled Task and how the Azure File Share gets mounted in the data transfer script.

Azure File Share

To create an Azure File Share I just followed the official documentation.

  1. Create a storage account
  2. Create a file share in Azure Files

After creation the Azure File Share can be mounted in Windows with File Explorer, Command Prompt or PowerShell (for details see Mount an Azure File share and access the share in Windows). I mounted the Azure File Share under the actual user with PowerShell and could successfully access the share using File Explorer and PowerShell.

PowerShell Script for Data Transfer

First the connection credentials to mount the Azure File Share have to be exported to a configuration file.

  1. Open PowerShell console as administrator
  2. Create a credential object (Username and password according section Mount the Azure File share with File Explorer of Mount on Windows) and export the credential to a configuration file

     $cred = Get-Credential;
     $cred | Export-Clixml C:\PATH\TO\DataTransferConfig.xml;

In the data transfer script itself the credential can then be loaded using the following code snippet.

$azureCredential = Import-CliXml $PathToConfigFile;

The $PathToConfigFile can either be specified in the script itself or can be passed to the script via a parameter.

Now the Azure File Share can be mounted in the script using the following code, where $unc contains the path to the Azure File Share (i.e \\servername\azure-file-share-name).

if (!(Test-Path -Path $unc))
    New-PSDrive -Name "Y" -PSProvider "FileSystem" -Root $unc -Credential $azureCredential -Persist;

Scheduled Task

Last but not least the Scheduled Task, that executes the data transfer script, has to be created.

  1. Open Task Scheduler
  2. Create new task

    Action > Create Task...

  3. General Settings
    • Enter Name
    • Select Run wheter user is logged in or not
    • Tick Run with highest privileges

    Scheduled Task Azure File Share - General

  4. Create new trigger

    Scheduled Task Azure File Share - Trigger

  5. Create new action

    Scheduled Task Azure File Share - Action

    Add arguments: -ExecutionPolicy RemoteSigned C:\PATH\TO\Move-Data.ps1 -PathToConfigFile C:\PATH\TO\DataTransferConfig.xml

comments powered by Disqus


HOWTO Access Azure File Share in PowerShell Script executed by a Scheduled Task BUG "Task Scheduler service is not available" Error, if specifying Network Connection Condition HOWTO Sync OneDrive on Server even if Windows User not logged in HOWTO Access Microsoft Access Database with PowerShell Get NetworkCredential from PSCredential splits Username into Username and Domain HOWTO Create LocalDB File (.mdf) manually in Visual Studio 2015 HOWTO Set Cookie Header on DefaultRequestHeaders of HttpClient HOWTO Set up PowerShell Remote Session Configuration HOWTO Analyze PowerShell Scripts with PSScriptAnalyzer HOWTO Sign PDF with SuisseID NoBrainer PUT/PATCH/MERGE with OData Service Client Apply Commit from one Repository to another GitHub Repository creation with PowerShell HOWTO Sign PowerShell Script with SuisseID Swissunihockey game schedule PDF generator HOWTO Maven Release on JetBrains TeamCity Limitations when running Activiti in H2 Embedded Mode HOWTO Pylint Integration in IntelliJ 14 HOWTO Install Windows 10 IoT Core on Raspberry PI 2 from a VM First steps with Dropwizard Allman code style for IntelliJ First steps with Flyway IntelliJ - Setup custom inspection profiles Configuration properties meta-data support in IntelliJ AMQP Integration with spring integration OCA Java SE 7 Programmer I Certification Guide Swissunihockey key matcher Swisscom SMS-API-Client Some hints concerning logging with Log4j IFTTT A brilliant service Run deltacloud on a VirtualBox-VM Project specific maven settings in IntelliJ AngularJS Javascript for beginners Liferay in Action