I have a powershellv2 script for starting services but I can't get it to work propparly I know the start-service command only works localy but I can't seem to get the (gwmi win32_service -computer $comp -Filter "name='$serviceName'").StartService() command to work, I get the following error
You cannot call a method on a null-valued expression. At line:1 char:146 + (Get-WmiObject win32_service -computer cap-test4-biz1 -Credential captest\jola_adm -filter "Name='BTSSvc$BizTalkServerApplication'").invokemethod <<<< ("StartService",$null) + CategoryInfo : InvalidOperation: (invokemethod:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
i have pastet the script below, hopeing some one can help
$computername = "remoteserver"
$password = type H:\Powershell\MyPassword.txt | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist “domain\user”,$password
function StartIfStopped([string]$ServiceName) {
$Service = Get-WmiObject Win32_Service -computername $computername -cred $cred -Filter "name='$ServiceName'"
if (!$Service) {
# Service is not installed
Write-Host "$ServiceName is not installed on this machine."
} else {
# Service is installed
if ($Service.State -eq "Stopped") {
if ($Service.StartMode -eq "Disabled") {
Write-Host "Cannot start service $ServiceName as it is disabled."
} else {
Write-Host "Starting service $ServiceName ..."
Start-Service $ServiceName
}
} else {
Write-Host "$ServiceName is already running."
}
}
}
StartIfStopped 'BTSSvc$BizTalkServerApplication'
StartIfStopped 'BTSSvc$TASMSMQHost'
StartIfStopped 'BTSSvc$TASProcessingHost'
StartIfStopped 'BTSSvc$TASSendHost'
StartIfStopped 'BTSSvc$TASTrackingHost'