{"id":4614,"date":"2017-08-04T07:45:33","date_gmt":"2017-08-04T06:45:33","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=2532"},"modified":"2017-08-04T07:45:33","modified_gmt":"2017-08-04T06:45:33","slug":"the-process-cannot-access-the-file-cwindowssystem32configsystemprofileappdataroamingwindows-azure-powershelltokencache-dat-because-it-is-being-used-by-another-process-4","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2017\/08\/the-process-cannot-access-the-file-cwindowssystem32configsystemprofileappdataroamingwindows-azure-powershelltokencache-dat-because-it-is-being-used-by-another-process-4\/","title":{"rendered":"The process cannot access the file &#8216;C:\\Windows\\system32\\config\\systemprofile\\AppData\\Roaming\\Windows Azure Powershell\\TokenCache.dat&#8217; because it is being used by another process."},"content":{"rendered":"<p>While building some multithreading Azure Runbooks that log into multiple subscriptions simultaneously, I noticed that these multiple concurrent runs often end up on the same Azure Automation Host.<\/p>\n<p>Apparently, these runbooks then don&#8217;t run in full isolation, and the following error may occur:<\/p>\n<blockquote><p>The running command stopped because the preference variable &#8220;ErrorActionPreference&#8221; or common parameter is set to Stop: The process cannot access the file &#8216;C:\\Windows\\system32\\config\\systemprofile\\AppData\\Roaming\\Windows Azure Powershell\\TokenCache.dat&#8217; because it is being used by another process.<\/p><\/blockquote>\n<p>AzureProfile.json may also get locked. I resolved this by doing a retry on the Save-AzureRMContext, and using a randomized file name for the azure json profile:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n\n$randomProfileName = &#x5B;System.IO.Path]::GetRandomFileName()\n\nSave-AzureRmContext -Path .\\$randomProfileName -Force -Confirm:$False\n\n<\/pre>\n<p>And for my full Azure Login code snippet:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n$randomProfileName = &#x5B;System.IO.Path]::GetRandomFileName()\n $tries=0\n    while($true){\n        $tries++\n        try{\n            Write-Output &quot;Logging in to Azure $azureSubscription&quot;\n            $res = Login-AzureRmAccount -Credential $azureCreds -SubscriptionId $azureSubscription -TenantId $tenantId -ErrorAction Stop\n            Select-AzureRmSubscription -SubscriptionId $azureSubscription -ErrorAction Stop -TenantId $tenantId\n            if($res.Context.Subscription.Id -eq $azureSubscription){\n                Write-Output &quot;Logged in to Azure subscription $($res.Context.Subscription.Id)&quot;\n            }else{\n                Throw &quot;Failed, we were logged in to $($res.Context.Subscription.Id) while trying to log in to $azureSubscription&quot;\n            }\n            Save-AzureRmContext -Path .\\$randomProfileName -Force -Confirm:$False\n            break\n        }catch{\n            if($tries -ge 30){\n                Throw\n                Exit\n            }\n            #sleep on failed attempts, as the azure token cache gets locked by concurrent jobs\n            sleep -s (Get-Random -minimum 1 -maximum 6)\n        }\n    }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>While building some multithreading Azure Runbooks that log into multiple subscriptions simultaneously, I noticed that these multiple concurrent runs often end up on the same Azure Automation Host. Apparently, these runbooks then don&#8217;t run in full isolation, and the following error may occur: The running command stopped because the preference variable &#8220;ErrorActionPreference&#8221; or common parameter &hellip; <a href=\"https:\/\/lieben.nu\/liebensraum\/2017\/08\/the-process-cannot-access-the-file-cwindowssystem32configsystemprofileappdataroamingwindows-azure-powershelltokencache-dat-because-it-is-being-used-by-another-process-4\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The process cannot access the file &#8216;C:\\Windows\\system32\\config\\systemprofile\\AppData\\Roaming\\Windows Azure Powershell\\TokenCache.dat&#8217; because it is being used by another process.<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[5,39],"tags":[],"class_list":["post-4614","post","type-post","status-publish","format-standard","hentry","category-azure","category-powershell"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/4614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=4614"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/4614\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=4614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=4614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=4614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}