我可以停止STDOUT打印对象信息吗?

时间:2018-04-17 08:15:21

标签: powershell stdout

我正在使用WScript.Shell COM对象运行Powershell:

def Powershell.exec(data)
    #Encoding ensures there'll be no issues with complex quoted data
    require 'base64'
    data = Base64.strict_encode64(data.encode("utf-16le"))

    #Execute encoded commands:
    require 'win32ole'
    shell = WIN32OLE.new("WScript.Shell")
    app = shell.exec("powershell -NoProfile -NonInteractive -WindowStyle Hidden -EncodedCommand \"" + data + "\"")
    return {:STDOUT=>app.StdOut.ReadAll(),:STDERR=>app.StdErr.ReadAll()}
end

我正在运行以下powershell脚本:

$data = ConvertFrom-Json $data

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

#region begin GUI{ 

$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '500,400'
$Form.text = "Form"
$Form.TopMost = $false
$form.Resize = $false
$form.FormBorderStyle = 'FixedToolWindow'

$okButton = New-Object System.Windows.Forms.Button
$okButton.text = "OK"
$okButton.width = 150
$okButton.height = 50
$okButton.location = New-Object System.Drawing.Point(96,330)
$okButton.Add_Click({
    ForEach($item in $ListView1.SelectedIndices){
        Write-Host $item
    }
    $Form.close()
})

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.text = "Cancel"
$cancelButton.width = 150
$cancelButton.height = 50
$cancelButton.location = New-Object System.Drawing.Point(256,330)
$cancelButton.add_click({
    $Form.close()
})

$ListView1 = New-Object System.Windows.Forms.ListView
$ListView1.text = "listView"
$ListView1.width = 490
$ListView1.height = 300
$ListView1.location = New-Object System.Drawing.Point(5,5)
$ListView1.MultiSelect = 1
$ListView1.View = 'Details'
$ListView1.FullRowSelect = 1
$ListView1.Font = 'Microsoft Sans Serif,20'

#Generate headers
ForEach($d in $data.head){
    $col = $ListView1.columns.add($d)
    $col.width = -2
}

#Generate items
ForEach($item in $data.body){
    $lvi = New-Object System.Windows.Forms.ListViewItem($item)
    For($i=1;$i -lt $item.length; $i++){
        $lvi.SubItems.Add($item[$i])
    }
    $ListView1.items.add($lvi)
}

$Form.controls.AddRange(@($okButton, $cancelButton ,$ListView1))

[Console]::Out.Flush() 
[void]$Form.ShowDialog()

然而目前最大的问题是,当从WScript.Shell读取STDOUT时,我得到了这个:

Name      : 
BackColor               : Color [Window]
Bounds                  : {X=0,Y=1015,Width=486,Height=35}
Checked                 : False
Focused                 : False
Font                    : [Font: Name=Microsoft Sans Serif, Size=20, Units=3, 
                          GdiCharSet=1, GdiVerticalFont=False]
ForeColor               : Color [WindowText]
Group                   : 
ImageIndex              : -1
ImageKey                : SK46587402
ImageList               : 
IndentCount             : 0
Index                   : 28
ListView                : System.Windows.Forms.ListView, Items.Count: 29, 
                          Items[0]: ListViewItem: {hw_node}
Name                    : 
Position                : {X=4,Y=1015}
Selected                : False
StateImageIndex         : -1
SubItems                : {ListViewSubItem: {hw_subcatchment}, 
                          ListViewSubItem: {SK46587402}}
Tag                     : 
Text                    : hw_subcatchment
ToolTipText             : 
UseItemStyleForSubItems : True

BackColor : Color [Window]
Bounds    : {X=0,Y=0,Width=0,Height=0}
Font      : [Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, 
            GdiCharSet=0, GdiVerticalFont=False]
ForeColor : Color [WindowText]
Tag       : 
Text      : SK46587502
Name      : 

BackColor               : Color [Window]
Bounds                  : {X=0,Y=1050,Width=486,Height=35}
Checked                 : False
Focused                 : False
Font                    : [Font: Name=Microsoft Sans Serif, Size=20, Units=3, 
                          GdiCharSet=1, GdiVerticalFont=False]
ForeColor               : Color [WindowText]
Group                   : 
ImageIndex              : -1
ImageKey                : SK46587502
ImageList               : 
IndentCount             : 0
Index                   : 29
ListView                : System.Windows.Forms.ListView, Items.Count: 30, 
                          Items[0]: ListViewItem: {hw_node}
Name                    : 
Position                : {X=4,Y=1050}
Selected                : False
StateImageIndex         : -1
SubItems                : {ListViewSubItem: {hw_subcatchment}, 
                          ListViewSubItem: {SK46587502}}
Tag                     : 
Text                    : hw_subcatchment
ToolTipText             : 
UseItemStyleForSubItems : True

对于大约2100行,直到最后我得到输出我用Write-Host写到stdout ...有没有办法可以'沉默'Powershell喷出它创建的对象?

1 个答案:

答案 0 :(得分:1)

在黑暗中拍摄时,我尝试将[void]添加到Generate items中的多个方法调用中:

#Generate items
ForEach($item in $data.body){
    $lvi = New-Object System.Windows.Forms.ListViewItem($item)
    For($i=1;$i -lt $item.length; $i++){
        [void]$lvi.SubItems.Add($item[$i])
    }
   [void]$ListView1.items.add($lvi)
}

令我惊讶的是,这可以防止我遇到的问题。我想这个对象是由add()方法创建和返回的,所以我必须告诉Powershell我不需要数据吗?似乎有点奇怪,这是必需的,但很高兴我设法让它工作。