powershell for for for got-object与AZ CLI轮廓隔离平行

发布于 2025-01-22 11:43:06 字数 974 浏览 5 评论 0原文

我有一个脚本需要并行调用子脚本。使用AZ CLI的子脚本,并在不同的Azure订阅中创建/修改Azure Paas对象。
问题在于,由于不同的脚本正在使用az帐户集-subscription< subscription-for-script>,它们重叠,并且需要在script a中创建的东西,在订阅B中创建的B导致片刻B将订阅设置为订阅B。 作为AZ CLI在AzureProfile.json中存储上下文,我尝试每个脚本创建新文件夹,并通过$ env:azure_config_dir指定每个脚本的不同值。但是我找不到在不使用环境变量的情况下指定儿童脚本中环境变量的方法,或指定AzureProfile上下文。 在父脚本中:

$listOfScripts | Foreach-Object -Parallel {
  <block to run script with arguments>
} -AsJob -ThrottleLimit 50

在每个子脚本中:

$Env:AZURE_CONFIG_DIR = "$RootPath\..\AzureProfiles\folderForScript"
az login --service-principal -u ${env:ARM_CLIENT_ID} -p ${env:ARM_CLIENT_SECRET} --tenant ${env:ARM_TENANT_ID}
az account set --subscription $subscription_id

值得赞赏的建议如何实现并行独立脚本,这些脚本使用不同的订阅来修改Azure Paas对象

更新:仅找到的解决方案 - 不使用<<代码> AZ登录和AZ帐户集在并行运行的脚本中。只需在父脚本中通过SPN连接,然后在每个AZ命令中使用参数- subscription

I have a script that need to call child scripts in parallel. Child scripts using az cli and create/modify Azure PaaS objects in different Azure subscriptions.
The problem is that as different scripts are using az account set --subscription <subscription-for-script>, they overlap and something that need to be created in subscription A by script A, created in subscription B cause a moment before script B set subscription to subscription B.
As az cli stores context in AzureProfile.json, I tried to create new folder per script and via $Env:AZURE_CONFIG_DIR specify different values per script. But I cannot find a way to isolate environment variables in child scripts, or specify AzureProfile context without using environment variables.
In parent script:

$listOfScripts | Foreach-Object -Parallel {
  <block to run script with arguments>
} -AsJob -ThrottleLimit 50

and in each child script:

$Env:AZURE_CONFIG_DIR = "$RootPath\..\AzureProfiles\folderForScript"
az login --service-principal -u ${env:ARM_CLIENT_ID} -p ${env:ARM_CLIENT_SECRET} --tenant ${env:ARM_TENANT_ID}
az account set --subscription $subscription_id

Would be appreciate for advice how could be achieved running parallel independent scripts that uses different subscriptions to modify Azure PaaS objects

Update: Only solution that found - not to use az login and az account set inside scripts that run in parallel. Just connect via SPN in parent script and use parameter --subscription in each az command.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2025-01-29 11:43:06

中运行脚本块

在PowerShell中的A Runspace 上下文中,在并行脚本块 。它包含所有定义的变量,函数&amp;加载模块。初始化 runspace 脚本运行时间和资源。当脚本并行运行时,它必须在其自己的Runspace 中运行。每个 runspace 必须 load 需要任何模块 ,并具有任何变量可显式传递来自调用脚本。

唯一自动出现在并行脚本块中的变量是对象中的管道。其他变量在使用$中传递:关键字。

示例

$computers =  'computerA','computerB','computerC','computerD' 
$logsToGet =  'LogA','LogB','LogC'  
# Read specified logs on each machine, using custom module 
$logs = $computers |  ForEach-Object  -ThrottleLimit  10  -Parallel  {  
Import-Module  MyLogsModule  
Get-Logs  -ComputerName $_ -LogName $using:logsToGet 
}

参考

Run script block in parallel

Script blocks run in a runspace context in a PowerShell. It contains all the defined variables, functions & loaded modules. Initializing a runspace for script to run in takes time and resources. It must be run within their own runspace when scripts are running in parallel. Each runspace must load whatever module is needed and have any variable be explicitly passed in from the calling script.

The only variable that automatically appears in the parallel script block is the piped in object. Other variables are passed in using the $using: keyword.

Example

$computers =  'computerA','computerB','computerC','computerD' 
$logsToGet =  'LogA','LogB','LogC'  
# Read specified logs on each machine, using custom module 
$logs = $computers |  ForEach-Object  -ThrottleLimit  10  -Parallel  {  
Import-Module  MyLogsModule  
Get-Logs  -ComputerName $_ -LogName $using:logsToGet 
}

References

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文