HomeBlogMagic

Unbind network protokoll filter

Mit Powershell und netcfg

$oBindings = Get-NetAdapterBinding

foreach($oBinding in $oBindings)
{
    # Filter for ms_ndt_MPE
    [string]$sComponentId = $oBinding.ComponentID
    if($sComponentId.StartsWith("ms_ndt_MPE"))
    {
        #Disable-NetAdapterBinding -Name $oBinding.Name -ComponentID $
        Start-Process netcfg -ArgumentList ("-u","$sComponentId") -Wait -PassThru
    }
}

Mit C++

Aus verschiedenen Quellen zusammen kopierter Code:

#include <Netcfgx.h>

bool UninstallComponent(const std::wstring& iComponentId)
{
  bool bSuccess = false;
  HRESULT hr = S_OK;

  if (!SUCCEEDED(CoInitialize(NULL)))
  {
    return false;
  }
  else
  {
    INetCfg *pNetCfg = NULL;
    if (S_OK != CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER, IID_INetCfg, (void**) &pNetCfg))
    {
      return false;
    }
    else
    {
      INetCfgLock* pNetCfgLock;
      hr = pNetCfg->QueryInterface(IID_INetCfgLock, (LPVOID *) &pNetCfgLock);
      if (SUCCEEDED(hr))
      {
        PWSTR szLockedBy = nullptr;
        hr = pNetCfgLock->AcquireWriteLock(10000, L"ConsoleApplication", &szLockedBy);
        if (!SUCCEEDED(hr))
        {
          wprintf(L"Could not lock INetcfg, it is already locked by '%s'", szLockedBy);
        }
        else
        {
          hr = pNetCfg->Initialize(NULL);
          if (!SUCCEEDED(hr))
          {
            printf("Netcnfg not initialiazable\r\n");
          }
          else
          {
            INetCfgComponent *pNetCfgComponent = NULL;
            if (S_OK != pNetCfg->FindComponent(iComponentId.c_str(), &pNetCfgComponent))
            {
              printf("Component not found\r\n");
            }
            else
            {
              // yes, it is installed. obtain INetCfgClassSetup and DeInstall

              GUID guidClass;
              hr = pNetCfgComponent->GetClassGuid(&guidClass);

              if (S_OK == hr)
              {
                INetCfgClass* pncClass;
                hr = pNetCfg->QueryNetCfgClass(&guidClass, IID_INetCfgClass, (void**) &pncClass);
                if (SUCCEEDED(hr))
                {
                  INetCfgClassSetup* pncClassSetup;
                  hr = pncClass->QueryInterface(IID_INetCfgClassSetup, (void**) &pncClassSetup);
                  if (SUCCEEDED(hr))
                  {
                    // set it to OBO_USER so that szComponentId will be uninstalld
                    // On-Behalf-Of "user"
                    OBO_TOKEN OboToken;
                    ZeroMemory(&OboToken, sizeof(OboToken));
                    OboToken.Type = OBO_USER;

                    hr = pncClassSetup->DeInstall(pNetCfgComponent, &OboToken, NULL);

                    bSuccess = SUCCEEDED(hr);

                    pncClassSetup->Release();
                  }
                  pncClass->Release();
                }
              }
              pNetCfgComponent->Release();
            }
          }
        }
        pNetCfgLock->Release();
      }
      pNetCfg->Release();
    }
  }
  return bSuccess;
}
Permalink: https://adirmeier.de/Blog/ID_387
Tags: Blog, powershell, windowsvon am 2021-12-13