PowerShell v2脚本需要一些帮助

我有一个powershellv2脚本启动服务,但我不能让它正常工作我知道start-service命令只能工作本地化,但我似乎无法得到(gwmi win32_service-computer $ comp -Filter“name =' $ serviceName'“)。StartService()命令工作,我得到以下错误

您无法在空值expression式上调用方法。 在行: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

我已经下面的脚本,希望有人可以帮助

$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' 

我认为这是你的密码到一个SecureString这样做的转换。 我可以通过将第2行更改为:

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

默认情况下, ConvertTo-SecureString将encryptionstring作为input。 -AsPlainText告诉它使用纯文本string。

然后有一个想法,你在一个文本文件中存储密码….

 $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' 

脚本更新,我得到它的工作,但现在我需要它login到多个服务器,并启动服务。

我知道这个问题现在是6岁,但无论如何。

看看错误信息。 问题是BTSSvc $ BizTalkServerApplication中的$和所有其他服务名称都带有$。 它必须像这样逃脱:

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