Wie sammle ich Netzwerkstatistiken mit PowerShell oder Klasse von Remote Machine in C#?C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Wie sammle ich Netzwerkstatistiken mit PowerShell oder Klasse von Remote Machine in C#?

Post by Anonymous »

Ich versuche einen Ansatz zu erforschen und zu finden, der mir eine Ausgabe liefert, die die Netzwerkstatistik pro jeder Verbindung untersucht. Ich weiß, dass PowerShell die Gesamtstatistik des Netzwerks wie < /p>
bereitstellt(using WSMan)
pipeline.AddCommand("Get-NetTCPConnection");
pipeline.AddCommand("Select-Object").AddArgument("LocalAddress, LocalPort, RemoteAddress, RemotePort, State, OwningProcess");

pipeline.AddCommand("Get-NetAdapterStatistics");
pipeline.AddCommand("Select-Object").AddArgument("InterfaceAlias, OutgoingBytes, IncomingBytes");
< /code>
LocalAddress LocalPort RemoteAddress RemotePort State OwningProcess
-----------------------------------------------------------------------------------------------------
192.168.1.10 49242 93.184.216.34 80 ESTABLISHED 1234
192.168.1.10 49243 93.184.216.34 443 ESTABLISHED 1235

InterfaceAlias OutgoingBytes IncomingBytes
-------------------------------------------------
Ethernet 1000000 2000000
Wi-Fi 500000 1500000
< /code>
Is there a class that I can use that will provide this information?
What I am expecting the output would be similar to the one below
--------------------------------------------------------
Local Address: 192.168.1.10
Local Port: 443
Remote Address: 203.0.113.5
Remote Port: 12345
State: Established
Bytes Sent: 1048576
Bytes Received: 2048000
Packets Sent: 512
Packets Received: 768
Owning Process ID: 1234
--------------------------------------------------------
< /code>
My approach on Getting the IPv4Address and other parameters:
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;

class Program
{
static void Main()
{
string remoteMachine = "RemoteMachine";
string username = "user";
string password = "password";

// WSMan connection info
PSCredential credentials = new PSCredential(username, new System.Security.SecureString());
foreach (char c in password) credentials.Password.AppendChar(c);

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
new Uri($"http://{remoteMachine}:5985/wsman"),
"http://schemas.microsoft.com/powershell ... PowerShell",
credentials
);

// Create a runspace
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();

// Create the pipeline
using (PowerShell pipeline = PowerShell.Create())
{
// Execute remotely
pipeline.Runspace = runspace;
// Add the Get-NetIPConfiguration command to the pipeline
pipeline.AddCommand("Get-NetIPConfiguration");
pipeline.AddCommand("Select-Object").AddParameter("Property", new string[] { "InterfaceAlias", "IPv4Address", "InterfaceIndex", "InterfaceDescription", "NetProfile", "IPv4DefaultGateway", "DNSServer" });

try
{
// Results
Collection
results = pipeline.Invoke();

if (results.Count > 0)
{

foreach (PSObject result in results)
{
Console.WriteLine("InterfaceAlias : " + result.Members["InterfaceAlias"]?.Value);
Console.WriteLine("InterfaceIndex : " + result.Members["InterfaceIndex"]?.Value);
Console.WriteLine("InterfaceDescription: " + result.Members["InterfaceDescription"]?.Value);
Console.WriteLine("NetProfile Name : " + result.Members["NetProfile"]?.Value);
Console.WriteLine("IPv4Address : " + result.Members["IPv4Address"]?.Value);
Console.WriteLine("IPv4 DefaultGateway: " + result.Members["IPv4DefaultGateway"]?.Value);
Console.WriteLine("DNSServer : " + result.Members["DNSServer"]?.Value);
Console.WriteLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error executing the pipeline: " + ex.Message);
}
}

// Close the runspace
runspace.Close();
}
}
< /code>
my outcome are the following:
InterfaceAlias : Ethernet1
InterfaceIndex :
InterfaceDescription:
NetProfile Name :
IPv4Address : MSFT_NetIPAddress (Name = ";P?9;!K5???9F66F66;99;", CreationClassName = "", SystemCreationClassName = "", SystemName = "")
IPv4 DefaultGateway:
DNSServer :
< /code>
I tried to follow like this structure:
Image

WQL Queries

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post