Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leftp/f37b777d96428ddaeab7cba9d14e4b03 to your computer and use it in GitHub Desktop.
Save leftp/f37b777d96428ddaeab7cba9d14e4b03 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Management;
using System.Management.Instrumentation;
using System.Runtime.InteropServices;
using System.Configuration.Install;
/*
* Added references:
* system.configuration.install
* system.enterpriseservices
* system.management
* system.management.instrumentation
*
* "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /target:library /R:system.configuration.install.dll /R:system.enterpriseservices.dll /R:system.management.dll /R:system.management.instrumentation.dll wmitest.cs
*
* Install:
* InstallUtil.exe WMITest.dll
* C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe wmitest.dll
* [System.Configuration.Install.ManagedInstallerClass]::InstallHelper(@( ".\WMITest.dll"))
*
* Need to copy file to gac or wbem folder
* copy wmitest.dll c:\windows\system32\wbem\
*
* Run command
* Invoke-WmiMethod -Class Win32_Template -Name RunSomething
*/
[assembly: WmiConfiguration(@"root\cimv2", HostingModel = ManagementHostingModel.LocalSystem)]
namespace WMITest
{
[System.ComponentModel.RunInstaller(true)]
public class MyInstall : DefaultManagementInstaller
{
//private static string fileName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
public override void Install(IDictionary stateSaver)
{
try
{
base.Install(stateSaver);
RegistrationServices registrationServices = new RegistrationServices();
}
catch { }
}
public override void Uninstall(IDictionary savedState)
{
try
{
ManagementClass managementClass = new ManagementClass(@"root\cimv2:Win32_Template");
managementClass.Delete();
}
catch { }
try
{
base.Uninstall(savedState);
}
catch { }
}
}
[ManagementEntity(Name = "Win32_Template")]
public class Class1
{
[ManagementTask]
public static string RunSomething(string command, string parameters)
{
return "test";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment