Pages

Tuesday, December 6, 2016

PowerShell v2 Invoke WmiMethod Create a Service

After digging around on the net for a while I did not find any real good posts telling me how to create a service. I know the Win32_Service class has a Create function but I was not sure how to get it syntactically:
Create method of the Win32_Service Class
To give myself something to play with I created a blank C# service project in Visual Studio 2010, compiled the project and got my .exe path.


NOTE: You have to have an executable with service headers or else it wont work.  Trust me, I tried.

So, my first stab at this task involved this command:
cls
[Array]$parms = Dummy,Dummy Service,G:Visual Studio2010Projects4.0DummyServiceDummyService inDebugDummyService.exe,16,2,Automatic,$false,LocalSystem
Invoke-WmiMethod -Class win32_service -name create -ArgumentList $parms
When I ran this command I got this error:
Invoke-WmiMethod : String was not recognized as a valid Boolean. At C:UserswillAppDataLocalTemp8bc972e2-a15c-41ba-bd4f-f4cfbaae9fbe.ps1:3 char:17 + Invoke-WmiMethod <<<< -Class win32_service -name create -ArgumentList $parms + CategoryInfo : NotSpecified: (:) [Invoke-WmiMethod], FormatException + FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.InvokeWmiMethod
which basically told me nothing.  It did however point me to this post:
Invoke-WmiMethod to call "create" method of win32_service not working 
As I read through the post I saw precisely what I ran into, and, it took me a while to run the first few lines to see what jrv was referring to.  The expected order of parameters for the Create method of the class is as follows (Ill use all the commands exactly as I did in my machine):
$computer = "." # this computer
$class = "Win32_Service"
$method = "Create"
$mc = [wmiclass]"$computerROOTCIMV2:$class"
$mc.PSBase.GetMethodParameters($method)

__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 12
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
DesktopInteract :
DisplayName :
ErrorControl :
LoadOrderGroup :
LoadOrderGroupDependencies :
Name :
PathName :
ServiceDependencies :
ServiceType :
StartMode :
StartName :
StartPassword :
Now that I see the correct order I was able to come up with these two lines which worked just fine:
$argumentlist = $false, "Dummy", 0, $null, $null, "Dummy Service", "G:Visual Studio2010Projects4.0DummyServiceDummyService inDebugDummyService.exe", $null, 16, "Automatic", $null, $null
Invoke-WmiMethod –Class Win32_Service –Name Create –ArgumentList $argumentlist
As a side note, if you successfully create a service that does not exist you will get this.
Invoke-WmiMethod -Class Win32_Service -Name Create -ArgumentList $argumentlist


__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
If you try to run it again but the service already exists this is what you will see:
Invoke-WmiMethod -Class Win32_Service -Name Create -ArgumentList $argumentlist


__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 23

Related Posts by Categories

0 comments:

Post a Comment