Powershell - 创建宽表 [英] Powershell - Creating Wide Tables

查看:36
本文介绍了Powershell - 创建宽表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好!

我是 Powershell 新手,试图了解数据输出的工作原理.我试图找出 Active Directory 中两个用户之间的差异.我找到了一个有效的解决方案(比较两个 AD 用户帐户上的对象),但相关字段中的一些数据被截断了...这没有帮助.

I am a Powershell novice trying to understand how data output works. I am trying to find the differences between two users in Active Directory. I found a solution that worked (Compare-Object on two AD user accounts), but the data some of in the relevant fields was truncated with an ... which didn't help.

我在页面底部找到了一个看起来非常优雅的解决方案:http://poshoholic.com/2010/11/11/powershell-quick-tip-creating-wide-tables-with-powershell/

I found a solution which seems very elegant at the bottom of the page here: http://poshoholic.com/2010/11/11/powershell-quick-tip-creating-wide-tables-with-powershell/

我尝试将这两者合并为一个脚本.这就是我所拥有的:

I attempted to combine these two into a single script. This is what I have:

$user1 = get-aduser jdoe -Properties *
$user2 = get-aduser jsmith -Properties *

$Usercomparison = @()

$user1.GetEnumerator() | ForEach-Object {
    If ($User2.($_.Key) -eq $_.Value)
    {
        $Comparison = 'Equal'
    }
    else
    {
        $Comparison = 'Different'
    }

    $UserObj = New-Object PSObject -Property ([ordered]@{
        Property = $_.Key
        User1 = $_.Value
        User2 = $User2.($_.Key)
        Comparison = $Comparison
    })
    $UserComparison += $UserObj
}

$UserComparison
| Format-Table -Property * -AutoSize `
| Out-String -Width 4096 `
| Out-File C:\Users\USER\Desktop\differences.txt

这会产生不允许空管道元素"的错误.如果我删除一行返回将第一条管道放在 $UserComparison 变量之后...

This produces an error that "an empty pipe element is not allowed". If I delete a line return to put the first pipe line after the $UserComparison variable...

$UserComparison | Format-Table -Property * -AutoSize `
| Out-String -Width 4096 `
| Out-File C:\aliases.txt

...然后创建了文本文件,但格式错误.输出中只出现前两列,右侧有大量浪费的空白,每行后面有几个空行......与网站上的示例完全不同.

...then the text file is created, but it's badly formatted. Only the first two columns appear in the output and there is a ton of wasted whitespace to the right and several blank line returns after each line... nothing like the example on the website.

这是因为我找到的脚本将数据写入变量,然后只是在屏幕上打印变量,而不是使用可以正确输出的命令吗?我觉得我有我需要的所有部分,只是没有正确的配置来获得我想要的输出.

Is this because the script I found writes the data to a variable and then just prints the variable on screen instead of using a command that can be output properly? I feel like I have all the pieces that I need, just not in the right configuration to get the output I want.

谢谢!

推荐答案

所以,#1 行:

$UserComparison
| Format-Table -Property * -AutoSize `
| Out-String -Width 4096 `
| Out-File C:\Users\USER\Desktop\differences.txt

不起作用,因为您是第一次执行

Doesn't work because you are first executing

$UserComparison

输出 $UserComparison 的内容.接下来,你执行

Which outputs the contents of $UserComparison. Next, you execute

| Format-Table -Property * -AutoSize `

哪个错误是因为没有被传送到Format-Table.Format-Table 语句末尾的刻度"( ` )是一个继续行语句,即第二个版本:

Which errors out because nothing is being piped into Format-Table. The "ticks" ( ` ) at the end of the Format-Table statement is a continue line statement i.e. the second version:

$UserComparison | Format-Table -Property * -AutoSize `
| Out-String -Width 4096 `
| Out-File C:\aliases.txt

正确,因为它将被解释为一条巨线.

Is correct because it will be interpreted as one giant line.

第二个问题,您遇到问题的原因是因为 4096 个字符没有足够的空间来容纳所有内容,因此被截断了.请记住,-AutoSize 将计算最长项 的宽度,并将其作为列的宽度.有些项目太长了.例如.对我来说,thumbnailPhoto(恰好是我数组中的第 140 项):

Second question, the reason why you are having issues is because 4096 characters is not enough space to hold everything, and so is truncated. Remember, -AutoSize will calculate the width of the longest item, and make that the width of the column. There are some items that are too long. For ex. For me, the thumbnailPhoto (which happened to be item 140 in my array):

$UserComparison[140]

给出这样的东西(根据宽度截断):

Gives something like this (truncated depending on width):

Property       User1
--------       -----
thumbnailPhoto {255 216 255 224 0 16 74 70 73 70 0 1 1 1 0 96 0...

当我计算它的宽度时,它给了我:

When I calculate the width of this, it gives me:

#Calculate  width of User1
($UserComparison[140].User1 | Out-String).Length

7555

#Calculate width of full field
($UserComparison[140] | Out-String).Length

12297

是的,User1 的长度为 7,555 个字符.这意味着 Format-Table -Autosize 将使 User1至少 7,555 个字符宽,这显然被 4,096 的宽度限制截断了您在 Out-String 上指定,然后不会显示 User2Comparison 列.在这种情况下,您的 Out-String 需要有至少 12,297 的宽度才能显示完整的字段.

Yes, User1 is 7,555 characters long. That means that Format-Table -Autosize will make the User1 column at least 7,555 characters wide, which obviously is truncated by the 4,096 width limit that you specified on the Out-String, and then won't display the User2 or Comparison columns. In this case, your Out-String needs to have a width of at least 12,297 wide in order to display the full field.

解决方法是在 Out-String 上指定更大的宽度,保证更宽,例如 50,000,因此您的代码将是:

The workaround is to specify an even bigger width on the Out-String that is guaranteed to be wider, like, say, 50,000 so your code would be:

$UserComparison | Format-Table -Property * -AutoSize `
| Out-String -Width 50000 `
| Out-File C:\Users\USER\Desktop\differences.txt

现在,这样做的缺点是文本文件中的每一行都将是最长项目的全宽,因此(在我的情况下)每行的长度为 12,297 个字符.这使内容更难阅读.

Now, the downside to doing things this way is that every line in the text file will be the full width of the longest item, and so (in my case) every line will be 12,297 characters long. This makes things harder to read.

其他输出东西的方法是:

Other ways to output things would be to:

限制只显示属性和比较列:

Limit things to just displaying the Property and Comparison columns:

$UserComparison | Select Property, Comparison `
| Format-Table -Property * -AutoSize `
| Out-String -Width 4096 `
| Out-File SimpleCompare.txt

或者,如果您需要查看完整值是什么,请将每个属性切成带有 ForEach-Object 的单独表,然后将其传递以便更易于阅读,并且每个属性仅限于它的特定宽度:

Or if you need to see what the full values are, chop each property up into a separate table with a ForEach-Object, and then pass that through so that would be easier to read, and each property is limited to it's specific width:

$UserComparison | Select Property, Comparison, User1, User2 `
| ForEach-Object { $_ | Format-Table -Property * -AutoSize `
| Out-String -Width 50000 `
| Out-File EasyToRead.txt -Append }

这篇关于Powershell - 创建宽表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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