如何在 Powershell 中返回格式化为表格的自定义对象? [英] How do I return a custom object in Powershell that's formatted as a table?

查看:38
本文介绍了如何在 Powershell 中返回格式化为表格的自定义对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 powershell 还很陌生,所以如果我把这一切都弄错了,我一点也不会感到惊讶.我正在尝试创建一个函数,该函数在执行时打印格式化为表格的结果.也许甚至可以将这些结果通过管道传输到另一个函数以进行进一步分析.

I'm pretty new to powershell, so I won't be surprised at all if I'm going about this all wrong. I'm trying to create a function that, when executed, prints results formatted as a table. Maybe it would even be possible to pipe those results to another function for further analysis.

这是我目前所拥有的.这是一个简单的函数,它遍历路径列表并收集目录名称和该目录中的项目数,将数据放入哈希表中,并返回哈希表数组:

Here's what I have so far. This is a simple function that iterates through a list of paths and collects the name of the directory and the number of items in that directory, putting the data in a hashtable, and returning an array of hashtables:

function Check-Paths(){
  $paths = 
    "C:\code\DirA",
    "C:\code\DirB"
  $dirs = @()
  foreach ($path in $paths){
      if (Test-Path $path){
        $len = (ls -path $path).length
      }
      else{
        $len = 0
      }
      $dirName = ($path -split "\\")[-1]
      $dirInfo = @{DirName = $dirName; NumItems = $len}
      $dirs += $dirInfo
  }
  return $dirs

}

这看起来很简单.但是,当我运行命令时,这就是我得到的:

That seems straightforward enough. However, when I go run the command, this is what I get:

PS > Check-Paths

Name                           Value
----                           -----
DirName                        DirA
NumItems                       0
DirName                        DirB
NumItems                       0

想要的是这个:

DirName                       NumItems
-------                       --------
DirA                          0
DirB                          0

我可以修改我的函数以使用 write 语句,但我认为必须有更好的方法来做到这一点.有没有办法将数据格式化为表格,如果可以将数据通过管道传输到另一种方法,那就更好了?

I could just hack my function to use a write statement, but I think there must be a much better way to do this. Is there a way to get the data formatted as a table, even better if that can be such that it can be piped to another method?

推荐答案

如何使用

return new-object psobject -Property $dirs

这将返回一个对象,其属性与哈希表中的项目相匹配.然后,您可以使用内置的 powershell 格式化 cmdlet 使其看起来像您想要的那样.由于您只有 2 个属性,因此默认情况下它会被格式化为表格.

That would return an object whose properties match the items in the hashtable. Then you can use the built-in powershell formatting cmdlets to make it look like you want. since you only have 2 properties, it will be formatted as a table by default.

这是整个事情的样子(在各种建议之后):

Here's how the whole thing would look (After the various suggestions):

function Check-Paths(){
  $paths = 
    "C:\code\DirA",
    "C:\code\DirB"
  $dirs = @()
  foreach ($path in $paths){
      if (Test-Path $path){
        $len = (ls -path $path).length
      }
      else{
        $len = 0
      }
      $dirName = ($path -split "\\")[-1]
      new-object psobject -property  @{DirName = $dirName; NumItems = $len}
  }
}

这篇关于如何在 Powershell 中返回格式化为表格的自定义对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆