Sou*_*kkr 1 powershell foreach formattable
不确定我在这里做错了什么,我知道这与它位于 ForEach 循环内这一事实有关。我尝试移动下面的代码。即使代码各半(一半在循环外,一半在循环外似乎不起作用)。对于 PowerShell 来说还是个新手,我知道每次迭代我都需要将变量添加到 $table 中,或者将它们存储在某个位置并稍后读取它们。
foreach ($gp in $GPINFO) {
# Code that gets the values for $gp, $aclBefore and $aclAfter is here
$table = new-object psobject -Property @{
GPO_Name = $gp.DisplayName
Old_Owner = $aclBefore
New_Owner = $aclAfter
}
$table | Format-Table GPO_Name,Old_Owner,New_Owner
}
Run Code Online (Sandbox Code Playgroud)
如果你能帮助我找出我做错了什么,那就太好了,我知道每次 ForEach 从 $GPINFO 中获取 $gp 时,它都会运行 $table 的东西,这就是我的问题。因此,我最终得到的是多个表,每个表中包含一组数据,而不是一个连续的表。
提前致谢
你只是不能从 foreach () 进行管道传输。这是该语言中经常出现的一个奇怪的部分。其他方法:
Foreach 对象:
$GPINFO | foreach-object {
$gp = $_
new-object psobject -Property @{
GPO_Name = $gp.DisplayName
Old_Owner = $aclBefore
New_Owner = $aclAfter
}
} | Format-Table GPO_Name,Old_Owner,New_Owner
Run Code Online (Sandbox Code Playgroud)
调用运算符和脚本块(或 $( ) 但会等到完成):
& {
foreach ($gp in $GPINFO) {
new-object psobject -Property @{
GPO_Name = $gp.DisplayName
Old_Owner = $aclBefore
New_Owner = $aclAfter
}
}
} | Format-Table GPO_Name,Old_Owner,New_Owner
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12780 次 |
| 最近记录: |