简体   繁体   中英

Programmatical equivalence of Remove-NetEventSession

Remove-NetEventSession is a powershell cmdlet that removes a network event session.

It's usually used after Stop-NetEventSession and Get-NetEventSession :

$session = Get-NetEventSession
Stop-NetEventSession $session.Name
Remove-NetEventSession $session.Name

Is there a way to achieve the same result using .Net? (Not by executing a powershell script)

Yes, there is.

Using the WMI API for .Net ( System.Management ) you can manipulate the MSFT_NetEventSession instance.

The following example gets the running session ( there can be only one ), stops it if it's running and removes it:

var netEventSession = new ManagementClass("/root/standardcimv2:MSFT_NetEventSession").
    GetInstances().
    Cast<ManagementObject>().
    SingleOrDefault();
if (netEventSession != null)
{
    if ((byte)netEventSession["SessionStatus"] == 0)
    {
        netEventSession.InvokeMethod("Stop", null, null);
    }
    netEventSession.Delete();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM