【问题标题】:PSObject -Property with conditionsPSObject - 有条件的属性
【发布时间】:2015-12-02 09:57:14
【问题描述】:

我想在这部分代码中添加条件:

$objInfos = New-Object PSObject -Property @{
                    Dossier = [string]"$($logs[0])"
                    "Taille totale" = [double]$logs[1]
                    "Categorie recherchee" = [double]$logs[2]
                    "Pourcentage" = [double]$logs[3]
                    "Date de dernier acces" = [DateTime]"$($logs[5])"
}

我需要每个案例都有条件,比如:

$objInfos = New-Object PSObject -Property @{
                    if ($test -eq 1){
                        Dossier = [string]"$($logs[0])"
                    }
                    "Taille totale" = [double]$logs[1]
                    "Categorie recherchee" = [double]$logs[2]
                    "Pourcentage" = [double]$logs[3]
                    "Date de dernier acces" = [DateTime]"$($logs[5])"
}

我试过这个方法没用

散列文字中的键后缺少“=”运算符。

请问有人知道怎么做吗?

【问题讨论】:

  • 如果需要,可以使用Add-Member添加属性。我看到你最近在这里问了很多问题,但还没有接受任何答案,尽管有一些很好的答案。请这样做,或者给作者留下评论,以便他们可以在需要时进行修改。

标签: powershell powershell-2.0 psobject


【解决方案1】:

您可以在单独的语句中创建Hashtable,用值填充它,然后将其传递给New-Object cmdlet。

$Hashtable = @{}
if ($test -eq 1){
    $Hashtable.Add('Dossier', [string]"$($logs[0])")
}
$Hashtable.Add("Taille totale", [double]$logs[1])
$Hashtable.Add("Categorie recherchee", [double]$logs[2])
$Hashtable.Add("Pourcentage", [double]$logs[3])
$Hashtable.Add("Date de dernier acces", [DateTime]"$($logs[5])")
$objInfos = New-Object PSObject -Property $Hashtable

如果您希望属性与添加到Hashtable 的元素顺序相同,则需要使用OrderedDictionary 而不是Hashtable

$Hashtable = New-Object System.Collections.Specialized.OrderedDictionary

【讨论】:

  • 我现在有一个新问题,在条件尚未到来之前,我使用了这个:$tab | select Dossier,"Taille Totale","Categorie recherchee", "Pourcentage", "Date de dernier acces" | Sort-Object "Pourcentage" -descending | format-table -autosize | out-string -width 4000 | out-file -append O:\******\******\parcoursArborescence\bilanAnalyse.txt我怎样才能让他只写正确的?
  • 我不明白你建议我做什么(对不起,我觉得很荒谬)。问题是,我需要选择这些,但我不知道如何,是否可以再次编写这些条件,将结果放入变量中,然后选择重新组合所有内容?我有点迷茫,我不知道该怎么办
  • @Kikopu 我不确定你的问题是什么。如果$tab 是您刚刚创建的对象,那么我看不到,为什么您的管道中需要select 命令。只需将其删除:$tab | Sort-Object ...。如果这没有帮助,那么您应该显示当前输出、您想要获得的输出以及它们的不同之处。
  • .....好吧,我不知何故迷路了,但这很明显......所以是的,它解决了“问题”。我使用 select 命令解决了我之前遇到的问题,所以通过再次阅读,我认为我需要它,但没有。非常感谢!
  • @Kikopu 请不要移动目标。如果您有新问题:发布新问题。
【解决方案2】:

您可以按照@PetSerAl 的建议有条件地将条目添加到属性哈希表:

$props = @{
  'Taille totale'         = [double]$logs[1]
  'Categorie recherchee'  = [double]$logs[2]
  'Pourcentage'           = [double]$logs[3]
  'Date de dernier acces' = [DateTime]$logs[5]
}

if ($test -eq 1) {
  $props['Dossier'] = "$($logs[0])"
}

$objInfos = New-Object PSObject -Property $props

您可以使用 Add-Member 作为 cmets 中建议的 @arco444 来回答您的问题:

$objInfos = New-Object PSObject -Property @{
  'Taille totale'         = [double]$logs[1]
  'Categorie recherchee'  = [double]$logs[2]
  'Pourcentage'           = [double]$logs[3]
  'Date de dernier acces' = [DateTime]$logs[5]
}

if ($test -eq 1) {
  $objInfos | Add-Member -Type NoteProperty -Name 'Dossier' -Value "$($logs[0])"
}

或者您也可以添加属性,但根据检查结果设置其值:

$objInfos = New-Object PSObject -Property @{
  'Dossier'               = if ($test -eq 1) {"$($logs[0])"} else {''}
  'Taille totale'         = [double]$logs[1]
  'Categorie recherchee'  = [double]$logs[2]
  'Pourcentage'           = [double]$logs[3]
  'Date de dernier acces' = [DateTime]$logs[5]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多