1

I wanna get all the details from the PowerShell cmdlet Get-NetConnectionProfile but it doesn't show it in the textbox and I don't really know why.

Console Screenshot

I tried other "get" cmdlets like Get-NetAdapter, which worked perfectly. I could see everything in the textbox.

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;
3
  • This isn't a great way to use PowerShell from C#. You should use the PowerShell class instead of trying to run your commands in a separate process. Commented Sep 14, 2019 at 16:32
  • Does Get-NetConnectionProfile work in PowerShell? What output do you expect? You may need to pipe the results Commented Sep 14, 2019 at 20:07
  • By "all details", I assume you mean the default output seen when you run Get-NetConnectionProfile. See my answer and let me know Commented Sep 15, 2019 at 14:52

1 Answer 1

2

By "all details", I assume you mean the default output seen when you run Get-NetConnectionProfile:

PS C:\Temp> Get-NetConnectionProfile

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

Program 1 is a pure PowerShell solution using WinForms

I tried to execute Get-NetConnectionProfile from c# (see Creating an InitialSessionState but got a `Provider load failure' and could not solve the issue (This is the PowerShell class solution mentioned by boxdog).

Note: I have had success with executing PowerShell cmdlets from a C# program (see How do I login to a XEN session from a C# program using a secure string password? so I know its possible

Program 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()

Output

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.