使用Powershell分配标签并从CSV读取

时间:2017-07-14 13:49:20

标签: powershell azure

我有点棘手。

我有一个csv文件,其中包含以下标题。我试图在csv中为vsname分配相关标题下csv中提到的标签(希望这很有意义)。

vmname,resourcegroup,size,costcenter,displayname等

当我运行前20行时,它会按预期生成azure文本文件。

但是,如果我向前运行第二个第23行以将标签分配给vm,则会添加键值但不添加实际值,并通过gui在门户网站上的标签中显示为空白。

我不确定我做错了什么,并想知道是否有人能看到我正在做的事情,我认为唯一可能是$ vm = $ _。。vmname,但是无法理解。

我的代码在下面

$csv = import-csv "C:\temp\book.csv"
$csv | foreach-object {
  $first =$_.VMName
  $Second =$_.ResourceGroup
  $size=$_.Size
  $t1= $_.Costcenter
  $t2= $_.Displayname
  $t3= $_.Environment
  $t4= $_.Project
  $t5= $_.Role
  $t6= $_.Template
  $t7= $_.DSC
  $t8= $_.Schedule
  $t9= $_.AppID
  $t10= $_.Service
  $t11= $_.REF
  $t12= $_.OS
  $t13= $_.Zone
  "The VM is $first, the RG is $second, the size is $size and tags $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11, $t12, $t13" | out-file C:\temp\Azure.txt -append
  }
##the above works fine

Select-AzureRmSubscription -SubscriptionId "xxxxxx-xxxx-xxxx-xxxx-xxxxxxx"
$csv = import-csv "C:\temp\book.csv"
$vm = "$_.VMName"
$tags = (Get-AzureRmResource -ResourceGroupName "RG01" -ResourceType "Microsoft.Compute/virtualMachines"   -Name "$VM").Tags
$csv | ForEach-Object {
  $first =$_.VMName
  $t1= $_.Costcenter
  $t2= $_.Displayname
  $t3= $_.Environment
  $t4= $_.Project
  $t5= $_.Role
  $t6= $_.Template
  $t7= $_.DSC
  $t8= $_.Schedule
  $t9= $_.AppID
  $t10= $_.Service
  $t11= $_.REF
  $t12= $_.OS
  $t13= $_.Zone

$tags += @{
Costcenter="$t1"
Displayname="$t2"
Environment="$t3"
Project="$t4"
Role="$t5"
Template="$t6"
DSC="$t7"
Schedule="$t8"
AppID="$t9"
Service="$t10"
DREF="$t11"
OS="$t12"
Zone="$t13"
    }
}

Set-AzureRmResource  -ResourceGroupName "RG01" -Name "$VM" -Tag $tags -ResourceType "Microsoft.Compute/virtualMachines" -verbose

提前致谢:)

1 个答案:

答案 0 :(得分:1)

看起来你的任务过于复杂了。

如果我理解正确,你想要做的是(伪代码):

foreach $VM in $CSVfile
    Retrieve existing tags for $VM
    Add csv values to existing tags
    Set new tag set on $VM in azure

这应该非常简单:

Select-AzureRmSubscription -SubscriptionId "xxxxxx-xxxx-xxxx-xxxx-xxxxxxx"
$csv = import-csv "C:\temp\book.csv"

$csv | ForEach-Object {
    # Retrieve existing tags
    $tags = (Get-AzureRmResource -ResourceGroupName "RG01" -ResourceType "Microsoft.Compute/virtualMachines" -Name $_.VMName).Tags
    # Add new value pairs from CSV
    $tags += @{
        Costcenter  = $_.Costcenter
        Displayname = $_.Displayname
        Environment = $_.Environment
        Project     = $_.Project
        Role        = $_.Project
        Template    = $_.Template
        DSC         = $_.DSC
        Schedule    = $_.Schedule
        AppID       = $_.AppID
        Service     = $_.Service
        DREF        = $_.REF
        OS          = $_.OS
        Zone        = $_.Zone
    }

    # Update resource with new tag set
    Set-AzureRmResource  -ResourceGroupName "RG01" -Name $_.VMName -Tag $tags -ResourceType "Microsoft.Compute/virtualMachines" -verbose
}

如果您的CSV文件中的任何标记名称已作为资源上的标记存在,请确保手动添加它们,一次添加一个:

Select-AzureRmSubscription -SubscriptionId "xxxxxx-xxxx-xxxx-xxxx-xxxxxxx"
$csv = import-csv "C:\temp\book.csv"

$csv | ForEach-Object {
    # Retrieve existing tags
    $tags = (Get-AzureRmResource -ResourceGroupName "RG01" -ResourceType "Microsoft.Compute/virtualMachines" -Name $_.VMName).Tags

    # Define new value pairs from CSV
    $newTags = @{
        Costcenter  = $_.Costcenter
        Displayname = $_.Displayname
        Environment = $_.Environment
        Project     = $_.Project
        Role        = $_.Project
        Template    = $_.Template
        DSC         = $_.DSC
        Schedule    = $_.Schedule
        AppID       = $_.AppID
        Service     = $_.Service
        DREF        = $_.REF
        OS          = $_.OS
        Zone        = $_.Zone
    }

    # Add new tags to existing set (overwrite conflicting tag names)
    foreach($tagName in $newTags.Keys){
        $tags[$_] = $newTags[$_]
    }

    # Update resource with new tag set
    Set-AzureRmResource  -ResourceGroupName "RG01" -Name $_.VMName -Tag $tags -ResourceType "Microsoft.Compute/virtualMachines" -verbose
}