gpt4 book ai didi

azure - 为什么在调用其中包含 azure commandlet 的脚本时未出现 write-host 语句?

转载 作者:行者123 更新时间:2023-12-03 03:35:55 24 4
gpt4 key购买 nike

我有一个非常简单的 2 个脚本,我正在尝试从另一个 powershell 运行脚本调用 powershell 脚本

运行脚本(run.ps1)

.\NewRG.ps1 -rgName "singleVM12" -location "Canada Central" -tags @{dept="Marketing"}

调用的脚本(newRG.ps1)

[CmdletBinding()]
param (
[string]$rgName = "Test1-rg",
[string]$location = "Canada Central",
[Parameter(Mandatory)]
[hashtable]$tags)
$newRG = New-AzResourceGroup -name $rgName -location $location -tags @{dept="marketing"}
write-output "test"

我希望我应该在控制台中进行测试,但我获得了资源组的属性

资源组名称:singleVM12
地点:加拿大中心
配置状态:成功

问题是我有更复杂的脚本,其中包含我想要显示的多个写入主机条目,但当我运行“run.ps1”文件时,这些条目都不会出现,如果我只单独调用被调用的脚本,它就可以正常工作。我尝试使用写入输出,但发生了同样的事情。我注意到 hello world 可以工作,所以我猜测 Azure commandlet 可能会导致此问题。有办法解决这个问题吗?

最佳答案

我正在使用Write-Output来打印提示中的值。我也是按照你的方法做的。

遵循解决方法:

测试输出.ps1

# I am getting resource information 
[CmdletBinding()]
param (
[string]$rgName = "test")


#$newRG = New-AzResourceGroup -name $rgName -location $location -tags @{dept="marketing"}

$getResource = Get-AzResource -ResourceGroupName $rgName
write-output "azure resoure get successfully- " $rgName


$getResource = Get-AzResource -ResourceGroupName $rgName
write-output "test2- " $rgName


$getResource = Get-AzResource -ResourceGroupName $rgName
write-output "test3 - " $rgName


$getResource = Get-AzResource
write-output "test4- " $rgName
# you can use return to send the the required data to promt as well. But you can use end of your script otherwise it will skip after the return statement.
return $getResource.ResourceGroupName

test2.ps1

test2.ps1脚本中调用testout.ps1

# Connect Azure Account using specific Subscription Id if you are using more subscription in single account
Connect-AzAccount -SubscriptionId '<Your subscription Id>'
# calling test.ps1 script
.\testout.ps1 -rgName "<Your Resourcegroup Name>"

结果

enter image description here

关于azure - 为什么在调用其中包含 azure commandlet 的脚本时未出现 write-host 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73338127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com