tags:

views:

179

answers:

1

Hello, I'm trying to use the Create method of the Win32_Service class, but when I call the InvokeMethod, I receive this exception:

System.Management.ManagementException: Invalid method 
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, InvokeMethodOptions options)
at <StartupCode$FSI_0075>.$FSI_0075.main@()

This is the code (in F# but it is understable for C# programmers too :)):

let scope = new ManagementScope(@"root\cimv2", null)
use imageService = Utility.getServiceObject scope "Win32_Service"
use inParams = imageService.GetMethodParameters("Create")
inParams.["Name"] <- name
inParams.["DisplayName"] <- displayName
inParams.["PathName"] <- pathName
inParams.["ServiceType"] <- 0x10   // Own Process
inParams.["ErrorControl"] <- 0     // User is not notified
inParams.["StartMode"] <- "Automatic"
inParams.["DesktopInteract"] <- false
inParams.["StartName"] <- "LocalSystem"
inParams.["StartPassword"] <- ""
inParams.["ServiceDependencies"] <- null

use outParams = imageService.InvokeMethod("Create", inParams, null) 

The exception is thrown when the last line is executed (I removed the next lines).

I think I'm calling correctly the method, so I don't know why the exception is thrown. Can anyone help me?

Thanks in advance, Marco

A: 

I resolved :) I just need to create the imageService object as this:

    let imageService = new ManagementClass(scope, new ManagementPath("Win32_Service"), null)

Thanks anyway :) Marco

Marco