For a list:
https://github.com/Microsoft/azure-pipelines-tasks/tree/master/Tasks
If you have an issue or request for the Azure Pipelines service, use developer community instead:
https://developercommunity.visualstudio.com/spaces/21/index.html )
Entering this information will route you directly to the right team and expedite traction.
Question, Bug, or Feature?
Type: BUG
Enter Task Name: AzurePowershell@4 (4.159.2)
list here (V# not needed):
https://github.com/Microsoft/azure-pipelines-tasks/tree/master/Tasks
Azure Pipelines
Agent - Private:
Server 2019 with latest VS 2019 installed
We depend on the AzurePowershell VSTS task to use resources in our Azure subs. With the latest deployment of the task 4.159.2 (today) we can no longer get the context to change to the appropriate subscription in the tenant, hence our builds, deployments, etc are blocked. The previous version of the task 4.157.4 was working just fine.
Azure PowerShell version on hosted VM: 2.3.2
We need someone to look at what the heck happened with the latest version ....

[Enable debug logging and please provide the zip file containing all the logs for a speedy resolution]
Here is an example of diagnostics enabled with a call to Get-AzContext in an Azure PowerShell task we have
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
Generating script.
307
308
========================== Starting Command Output ===========================
309
310
311
312
313
314
VERBOSE: C:\Modules\azurerm_2.1.0 is not present in
315
C:\Modules\az_2.3.2;C:\Users\tfsbuild\Documents\WindowsPowerShell\Modules;C:\Program
316
Files\WindowsPowerShell\Modules;C:\windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files\Microsoft
317
Monitoring Agent\Agent\PowerShell\;C:\Program Files\Microsoft Monitoring Agent\Agent\AzureAutomation\7.3.396.0
318
VERBOSE: C:\Modules\azure_2.1.0 is not present in
319
C:\Modules\az_2.3.2;C:\Users\tfsbuild\Documents\WindowsPowerShell\Modules;C:\Program
320
Files\WindowsPowerShell\Modules;C:\windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files\Microsoft
321
Monitoring Agent\Agent\PowerShell\;C:\Program Files\Microsoft Monitoring Agent\Agent\AzureAutomation\7.3.396.0
322
VERBOSE: The value of the module path is: C:\Modules\az_2.3.2
323
VERBOSE: The updated value of the PSModulePath is:
324
C:\Modules\az_2.3.2;C:\Modules\az_2.3.2;C:\Users\tfsbuild\Documents\WindowsPowerShell\Modules;C:\Program
325
Files\WindowsPowerShell\Modules;C:\windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files\Microsoft
326
Monitoring Agent\Agent\PowerShell\;C:\Program Files\Microsoft Monitoring Agent\Agent\AzureAutomation\7.3.396.0
327
VERBOSE: Loading module from path 'D:\agent\a1_work\15\s\infra\deployment\ClusterManager\clustermanager.psm1'.
328
VERBOSE: Exporting function 'Test-CMCluster'.
329
VERBOSE: Exporting function 'Get-CMClusters'.
330
VERBOSE: Exporting function 'Get-CMClusterIds'.
331
VERBOSE: Exporting function 'Get-CMCluster'.
332
VERBOSE: Exporting function 'Set-CMCluster'.
333
VERBOSE: Exporting function 'Remove-CMCluster'.
334
VERBOSE: Exporting function 'New-CMCluster'.
335
VERBOSE: Exporting function 'Lock-CMCluster'.
336
VERBOSE: Exporting function 'Unlock-CMCluster'.
337
VERBOSE: Exporting function 'Get-CMIsLocked'.
338
VERBOSE: Exporting function 'Set-CMEnvironmentFromCluster'.
339
VERBOSE: Exporting function 'Invoke-CMRetryingCommand'.
340
VERBOSE: Exporting function 'Invoke-CMLog'.
341
VERBOSE: Importing function 'Get-CMCluster'.
342
VERBOSE: Importing function 'Get-CMClusterIds'.
343
VERBOSE: Importing function 'Get-CMClusters'.
344
VERBOSE: Importing function 'Get-CMIsLocked'.
345
VERBOSE: Importing function 'Invoke-CMLog'.
346
VERBOSE: Importing function 'Invoke-CMRetryingCommand'.
347
VERBOSE: Importing function 'Lock-CMCluster'.
348
VERBOSE: Importing function 'New-CMCluster'.
349
VERBOSE: Importing function 'Remove-CMCluster'.
350
VERBOSE: Importing function 'Set-CMCluster'.
351
VERBOSE: Importing function 'Set-CMEnvironmentFromCluster'.
352
VERBOSE: Importing function 'Test-CMCluster'.
353
VERBOSE: Importing function 'Unlock-CMCluster'.
354
VERBOSE: ------------------------------------------------------------
355
VERBOSE: The script was invoked with the following parameters:
356
VERBOSE: -SubscriptionId: zzz
357
VERBOSE: -StorageAccountName: zzz
358
VERBOSE: -Automation
359
VERBOSE: -Enabled
360
VERBOSE: -SetJobScopedEnvironment
361
VERBOSE: ------------------------------------------------------------
362
VERBOSE: Loading module from path 'C:\Program
363
Files\WindowsPowerShell\Modules\Az.Accounts\1.6.2\Microsoft.Azure.PowerShell.Cmdlets.Accounts.dll'.
364
VERBOSE: Importing cmdlet 'Get-AzProfile'.
365
VERBOSE: Importing cmdlet 'Register-AzModule'.
366
VERBOSE: Importing cmdlet 'Select-AzProfile'.
367
VERBOSE: Importing cmdlet 'Connect-AzAccount'.
368
VERBOSE: Importing cmdlet 'Disconnect-AzAccount'.
369
VERBOSE: Importing cmdlet 'Get-AzContext'.
370
VERBOSE: Importing cmdlet 'Import-AzContext'.
371
VERBOSE: Importing cmdlet 'Save-AzContext'.
372
VERBOSE: Importing cmdlet 'Set-AzContext'.
373
VERBOSE: Importing cmdlet 'Disable-AzDataCollection'.
374
VERBOSE: Importing cmdlet 'Enable-AzDataCollection'.
375
VERBOSE: Importing cmdlet 'Add-AzEnvironment'.
376
VERBOSE: Importing cmdlet 'Get-AzEnvironment'.
377
VERBOSE: Importing cmdlet 'Remove-AzEnvironment'.
378
VERBOSE: Importing cmdlet 'Set-AzEnvironment'.
379
VERBOSE: Importing cmdlet 'Send-Feedback'.
380
VERBOSE: Importing cmdlet 'Get-AzSubscription'.
381
VERBOSE: Importing cmdlet 'Get-AzTenant'.
382
VERBOSE: Importing cmdlet 'Uninstall-AzureRm'.
383
VERBOSE: Importing cmdlet 'Resolve-AzError'.
384
VERBOSE: Importing cmdlet 'Clear-AzDefault'.
385
VERBOSE: Importing cmdlet 'Get-AzDefault'.
386
VERBOSE: Importing cmdlet 'Set-AzDefault'.
387
VERBOSE: Importing cmdlet 'Disable-AzureRmAlias'.
388
VERBOSE: Importing cmdlet 'Enable-AzureRmAlias'.
389
VERBOSE: Importing cmdlet 'Disable-AzContextAutosave'.
390
VERBOSE: Importing cmdlet 'Enable-AzContextAutosave'.
391
VERBOSE: Importing cmdlet 'Get-AzContextAutosaveSetting'.
392
VERBOSE: Importing cmdlet 'Clear-AzContext'.
393
VERBOSE: Importing cmdlet 'Remove-AzContext'.
394
VERBOSE: Importing cmdlet 'Rename-AzContext'.
395
VERBOSE: Importing cmdlet 'Select-AzContext'.
396
VERBOSE: Importing alias 'Login-AzAccount'.
397
VERBOSE: Importing alias 'Login-AzureRmAccount'.
398
VERBOSE: Importing alias 'Add-AzAccount'.
399
VERBOSE: Importing alias 'Logout-AzAccount'.
400
VERBOSE: Importing alias 'Logout-AzureRmAccount'.
401
VERBOSE: Importing alias 'Remove-AzAccount'.
402
VERBOSE: Importing alias 'Save-AzProfile'.
403
VERBOSE: Importing alias 'Select-AzSubscription'.
404
VERBOSE: Importing alias 'Get-AzureRmDomain'.
405
VERBOSE: Importing alias 'Get-AzDomain'.
406
VERBOSE: Importing alias 'Resolve-Error'.
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
At D:\agent\a1_work\15\s\infra\deployment\ClusterManager\scripts\Set-Lease.ps1:163 char:5
423
425
427
429
431
432
433
434
435
436
437
438
439
440
441
442
443
444
Checkout how to troubleshoot failures and collect debug logs: https://docs.microsoft.com/en-us/vsts/build-release/actions/troubleshooting
This is also blocking us severely. In our Azure DevOps Release pipeline, we get the following error when using Azure Powershell 4.*:
2019-10-23T03:29:36.4566280Z ##[error]New-AzApplicationInsights : No account found in the context. Please login using Connect-AzAccount.
One thing I am trying as a work around to get builds, etc going (while this is investigated) is to copy the previous version Azure PowerShell task as the latest version under the agent tasks. I am only able to do this since it is our private pool and I have access to the VMs.
The simple repro is to have an azure powershell task that runs a script that just calls Get-AzContext and verifies that the return value is non-null. Unfortunately, it may not be reproducible on all agent VMs …
Any updated on this?
This is also blocking us severely as multiple of our release tasks depend on the Az module, which means that depend on version 4 of the Azure powershell task. Currently ALL of our deployments are failing with the issue:
Any time-frame on a fix?
I'm also running into this issue. Simple reproduction as suggested by @jpwarner67 above:
$context = Get-AzContext
if ($context -eq $null) {
Write-Error "Context is null"
} else {
$context
}
NOTE: I can only reproduce on our self-hosted agent. The hosted agent (vs2017-win2016) works.
Agent version on self-hosted: 2.140.0
Agent version on hosted agent: 2.158.0
So perhaps updating the agent version will solve the problem (I don't have access to that in the affected organization, so I can't try it myself).
Updating the self-hosted agent to 2.158.0 did not make a difference.
Hi, I see the script is trying to run 'Connect-AzAccount' command again. Azure PowerShell task already do so with the given service connection details.
Any reason why we are trying connect again? Are you trying to access a different subscription other than which is specified in the service connection?
I am not able to reproduce the issue in my private agent. For me "Get-AzContext" is working fine. I have installed Az module with the below command (as per Azure PS documentation)
Install-Module -Name Az -Repository PSGallery -Force
can you please check if the script you are running is working from local powershell on the agent machine?
Its failing for us too. I used following simple inline script with a private agent.
$context = Get-AzContext
if ($context -eq $null) {
Write-Error "Context is null"
} else {
$context
}
Here is the output
2019-10-23T14:59:22.8415737Z ##[command]Import-Module -Name C:\Program Files\WindowsPowerShell\Modules\Az.Accounts\1.6.2\Az.Accounts.psd1 -Global
2019-10-23T14:59:24.3340785Z ##[command]Clear-AzContext -Scope Process
2019-10-23T14:59:25.0863822Z ##[command]Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
2019-10-23T14:59:25.6228951Z ##[command]Connect-AzAccount -ServicePrincipal -Tenant *** -Credential System.Management.Automation.PSCredential -Environment AzureCloud
2019-10-23T14:59:27.4014314Z ##[command] Set-AzContext -SubscriptionId 68****-******* -TenantId ***
2019-10-23T14:59:27.9174184Z Generating script.
2019-10-23T14:59:27.9911682Z ========================== Starting Command Output ===========================
2019-10-23T14:59:28.0086713Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\agent\_work\_temp\62a44dda-f7a5-4135-aa4f-49c3902834dc.ps1'"
2019-10-23T14:59:31.6693268Z ##[error]C:\agent\_work\_temp\62a44dda-f7a5-4135-aa4f-49c3902834dc.ps1 : Context is null
Not sure what you mean by running Connect-AzAccount again. Our script is not doing that. It merely checks the context to change to a different sub if necessary. The context is $null in which case we are indicating that an interactive login needs to happen. Other folks call APIs in there scripts which do similar things and are expecting a context.
Again, see the canonical (simple) repro above and try different agents if necessary. It won't repro on every type of agent --- you may need to try several types of agents to see a repro. I haven't been able to narrow down agent differences where it does and does not repro.
Lastly, briefly looking at recent changes leading up to the latest Azure PowerShell task version, it shows that there were significant changes merged related to PowerShell core support(?). Not clear if that is related. In any case, this issue will be a significant problem if this is not addressed soon.
@niadak : I'm not sure what you mean by "I see the script is trying to run 'Connect-AzAccount' command again"
The repro script only calls Get-AzContext. It is the Azure PowerShell task which calls Connect-AzAccount and it seems to do it only once.
I have now also tried this in a different Azure DevOps organization. The agents there all run the Azure PowerShell task version 4.157.4, where it works fine. (I don't know why they aren't updating to 4.159.2).
@niadak: In your repro attempt on your self-hosted agent, did it use 4.159.2?
The exact same release definition with only a single task does not work in the original organization where the agent picks version 4.159.2.
In the original organization, we had a successful release of a service yesterday - but according to the logs from that it used version 4.157.4.
We've been troubleshooting this problem for several hours now.
We've in a very similar situation where up until today our deployment pipeline worked fine with version 4.157.4, from today we started experiencing issues connecting to Azure KeyVault where although it correctly connects (using Connect-AzAccount and then sets the correct subscription) it then fails to retrieve a secret from KeyVault, failing with an error:
2019-10-23T15:31:17.5247104Z ##[error]Get-AzKeyVaultSecret : Run Connect-AzAccount to login.
We can see from the logs that the new version of the task is: 4.159.2
Oddly, a temporary Azure Key Vault task also fails but with a "tunneling socket could not be established, statusCode=504" error. Not sure if this helps or is a red-herring.
This pipeline is running on a private hosted agent.
confirming we are seeing this problem on all of our jobs using Azure powershell tasks.
They all appear to fail due to the task failing to login.
We are mainly on vs2017-win2016 hosted agents.
problem started overnight with version 4.159.2 of the task.
yesterday all builds were successful running version 4.157.4 of the task.
Please fix ASAP
I can also confirm that I am seeing this issue. One thing I noticed is that if I run against a hosted pool on our instance it works, but against a custom agent it's having issues, so it seems to be specific to the environment, though that may just be because they have different versions.
This appears to be the PR that created the .2 version of the task, and being that it deals with tokens, I believe it is likely related.
@ds-ms, @utk-ch ##
To work around this on a private agent, you can go to each agent directory _work_tasks\AzurePowerShell and rename 4.159.2 to 4.159.2_temp and then copy 4.157.4 and name it 4.159.2
This essentially forces a rollback to the previous version of the task before this bug was introduced.
I am facing the same issue with hosted agent. I have different scenario - release pipelines with version 3 (AzureRM module) and version 4 (Az module) tasks. The task AzurePowershell@4 works fine when it is alone in the release pipeline. It throws the same error "Run Connect-AzAccount to login." in th pipelines where task AzurePowershell@3 executed before. Same behaviour on WS2016-VS2017 and WS2019-VS2019 agents. "Use PowerShell Core" for @4 task doesn't help.
BTW, it use to work just fine as this week week Monday, when task version was 4.157.4.
Reproduction steps.
(Get-AzResourceProvider -ProviderNamespace Microsoft.Web | Select-Object -ExpandProperty ResourceTypes | Where-Object ResourceTypeName -eq 'sites' | Select-Object -ExpandProperty ApiVersions)[0]
Task : Azure PowerShell
Description : Run a PowerShell script within an Azure environment
Version : 4.159.2
Author : Microsoft Corporation
Generating script.
========================== Starting Command Output ===========================
Name Account SubscriptionName Environment TenantId
SUB_Name (sub_guid... SUB_Name AzureCloud tenat_id...
2018-11-01
Get-AzureRMResourceGroupTask : Azure PowerShell
Description : Run a PowerShell script within an Azure environment
Version : 4.159.2
Author : Microsoft Corporation
Generating script.
========================== Starting Command Output ===========================
##[error]Get-AzResourceProvider : Run Connect-AzAccount to login.
ee6-ae2d-35245a263c51.ps1:4 char:2
oviderNamespace Microsoft.Web | Select-Obj ...
~~~~~~~~~
CloseError: (:) [Get-AzResourceProvider], PSInvalidOperationException
Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureProviderCmdlet
We're experiencing the same error. Our tasks with 4.1.157.4 work fine, but when they were updated to 4.1.159.0, they stopped working.
Looking at it. Will update shortly
To work around this on a private agent, you can go to each agent directory _work_tasks\AzurePowerShell and rename 4.159.2 to 4.159.2_temp and then copy 4.157.4 and name it 4.159.2
This essentially forces a rollback to the previous version of the task before this bug was introduced.
Won't the agents automatically update the task when you go to run again?
@KyleZielinski It will not update the task if the folder 4.159.2 is already present.
Just to update here, we are preparing a fix and will rollout the fix. Will update once the fix is available.
@niadak Thanks! It was the news we all have been waiting!
This appears to be the PR that created the .2 version of the task, and being that it deals with tokens, > I believe it is likely related.
@ds-ms, @utk-ch ##11378
@KyleZielinski The fix in that PR could have impacted the flow if your self-hosted agent is Linux based, but from the logs it looks like yours is a windows based agent.
Hi All, We have rolled out V4.159.3 with a fix for the issue. Please try now and let us know if you are still facing the issue.
4.159.3 works on our agents. Thank you for the quick fix.
Also works on our hosted agents. Thanks for the fix!
Seems to be working in my scenario. Thank you for quick fix!
any relationship why i'm getting different error now?
2019-10-24T12:45:12.3720247Z ##[command]Clear-AzContext -Scope Process -ErrorAction Stop
2019-10-24T12:45:12.7980448Z ##[debug]Caught exception from task script.
2019-10-24T12:45:12.8011476Z ##[debug]Error record:
2019-10-24T12:45:12.8725302Z ##[debug]Set-AzResourceGroup : Operation returned an invalid status code 'Forbidden'
2019-10-24T12:45:12.8735795Z ##[debug]At D:\a\_temp\60db5fb7-37dd-4fd5-9707-583306628241.ps1:1 char:1
2019-10-24T12:45:12.8747306Z ##[debug]+ Set-AzResourceGroup -Name 123 -Tag @{Department="IT"}
2019-10-24T12:45:12.8759767Z ##[debug]+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2019-10-24T12:45:12.8773460Z ##[debug] + CategoryInfo : CloseError: (:) [Set-AzResourceGroup], CloudException
2019-10-24T12:45:12.8785736Z ##[debug] + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceGroupCmd let
2019-10-24T12:45:12.8799485Z ##[debug]
Found the issue myself. The error message was not clear and it should be resource group doesnt exist.
Closing this issue as this is fixed.
@majorvin , Not sure if this is related. Seems like the script is failing for some reason. Feel free to create a new issue for it if required..
Thanks! I have verified the fix in our VM pools.
Quick question, is/will the repro be added to tests for future releases?
Thanks
Thank you, 4.159.3 fix worked for us.
Works now! Thanks.
Ack, fixed.
We're still getting this error when we select the "Powershell Core" option in the Azure PowerShell task. We're using the VS 2019 build agents.
I'm still seeing the issue as well when I select the "Powershell Core" option but in my case it's on a private agent
Can someone tell me how to apply the fix V4.159.3?
We recently began using TFS 2019.
We have a windows agent where we are running Azure PowerShell script.
We are using version 4.0 preview version of powershell task
It fails with error: "Object reference not set to an instance of object"
@NickGraham101 I am also seeing this issue in V4.159.3.
@brushwood24 The issue is still there in V4.159.3 version when "Powershell Core" option is enabled and we are fixing the issue in V5 task and the task will be available in next update.
There is no easy workaround for V4.159.3 version if you want to use PowerShell-Core option.
@rajcheval , Which version of the task you are using? You can upload the new version of the task to the TFS 2019 instance using tfx command line tool.
@niadak when to expect V5 on hosted agents? I want to move my client to PS Core and this issue is a blocker for us.
AzurePowerShellV5 will be deployed in next sprint deployment. It will take ~3 weeks to reach all the accounts.
4.159.7 is causing the same issue, how can we revert back to 4.159.3 on hosted agents?
I am having same issue with task: AzurePowerShell@5
When I list Get-ChildItem '/usr/share' what I see is below;
d----- 12/10/19 12:34 AM az_1.0.0
d----- 12/10/19 12:34 AM az_1.6.0
d----- 12/10/19 12:34 AM az_2.3.2
d----- 12/10/19 12:35 AM az_2.6.0
I guess there should be higher versions?
Most helpful comment
Just to update here, we are preparing a fix and will rollout the fix. Will update once the fix is available.