I am using powershell 1.0 and I need to install a service on a remote machine and first uninstall it if it exists.

This is my script I have that installs the service, however, I seem unable to uninstall the service. I have tried installutil however the service path is a network path which installutil, throws errors over.

I'm a complete newbie with powershell, however, I'm sure there must be a better and cleaner way of approaching this.

$class = "Win32_Service"
$method = "Create"
$mc = [wmiclass]"\\DUMMYServer\ROOT\CIMV2:$class"
$inparams = $mc.PSBase.GetMethodParameters($method)
$inparams.DesktopInteract = $false
$inparams.DisplayName = "DummyService"
$inparams.ErrorControl = 0
$inparams.LoadOrderGroup = $null
$inparams.LoadOrderGroupDependencies = $null
$inparams.Name = "DummyMessageService"
$inparams.PathName = '\\DummyServer\c$\Applications\DummyMessageWindowsService\DummyWindowsService.exe'
$inparams.ServiceDependencies = $null
$inparams.ServiceType = 16
$inparams.StartMode = "Automatic"
$inparams.StartName = $null # will start as localsystem builtin if null
$inparams.StartPassword = $null

$result = $mc.PSBase.InvokeMethod($method,$inparams,$null)
$result | Format-List
link|improve this question
feedback

1 Answer

try using wmi rather than all the fancy params.... Something like this (untested)

(gwmi win32_service -filter "name='yourservicename'").delete()

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.