nadk
nadk

Reputation: 11

Az Copy Scheduled Task Created but not executing script

I am using a powershell script that will read in a service principal and use it to run azcopy sync:

$StorageAccountName = 'nkstgacct'
$ContainerName = 'netangularproject'

$StorageURL = 'https://' + $StorageAccountName + '.blob.core.windows.net/' + $ContainerName

$LocalPath = <source path>

$TenantID = ''
$AppID = ''
$Password = ''

$env:AZCOPY_SPA_CLIENT_SECRET = $Password


.\azcopy login --service-principal --application-id $AppID --tenant-id $TenantID


.\azcopy sync $LocalPath $StorageURL --recursive=true 

From there, I add the script file into a windows scheduled task command to run every 5 min:

schtasks /CREATE /SC minute /MO 5 /TN "AzCopy Script 2" /TR C:\Users\nk\Documents\AzCopy\Windows\azcopyAutomatedTest.ps1

The windows scheduled task gets created but when it runs, it does not actually run the script. I've checked my storage account every-time the task runs and do not see it updated. I've also changed the file to .bat and .exe and have not seen it run as expected.

Upvotes: 0

Views: 789

Answers (2)

nadk
nadk

Reputation: 11

The reason this wasn't running the script was because I did not include the path to the az copy folder in the script.

so I just added the az copy command path right before the:

$env:AZCOPY_SPA_CLIENT_SECRET = $Password

and it worked.

Upvotes: 1

HAL9256
HAL9256

Reputation: 13453

You have to have 3 things in place.

First you have to execute the script with powershell.exe the default application for opening .ps1 files is notepad:

powershell.exe -file "C:\Users\nk\Documents\AzCopy\Windows\azcopyAutomatedTest.ps1"

Second, you have to set the Execution Policy from the default RemoteSigned to either ByPass or Unrestricted:

powershell.exe -ExecutionPolicy ByPass 

e.g.

schtasks /CREATE /SC minute /MO 5 /TN "AzCopy Script 2" /TR 'powershell.exe -ExecutionPolicy ByPass -File "C:\Users\nk\Documents\AzCopy\Windows\azcopyAutomatedTest.ps1"'

Third, when you are running the powershell script as a scheduled job, it starts up in the "C:\Windows\system32" folder (Note: This also happens when you specify the "Start in" directory). So you have to have all your paths be Fully Qualified.

Upvotes: 0

Related Questions