表达式中的哈希表条件语句
Hashtable conditional statement in Expression
我目前正在尝试从 System.Object[] 中提取信息,但我只想从 $var
中获取 Col1
和 Col2
,因为它有很多不必要的专栏。
设置:
$var =
.. Col1 Col2
.. 1 2147483648
.. 2 524288000
.. 3 268435456000
.. 4 268435456000
期望:
$res=
.. Col1 Col2 Col3
.. 1 2147483648 2 GB
.. 2 524288000 500 MB
.. 3 268435456000 250 GB
.. 4 268435456000 250 GB
我可以使用下面的代码获得所需的 Col3:
foreach($t in $var)
{
if($t.Col2% ([math]::pow(1024,3)))
{
$t.Col2/([math]::pow(1024,2))" MB"
}
else
{
$t.Col2/([math]::pow(1024,3))" GB"
}
}
但是,这只显示 Col3 而不是其他列,我想要的是通过选择列和使用列表达式来获得某种哈希值table,但我无法得到任何东西,如果我在以下代码的表达式中放置了一个带有 IF 和 ELSE 的条件语句:
$var| Select `
@{`
l='test';`
e=`
{`
if($_.Col2% ([math]::pow(1024,3)))`
{`
$_.Col2/([math]::pow(1024,2))"MB"`
}`
else`
{`
$_.Col2/([math]::pow(1024,3))"GB"`
}`
}`
}
我的主要目标是提取这些列并将它们批量插入到 SQL 中的 table 中,但我无法使表达式与 ELSE 一起工作,但如果我仅指定 IF 条件然后它似乎工作。
我想知道这是否是 powershell 中散列表达式中 IF-ELSE 语句的某种限制table。
你应该在你的对象中添加一个 ScriptProperty
来计算这个,然后你会像普通的 属性 一样调用它。定义脚本 属性 使用 Add-Member
和脚本块,但使用 $this
而不是 $_
来引用当前对象。
$var | Add-Member -MemberType ScriptProperty -Name DisplaySize -Value {
if($this.Col2 % ([math]::pow(1024,3)))
{
$this.Col2/([math]::pow(1024,2))"MB"
}
else
{
$this.Col2/([math]::pow(1024,3))"GB"
}
}
} -Force
$var[0].DisplaySize
另外,如您所知,PowerShell 包含 KB/MB/GB/etc 的 shorthand 语法。:
1MB # equivalent to writing 1048576
3TB # equivalent to writing 3298534883328
#Solution 1, with column builded in select, condensed
$var | select *, @{N="DisplaySize";E={ "{0} {1}" -f @(if($this.Col2 % ([math]::pow(1024,3))) {($this.Col2/1MB), "MB"} else {($this.Col2/1GB), "GB"} ) }}
#Solution 2, with column builded in select, explain
$var | select *, @{N="DisplaySize";E={
if($this.Col2 % ([math]::pow(1024,3)))
{
"{0} MB" -f ($this.Col2/1MB)
}
else
{
"{0} GB" -f ($this.Col2/1GB)
}
}}
#Solution 3, with column builded, logique to take 2 columns
$var | select *, @{N="DisplaySizeMB";E={$this.Col2/1MB}}, @{N="DisplaySizeGB";E={$this.Col2/1GB}}
我目前正在尝试从 System.Object[] 中提取信息,但我只想从 $var
中获取 Col1
和 Col2
,因为它有很多不必要的专栏。
设置:
$var =
.. Col1 Col2
.. 1 2147483648
.. 2 524288000
.. 3 268435456000
.. 4 268435456000
期望:
$res=
.. Col1 Col2 Col3
.. 1 2147483648 2 GB
.. 2 524288000 500 MB
.. 3 268435456000 250 GB
.. 4 268435456000 250 GB
我可以使用下面的代码获得所需的 Col3:
foreach($t in $var)
{
if($t.Col2% ([math]::pow(1024,3)))
{
$t.Col2/([math]::pow(1024,2))" MB"
}
else
{
$t.Col2/([math]::pow(1024,3))" GB"
}
}
但是,这只显示 Col3 而不是其他列,我想要的是通过选择列和使用列表达式来获得某种哈希值table,但我无法得到任何东西,如果我在以下代码的表达式中放置了一个带有 IF 和 ELSE 的条件语句:
$var| Select `
@{`
l='test';`
e=`
{`
if($_.Col2% ([math]::pow(1024,3)))`
{`
$_.Col2/([math]::pow(1024,2))"MB"`
}`
else`
{`
$_.Col2/([math]::pow(1024,3))"GB"`
}`
}`
}
我的主要目标是提取这些列并将它们批量插入到 SQL 中的 table 中,但我无法使表达式与 ELSE 一起工作,但如果我仅指定 IF 条件然后它似乎工作。
我想知道这是否是 powershell 中散列表达式中 IF-ELSE 语句的某种限制table。
你应该在你的对象中添加一个 ScriptProperty
来计算这个,然后你会像普通的 属性 一样调用它。定义脚本 属性 使用 Add-Member
和脚本块,但使用 $this
而不是 $_
来引用当前对象。
$var | Add-Member -MemberType ScriptProperty -Name DisplaySize -Value {
if($this.Col2 % ([math]::pow(1024,3)))
{
$this.Col2/([math]::pow(1024,2))"MB"
}
else
{
$this.Col2/([math]::pow(1024,3))"GB"
}
}
} -Force
$var[0].DisplaySize
另外,如您所知,PowerShell 包含 KB/MB/GB/etc 的 shorthand 语法。:
1MB # equivalent to writing 1048576
3TB # equivalent to writing 3298534883328
#Solution 1, with column builded in select, condensed
$var | select *, @{N="DisplaySize";E={ "{0} {1}" -f @(if($this.Col2 % ([math]::pow(1024,3))) {($this.Col2/1MB), "MB"} else {($this.Col2/1GB), "GB"} ) }}
#Solution 2, with column builded in select, explain
$var | select *, @{N="DisplaySize";E={
if($this.Col2 % ([math]::pow(1024,3)))
{
"{0} MB" -f ($this.Col2/1MB)
}
else
{
"{0} GB" -f ($this.Col2/1GB)
}
}}
#Solution 3, with column builded, logique to take 2 columns
$var | select *, @{N="DisplaySizeMB";E={$this.Col2/1MB}}, @{N="DisplaySizeGB";E={$this.Col2/1GB}}