Powershell v2 脚本需要一些帮助

Powershell v2 脚本需要一些帮助

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

您无法对空值表达式调用方法。第 1 行,字符: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'

答案1

我认为这是将密码转换为 SecureString 造成的。我通过将第 2 行更改为以下内容,使您的脚本正常工作:

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

默认情况下,ConvertTo-SecureString以加密字符串作为输入。 -AsPlainText告诉它使用纯文本字符串。

然后你就可以将密码存储在文本文件中......

答案2

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

更新脚本,我让它运行了,但现在我需要它登录多个服务器并启动服务。

答案3

我知道这个问题已经是6年前的问题了,但无论如何。

查看错误消息。问题在于 BTSSvc$BizTalkServerApplication 中的 $ 以及所有其他带有 $ 的服务名称。必须像这样进行转义:

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

相关内容