1

如何更改代码生成的输出的显示方式:

$apps = Import-CSV apps.csv
$computers = Import-CSV compobj.csv
foreach($computer in $computers) {    
    $computerLob = $computer.lob
    $lobApps = $apps | ? {$_.lob -eq $computerLob }
    foreach($app in $lobApps){
       $computerHostname = $computer.hostname
       $appLocation = $app.location
       $installed=Test-Path "\\$computerHostname\$appLocation"      
       New-Object PSObject @{Computer=$computer.hostname;App=$app.appname;Installed=$installed} 
    }
}

我希望更改代码的呈现方式。这是它的样子:

Name                         Value                                                                                                            
----                         -----                                                                                                            
Installed                    True                                                                                                             
App                          App1                                                                 
Computer                     171.159.192.10
Installed                    True                                                                                                             
App                          App2                                                                         
Computer                     171.159.192.10

我希望它看起来像这样:

Computer                 App1    App2
-----------              ------    -----
171.159.192.10           True     True
4

3 回答 3

2

您将哈希表New-Object作为其 ctor 参数而不是属性集传递给。将其更改为:

New-Object PSObject -Property @{
  Computer=$computer.hostname
  App=$app.appname
  Installed=$installed
}
于 2013-10-27T22:20:39.973 回答
1

如果您使用的是 PowerShell V3,而不是使用 new-object,您可以这样做:

[pscustomobject]@{Computer=$computer.hostname;App=$app.appname;Installed=$installed} 

在 V2 上,不要忘记使用-Property参数,例如:

new-object psobject -property @{Computer=$computer.hostname;App=$app.appname;Installed=$installed} 

要强制输出顺序,您可以使用 Format-Table:

$obj = new-object psobject -property @{Computer=$computer.hostname;App=$app.appname;Installed=$installed} 
$obj | Format-Table Computer,App,Installed
于 2013-10-27T22:20:16.963 回答
0

这就是我的意思(OP 在评论中提出的问题的后续问题,太大而无法容纳在那里):

function MyFunction(){
  $apps = Import-CSV apps.csv
  $computers = Import-CSV compobj.csv
  foreach($computer in $computers) {    
    $computerLob = $computer.lob
    $lobApps = $apps | ? {$_.lob -eq $computerLob }
    foreach($app in $lobApps){
      $computerHostname = $computer.hostname
      $appLocation = $app.location
      $installed=Test-Path "\\$computerHostname\$appLocation"      
      New-Object PSObject @{Computer=$computer.hostname;App=$app.appname;Installed=$installed} 
    }
  }
}

MyFunction | select Computer,App,Installed

证明上述内容的简化测试用例应该有效:

function MyFunction(){
  New-Object PSObject -Property @{
    Computer="computer"
    App="app"
    Installed="installed"
  }
}

MyFunction | select Computer,App,Installed
于 2013-10-28T00:11:53.967 回答