Azure Powershell script fails when run through task scheduler -


i have powershell script wrote backup local sqlserver azure blob. based on 1 took msdn, added feature delete old backups on 30 days old. when run user, works fine. when added task scheduler, set run me, , manually ask run, works fine. (all output captured in log file, can see working). when run task scheduler @ night when i'm not logged in (the task scheduler set run script me) fails. specifically, claims azure subscription name not know when call set-azuresubscription. then, fails when trying delete blob with:

get-azurestorageblob : can not find azure storage credential. please set current storage account using "set-azuresubscription" or set "azure_storage_connection_string" environment variable. 

the script in question:

import-module sqlps import-module azure  $storageaccount = "storageaccount" $subscriptionname = "subname" $blobcontainer = "backup" $backupurlcontainer = "https://$storageaccount.blob.core.windows.net/$blobcontainer/" $credentialname = "creds"  set-azuresubscription -currentstorageaccountname $storageaccount -subscriptionname $subscriptionname  $path = "sqlserver:\sql\servername\sqlexpress\databases" $alldatabases = get-childitem -force -path $path | where-object {$_.name -eq "db0" -or $_.name -eq "db1"}   foreach ($db in $alldatabases) {     backup-sqldatabase -backupcontainer $backupurlcontainer -sqlcredential $credentialname $db }   $oldblobs = get-azurestorageblob -container backup | where-object { $_.name.contains("db") -and (-((($_.lastmodified) - $([datetime]::now)).totaldays)) -gt $(new-timespan -days 30).totaldays } foreach($blob in $oldblobs) {     write-output $blob.name     remove-azurestorageblob -container "backup" -blob $blob.name } 

the backup part of script works, not blob deletion parts. appear being done environment when log in allows azure powershell scripts work isn't being done when run command @ night when i'm not logged in.

any 1 have idea might be?

task scheduler set run command

powershell -command "c:\scripts\backupdatabases.ps1" 2>&1 >> "c:\logs\backup.log" 

the azure powershell environment needs understand azure subscription work default. did own environment, task scheduler running in different environment.

you need add additional command beginning of script set azure subscription. this:

set-azuresubscription -subscriptionname

the documentation command here. can set subscriptionid etc. instead of subscriptionname.

in addition, this article walks through how connect azure subscription powershell environment.

update: messed around , got working. try adding "select-azuresubscription" before set-azuresubscription command.

select-azuresubscription $subscriptionname set-azuresubscription -subscriptionname $subscriptionname -currentstorageaccountname $storageaccount  

the documentation select-azuresubscription here. if aren't relying on storage account being set, may able remove set-azuresubscription command.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -