简体   繁体   English

Out-GridView(powershell 4)中的额外空列?

[英]Extra empty column in the Out-GridView (powershell 4)?

I have the following code snippet:我有以下代码片段:

 function MyTestString { [string]$OutString $OutString = 'ABCDEFG' #$OutString | Out-GridView -Wait -Title "String inside function" return $OutString } $MyString = MyTestString | Out-GridView -Wait -Title "Getting Extra Column"

When I run it in Powershell , I get an extra empty column in the grid view.当我在Powershell中运行它时,我在网格视图中得到一个额外的空列。 If Out-GridView is placed inside the function then there is no empty column.如果Out-GridView放在 function 内,则没有空列。 The same thing is happening with Export-Clixml . Export-Clixml也发生了同样的事情。

How can I avoid the empty column in the grid?如何避免网格中的空列? I would like to know why do I get an empty column in GridView .我想知道为什么我在GridView中得到一个空列。

Here's a screen shot of the extra empty column:这是额外空列的屏幕截图:

Thank you for your help.谢谢您的帮助。

That would be extra row instead of column, and it's caused by the function body.那将是额外的row而不是列,这是由 function 主体引起的。 Let's examine it.让我们检查一下。

function MyTestString
{
    [string]$OutString
    $OutString = 'ABCDEFG'
    return $OutString
}

MyTestString     

ABCDEFG

Okay, looks like a direct call returns extra row.好的,看起来直接调用会返回额外的行。 Is it really so?真的是这样吗?

$o=MyTestString
$o 

ABCDEFG

Let's look inside $o :让我们看看$o内部:

$o.GetType()
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

Now we are getting somewhere.现在我们正在取得进展。 The function returns an array. function 返回一个数组。 How about its contents?它的内容如何?

PS C:\> $o[0]


PS C:\> $o[1]
ABCDEFG

The zeroth element doesn't contain anything but emptiness.第零个元素除了空虚之外什么都不包含。 This looks strange, so let's make sure Powershell is using strict mode (which it doesn't do per default. And that is a brain-dead a design decision.)这看起来很奇怪,所以让我们确保 Powershell 正在使用严格模式(默认情况下不会这样做。这是一个脑残的设计决定。)

Set-StrictMode -Version 'latest'
MyTestString
The variable '$OutString' cannot be retrieved because it has not been set.
At line:3 char:13
+     [string]$OutString
+             ~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (OutString:String) [], 
RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

Let's see the function body again:我们再来看看 function 本体:

{   
    [string]$OutString
    $OutString = 'ABCDEFG'
    #$OutString | Out-GridView -Wait -Title "String inside function"
    
    return $OutString
}

Note the [string] part?注意[string]部分? That isn't a varaible declaration.这不是一个可变声明。 It will return its contents to the pipeline.它将其内容返回到管道。 When Powershell isn't in strict mode, it returns a big fat of nothing - the empty element at $o[0] .当 Powershell 未处于严格模式时,它会返回一大堆空虚 - $o[0]处的空元素。 When in strict mode, Powershell notes that an uninitialized variable is being accessed.在严格模式下,Powershell 指出正在访问未初始化的变量。

As for a fix, assign a value to OutString - unless you were trying to work with parameters.至于修复,请为OutString分配一个值 - 除非您尝试使用参数。

function MyTestString
{   
    [string]$OutString = 'ABCDEFG'
    return $OutString
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM