gpt4 book ai didi

azure - 无法将 'System.Object[]' 转换为参数 'System.String' 所需的类型 'Name' 。不支持指定的方法。在 :1 char:35 行

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

我要批量更新 CSV 文件中列出的特定资源的标签。但得到标题中的错误。知道如何转换为字符串吗?

CSV 示例:

<表类=“s-表”><标题>资源名称资源组 <正文>vm10rg01vm11rg02

代码:

$ResourceList = Import-Csv 'C:\Users\43263\Desktop\resources.csv'

$tags = @{"DeptNumber"="123G"; "OwnerEmail"="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c6d9c2d4d9f0ddd1d9dc9ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>"}

$getresource = Get-AzResource -Name $ResourceList.ResourceName -ResourceGroup $ResourceList.ResourceGroup

ForEach ($Resource in $ResourceList.ResourceName){
New-AzTag -ResourceId $getresource.id -Tag $tags
}

最佳答案

您需要循环 CSV 中的每一行并分别查询每个资源。此外,应为 New-AzTag -ResourceId 使用的属性值是 .ResourceId 而不是 .Id

$tags = @{
DeptNumber = '123G'
OwnerEmail = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc8a958e9895bc919d9590d29f9391" rel="noreferrer noopener nofollow">[email protected]</a>'
}

Import-Csv 'C:\Users\43263\Desktop\resources.csv' | ForEach-Object {
try {
$getAzResourceSplat = @{
Name = $_.ResourceName
ResourceGroup = $_.ResourceGroup
ErrorAction = 'Stop'
}

$resource = Get-AzResource @getAzResourceSplat
New-AzTag -ResourceId $resource.ResourceId -Tag $tags
}
catch {
Write-Warning $_
}
}

关于azure - 无法将 'System.Object[]' 转换为参数 'System.String' 所需的类型 'Name' 。不支持指定的方法。在 :1 char:35 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76264532/

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