【问题标题】:PowerShell output to textboxPowerShell 输出到文本框
【发布时间】:2019-09-14 16:25:03
【问题描述】:

我想从 PowerShell cmdlet Get-NetConnectionProfile 获取所有详细信息,但它没有在文本框中显示,我也不知道为什么。

我尝试了其他“get”cmdlet,例如Get-NetAdapter,效果很好。 我可以看到文本框中的所有内容。

ProcessStartInfo psi = new ProcessStartInfo("powershell");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
var proc = Process.Start(psi);

proc.StandardInput.WriteLine("Get-NetConnectionProfile");
proc.StandardInput.WriteLine(@"exit");
string s = proc.StandardOutput.ReadToEnd();

TxtIp.Text = s;

【问题讨论】:

  • 这不是从 C# 使用 PowerShell 的好方法。您应该使用 PowerShell 类而不是尝试在单独的进程中运行您的命令。
  • Get-NetConnectionProfile 是否在 PowerShell 中工作?你期望什么输出?您可能需要通过管道传输结果
  • 通过“所有详细信息”,我假设您指的是运行 Get-NetConnectionProfile 时看到的默认输出。看到我的答案,让我知道

标签: c# powershell


【解决方案1】:

“所有细节”是指运行Get-NetConnectionProfile时看到的默认输出:

PS C:\Temp> Get-NetConnectionProfile

Name             : E12345
InterfaceAlias   : Wi-Fi
InterfaceIndex   : 15
NetworkCategory  : Private
IPv4Connectivity : Internet
IPv6Connectivity : Internet

程序 1 是使用 WinForms 的纯 PowerShell 解决方案

我尝试从 c# 执行 Get-NetConnectionProfile(参见 Creating an InitialSessionState 但出现 `Provider load failure' 并且无法解决问题(这是 boxdog 提到的 PowerShell 类解决方案)。

注意:我已经成功地从 C# 程序执行 PowerShell cmdlet(请参阅 How do I login to a XEN session from a C# program using a secure string password? 所以我知道它可能

计划 1

#region functions

Function ButtonGo_Click
{
    $output = Get-NetConnectionProfile
    $textBoxDisplay.Text = ("Name="+$output.Name.ToString() + [Environment]::NewLine)
    $textBoxDisplay.Text = $textBoxDisplay.Text + ("InterfaceAlias="+$output.InterfaceAlias.ToString() + [Environment]::NewLine)
    $textBoxDisplay.Text = $textBoxDisplay.Text + ("InterfaceIndex="+$output.InterfaceIndex.ToString() + [Environment]::NewLine)
    $textBoxDisplay.Text = $textBoxDisplay.Text + ("NetworkCategory="+$output.NetworkCategory.ToString() + [Environment]::NewLine)
    $textBoxDisplay.Text = $textBoxDisplay.Text + ("IPv4Connectivity="+$output.IPv4Connectivity.ToString() + [Environment]::NewLine)
    $textBoxDisplay.Text = $textBoxDisplay.Text + "IPv6Connectivity="+$output.IPv6Connectivity.ToString()
}

#endregion

#region designer

[System.Windows.Forms.Application]::EnableVisualStyles()
$buttonGo = New-Object 'System.Windows.Forms.Button'
$buttonGo.Location = '10, 10'
$buttonGo.Name = "buttonGo"
$buttonGo.Size = '75, 25'
$buttonGo.TabIndex = 0
$buttonGo.Text = "&Go"
$buttonGo.UseVisualStyleBackColor = $true
$buttonGo.Add_Click({ButtonGo_Click})

$textBoxDisplay = New-Object 'System.Windows.Forms.TextBox'
$textBoxDisplay.Location = '12, 50'
$textBoxDisplay.Multiline = $true
$textBoxDisplay.Name = "textBoxDisplay"
$textBoxDisplay.Size = '470, 150'
$textBoxDisplay.TabIndex = 1

$mainForm = New-Object 'System.Windows.Forms.Form'
$mainForm.Size = '500, 250'
$mainForm.Controls.Add($buttonGo)
$mainForm.Controls.Add($textBoxDisplay)
$mainForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$mainForm.Name = "mainForm"
$mainForm.Text = "Show Get-NetConnectionProfile Output"

#endregion designer

cls
$mainForm.ShowDialog()

输出

【讨论】:

    猜你喜欢
    • 2022-07-23
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2021-02-02
    • 2017-12-24
    • 2019-01-24
    • 2014-01-18
    • 2016-09-23
    相关资源
    最近更新 更多