简体   繁体   English

将$ myVar设置为带通配符的变量

[英]Set $myVar as a variable with wildcard

I have the below script which I had help with from here, one quick question how do you I set $MyVar as variable, and a wildcard so partial name can be selected. 我有下面的脚本,可以从这里获得帮助,一个快速的问题,如何将$MyVar设置为变量,以及一个通配符,以便可以选择部分名称。

Select-AzureRmSubscription -SubscriptionName "MySubscription"
Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like '$MyVar'} | ForEach-Object {
    $VMs = Get-AzureRmVM -ResourceGroupName $_.ResourceGroupName
    ForEach ($vm in $vms) {
        $tags2 = $VM.tags
        $tags2 += @{ShutdownSchedule_AllowStart="True"; ShutdownSchedule_AllowStop="True" }

        Set-AzureRmResource -ResourceName $vm.Name -ResourceGroupName $vm.ResourceGroupName -ResourceType "Microsoft.Compute/virtualMachines" -Tag $tags2 -Force -Verbose
    }
}

Not sure how to set it as a variable. 不知道如何将其设置为变量。 Was thinking maybe {$_.ResourceGroupName -like $('$MyVar')} 正在考虑{$_.ResourceGroupName -like $('$MyVar')}

Or do I need to set it as a parameter, as in a workflow, but not sure how to do that outside a workflow.. 还是我需要像在工作流程中一样将其设置为参数,但不确定如何在工作流程之外进行设置。

使用双引号使变量可以扩展(请参阅about_quoting_rules ),然后在要通配的任一端(或两端)加*

{$_.ResourceGroupName -like "*$MyVar*"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM