2

I want to use PowerShell 2.0 to install a Windows service after detecting if the service exists. I have the part to detect the service working but can't get the install to work.

$mc = [wmiclass]"\\"+"$ServiceServer\ROOT\CIMV2:Win32_Service"

Running this line produces this error:

Method invocation failed because [System.Management.ManagementClass] doesn't contain a method named 'op_Addition'.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Kenoyer130
  • 6,874
  • 9
  • 51
  • 73

1 Answers1

4

Wrap all of the string in parentheses:

$mc = [wmiclass]("\\"+"$ServiceServer\ROOT\CIMV2:Win32_Service")

The problem is that [wmiclass] is casting just the first string "\\" to [System.Management.ManagementClass] which then is trying to add itself to a string.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Mark
  • 580
  • 3
  • 11