运行脚本或函数时没有输出。仅在命令行上输出

时间:2017-01-10 19:20:59

标签: powershell powershell-v5.0

我有一个包含以下代码的脚本文件 它运行相同的代码3次。

  1. 在脚本中,在函数内部
  2. 在脚本中,在函数外部
  3. 在脚本之外,在命令行上
  4. 源代码:

    Clear-Host
    
    $myKeys = @('key1','key2','key3','key4','key5')
    $myValues = @('one','two','three','four','five')
    
    # Declare function
    Function Invoke-MyArray
    {
        [CmdletBinding()]   param ()
    
        Write-Output "Function: $($MyInvocation.Mycommand)"
    
        $myLocalArray = @()
    
        foreach ($element in $myKeys)
        {
            $key = $element
            $val = $myValues[$myKeys.IndexOf($key)]
    
            $myObject = New-Object -TypeName PSObject -Property @{
                key = $key
                val = $val
            }   
    
            $myLocalArray += $myObject
        }
    
        Write-Output '### 1 ### $myArray'
        Write-Output '------------------'
        $myArray
    
        Write-Output '### 2 ### $myArray.getType()'
        Write-Output '----------------------------'
        $myArray.getType()
    
        Write-Output '### 3 ### $myArray.getType() | Out-String'
        Write-Output '-----------------------------------------'
        $myArray.getType() | Out-String
    
    
    
        Write-Output '### 4 ### $myArray[0]'
        Write-Output '---------------------'
        $myArray[0]
    
        Write-Output '### 5 ### $myArray[0].getType()'
        Write-Output '-------------------------------'
        $myArray[0].getType()
    
        Write-Output '### 6 ### $myArray[0].getType() | Out-String'
        Write-Output '--------------------------------------------'
        $myArray[0].getType() | Out-String
    
    }
    
    # Call function
    Write-Output '====================='
    Write-Output '=== Call function ==='
    Write-Output '====================='
    Invoke-MyArray
    
    Write-Output '==============================================='
    Write-Output '=== Execute same code, without the function ==='
    Write-Output '==============================================='
    # Execute same code, without the function
    $myArray = @()
    
    foreach ($element in $myKeys)
    {
        $key = $element
        $val = $myValues[$myKeys.IndexOf($key)]
    
        $myObject = New-Object -TypeName PSObject -Property @{
            key = $key
            val = $val
        }   
    
        $myArray += $myObject
    }
    
    
    Write-Output '### 1 ### $myArray'
    Write-Output '------------------'
    $myArray
    
    Write-Output '### 2 ### $myArray.getType()'
    Write-Output '----------------------------'
    $myArray.getType()
    
    Write-Output '### 3 ### $myArray.getType() | Out-String'
    Write-Output '-----------------------------------------'
    $myArray.getType() | Out-String
    
    
    
    Write-Output '### 4 ### $myArray[0]'
    Write-Output '---------------------'
    $myArray[0]
    
    Write-Output '### 5 ### $myArray[0].getType()'
    Write-Output '-------------------------------'
    $myArray[0].getType()
    
    Write-Output '### 6 ### $myArray[0].getType() | Out-String'
    Write-Output '--------------------------------------------'
    $myArray[0].getType() | Out-String
    
    
    Write-Output '===================================='
    Write-Output '=== Executed on the command line ==='
    Write-Output '===================================='
    

    结果输出:

    =====================
    === Call function ===
    =====================
    Function: Invoke-MyArray
    ### 1 ### $myArray
    ------------------
    
    key  val  
    ---  ---  
    key1 one  
    key2 two  
    key3 three
    key4 four 
    key5 five 
    ### 2 ### $myArray.getType()
    ----------------------------
    
    ### 3 ### $myArray.getType() | Out-String
    -----------------------------------------
    
    IsPublic IsSerial Name                                     BaseType                                                                               
    -------- -------- ----                                     --------                                                                               
    True     True     Object[]                                 System.Array                                                                           
    
    
    
    ### 4 ### $myArray[0]
    ---------------------
    key1 one  
    ### 5 ### $myArray[0].getType()
    -------------------------------
    
    ### 6 ### $myArray[0].getType() | Out-String
    --------------------------------------------
    
    IsPublic IsSerial Name                                     BaseType                                                                               
    -------- -------- ----                                     --------                                                                               
    True     False    PSCustomObject                           System.Object                                                                          
    
    
    
    ===============================================
    === Execute same code, without the function ===
    ===============================================
    ### 1 ### $myArray
    ------------------
    key1 one  
    key2 two  
    key3 three
    key4 four 
    key5 five 
    ### 2 ### $myArray.getType()
    ----------------------------
    
    ### 3 ### $myArray.getType() | Out-String
    -----------------------------------------
    
    IsPublic IsSerial Name                                     BaseType                                                                               
    -------- -------- ----                                     --------                                                                               
    True     True     Object[]                                 System.Array                                                                           
    
    
    
    ### 4 ### $myArray[0]
    ---------------------
    key1 one  
    ### 5 ### $myArray[0].getType()
    -------------------------------
    
    ### 6 ### $myArray[0].getType() | Out-String
    --------------------------------------------
    
    IsPublic IsSerial Name                                     BaseType                                                                               
    -------- -------- ----                                     --------                                                                               
    True     False    PSCustomObject                           System.Object                                                                          
    
    
    
    ====================================
    === Executed on the command line ===
    ====================================
    
    
    PS C:\>     Write-Output '### 1 ### $myArray'
        Write-Output '------------------'
    
    ### 1 ### $myArray
    ------------------
    PS C:\> $myArray
    
    key  val  
    ---  ---  
    key1 one  
    key2 two  
    key3 three
    key4 four 
    key5 five 
    
    
    PS C:\>     Write-Output '### 2 ### $myArray.getType()'
        Write-Output '----------------------------'
    
    ### 2 ### $myArray.getType()
    ----------------------------
    PS C:\> $myArray.getType()
    
    IsPublic IsSerial Name                                     BaseType                                                                               
    -------- -------- ----                                     --------                                                                               
    True     True     Object[]                                 System.Array                                                                           
    
    
    PS C:\>     Write-Output '### 3 ### $myArray.getType() | Out-String'
        Write-Output '-----------------------------------------'
    
    ### 3 ### $myArray.getType() | Out-String
    -----------------------------------------
    PS C:\> $myArray.getType() | Out-String
    
    IsPublic IsSerial Name                                     BaseType                                                                               
    -------- -------- ----                                     --------                                                                               
    True     True     Object[]                                 System.Array                                                                           
    
    
    
    PS C:\>     Write-Output '### 4 ### $myArray[0]'
        Write-Output '---------------------'
    
    ### 4 ### $myArray[0]
    ---------------------
    PS C:\>     $myArray[0]
    
    
    key  val
    ---  ---
    key1 one
    
    
    PS C:\>     Write-Output '### 5 ### $myArray[0].getType()'
        Write-Output '-------------------------------'
    
    ### 5 ### $myArray[0].getType()
    -------------------------------
    PS C:\>     $myArray[0].getType()
    
    
    IsPublic IsSerial Name                                     BaseType                                                                               
    -------- -------- ----                                     --------                                                                               
    True     False    PSCustomObject                           System.Object                                                                          
    
    
    PS C:\>     Write-Output '### 6 ### $myArray[0].getType() | Out-String'
        Write-Output '--------------------------------------------'
    
    ### 6 ### $myArray[0].getType() | Out-String
    --------------------------------------------
    PS C:\>     $myArray[0].getType() | Out-String
    
    
    IsPublic IsSerial Name                                     BaseType                                                                               
    -------- -------- ----                                     --------                                                                               
    True     False    PSCustomObject                           System.Object                                                                          
    
    
    PS C:\> 
    

    请注意,.getType()调用始终为空(无结果),除非直接从命令行调用。

    仅当.getType()通过管道传递给Out-String时,我们才会在脚本内部看到打印结果。

    为什么我不能仅使用.getType()在我的脚本和/或函数中获得输出?

    编辑(在被Ansgar Wiechers标记为重复之后):
    duplicate包含link到MSDN文章,其中声明:

      

    现在,如果没有数据类型的注册视图,那么   Out-Default查看STREAM中的第一个对象以确定如何   对象具有5个或更多属性的许多属性,它发送   整个STREAM到格式列表,否则它发送整个STREAM   格式表。当它将流发送到Format-Table时,该命令   需要生成列。 通过查看属性来实现此目的   第一个对象 - 那些成为专栏。如果是第一个Object   有2个属性,你将得到一个2列表,即使所有其他   流中的对象有10个属性。

    $myArray是一个包含2个属性的对象的数组 所以,我希望在输出流中得到一个2列的表 相反,我根本没有输出 从这个意义上说,这不是一个重复的问题,但肯定是相关的。

0 个答案:

没有答案