0

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' 

3 Answers 3

1

I think it's the conversion of your password to a SecureString that's doing it. I was able to get your script to work by changing line 2 to this:

$password = type H:\Powershell\MyPassword.txt | ConvertTo-SecureString -AsPlainText -Force 

By default ConvertTo-SecureString takes an encrypted string as its input. -AsPlainText tells it to use a plain text string.

Then there's the idea that you're storing a password in a text file....

1
  • its not that.. i doodled around with the remote log-on and got this. (Get-WmiObject Win32_Service -filter "name='$ServiceName'" -computername $computername -cred $cred ).StartService() Commented Aug 30, 2010 at 6:40
0
$computername = Get-Content H:\computers\Computers.txt; $password = type H:\Powershell\MyPassword.txt | ConvertTo-SecureString $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist “domain\user”,$password $filename = "{0:yyyy-MM-dd} start" -f (Get-Date) $tid = Get-Date -f HH:mm:ss 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 $computername." } 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 ..." Write-Output "$tid Starting $ServiceName ." >> H:\logfiler\$filename.txt #virker kun lokalt på servere #Start-Service $ServiceName #virker på remote servere også i andre domainer (Get-WmiObject Win32_Service -filter "name='$ServiceName'" -computername $computername -cred $cred ).StartService() } } else { #Write-Host "$ServiceName is already running." Write-Output "$tid already running $ServiceName." >> H:\logfiler\$filename.txt } } } StartIfStopped 'BTSSvc$BizTalkServerApplication' StartIfStopped 'BTSSvc$TASMSMQHost' StartIfStopped 'BTSSvc$TASProcessingHost' StartIfStopped 'BTSSvc$TASSendHost' StartIfStopped 'BTSSvc$TASTrackingHost' StartIfStopped 'BRNTService' StartIfStopped 'TimerService' StartIfStopped 'BouncedMailTracker' StartIfStopped 'CMToolService' StartIfStopped 'CalculationExcelService' StartIfStopped 'ControlNTService' StartIfStopped 'EnvelopeService' StartIfStopped 'PrinterService' 

Update on the script, i got it working but now i need it to log-on multiple servers and start the services.

0

I know this question is 6 years old by now but anyway.

Looking at the error message. The problem is the $ in BTSSvc$BizTalkServerApplication and all the other service names with a $. It has to be escaped like so:

Get-WmiObject -ClassName Win32_Service -Filter "Name='BTSSvc`$BizTalkServerApplication'" 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.