J. *_*ond 2 variables powershell herestring
我似乎无法弄清楚如何在herestring中使用变量,以及稍后在管道命令中扩展变量.我尝试过单引号'和双"引号,以及转义`字符.
我试图将herestring用于Exchange组的列表(例如数组),以及应用于这些组的相应条件列表.这是一个简化的示例,它无法$Conditions正确使用变量(它不会扩展$_.customattribute2变量):
# List of groups and conditions (tab delimitered)
$records = @"
Group1 {$_.customattribute2 -Like '*Sales*'}
Group2 {$_.customattribute2 -Like '*Marketing*' -OR $_.customattribute2 -Eq 'CEO'}
"@
# Loop through each line in $records and find mailboxes that match $conditions
foreach ($record in $records -split "`n") {
($DGroup,$Conditions) = $record -split "`t"
$MailboxList = Get-Mailbox -ResultSize Unlimited
$MailboxList | where $Conditions
}
Run Code Online (Sandbox Code Playgroud)
不,不,那不会起作用.关于PowerShell的全部好处是不必将所有东西都变成一个字符串,然后将它拖到月球上,然后重新尝试将重要的东西从字符串中取出.{$_.x -eq "y"}是一个scriptblock.这本身就是一件事,你不需要把它放在一个字符串中.
#Array of arrays. Pairs of groups and conditions
[Array]$records = @(
('Group1', {$_.customattribute2 -Like '*Sales*'}),
('Group2', {$_.customattribute2 -Like '*Marketing*' -OR $_.customattribute2 -Eq 'CEO'})
)
#Loop through each line in $records and find mailboxes that match $conditions
foreach ($pair in $records) {
$DGroup, $Condition = $pair
$MailboxList = Get-Mailbox -ResultSize Unlimited
$MailboxList | where $Condition
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
284 次 |
| 最近记录: |