Powershell PSObject-带条件的属性
我想在我的代码的这一部分中添加条件:Powershell PSObject-带条件的属性,powershell,powershell-2.0,psobject,Powershell,Powershell 2.0,Psobject,我想在我的代码的这一部分中添加条件: $objInfos = New-Object PSObject -Property @{ Dossier = [string]"$($logs[0])" "Taille totale" = [double]$logs[1] "Categorie recherchee" = [double]$logs[2]
$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])"
}
我试过这种方法,但没有成功
哈希文本中的键后缺少“=”运算符
有人知道怎么做吗?您可以在单独的语句中创建
哈希表
,用值填充,然后将其传递给新对象
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
您可以在单独的语句中创建
Hashtable
,用值填充,然后将其传递给newobject
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
您可以根据建议有条件地将条目添加到属性哈希表中:
$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
您可以按照问题评论中的@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]
}
您可以根据建议有条件地将条目添加到属性哈希表中:
$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
您可以按照问题评论中的@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]
}
如果需要,可以使用
addmember
添加属性。我看到你最近在这里问了很多问题,但还没有接受任何答案,尽管有一些很好的答案。请这样做,或者给作者留下评论,以便他们可以在需要时进行修改。如果需要,您可以使用addmember
添加属性。我看到你最近在这里问了很多问题,但还没有接受任何答案,尽管有一些很好的答案。请这样做,或者给作者留下评论,以便他们可以在需要时进行修改。我现在有一个新问题,在条件出现之前,我使用了这个:$tab |选择档案,“Taille total”,“Categorie recherchee”,“Pourcentage”,“Date de dernier acces”|排序对象“Pourcentage”-降序|格式表-自动调整大小|输出字符串-宽度4000 |输出文件-追加O:\***************\ParcoursarPerescence\bilanAnalyse.txt
我如何才能让他只写正确的?我不明白你建议我做什么(对不起,我觉得可笑)。问题是,我需要选择这些,但我不知道如何,是否有可能再次写入这些条件,将结果放入一个变量中,然后进行选择,重新组合所有内容?我有点迷路了,我不知道该怎么做do@Kikopu我不确定你的问题是什么。如果$tab
是您刚刚创建的对象,那么我看不到,为什么您需要管道中的选择命令。只需删除它:$tab |排序对象…
。如果这没有帮助,那么您应该显示当前输出、您想要获得的输出以及它们之间的区别。。。。。。好吧,我不知怎么迷路了,但这很明显。。。。所以是的,它解决了“问题”。我用select命令解决了我以前遇到的一个问题,所以通过再次阅读,我认为我需要它,但是没有。非常感谢@Kikopu请不要移动目标。如果你有一个新问题:发布一个新问题。我现在有一个新问题,在条件出现之前,我使用了这个:$tab |选择档案,“Taille total”,“Categorie recherchee”,“Pourcentage”,“Date de dernier acces”|排序对象“Pourcentage”-降序|格式表-自动调整大小|输出字符串-宽度4000 |输出文件-追加O:\***************\ParcoursarPerescence\bilanAnalyse.txt
我如何才能让他只写正确的?我不明白你建议我做什么(对不起,我觉得可笑)。问题是,我需要选择这些,但我不知道如何,是否有可能再次写入这些条件,将结果放入一个变量中,然后进行选择,重新组合所有内容?我有点迷路了,我不知道该怎么做do@Kikopu我不确定你的问题是什么。如果$tab
是您刚刚创建的对象,那么我看不到,为什么您需要管道中的选择命令。只需删除它:$tab |排序对象…
。如果这没有帮助,那么您应该显示当前输出、您想要获得的输出以及它们之间的区别。。。。。。好吧,我不知怎么迷路了,但这很明显。。。。所以是的,它解决了“问题”。我用select命令解决了我以前遇到的一个问题,所以通过再次阅读,我认为我需要它,但是没有。非常感谢@Kikopu请不要移动目标。如果你有一个新问题:发布一个新问题。