微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过 Powershell 远程更新 Windows 服务器

如何解决通过 Powershell 远程更新 Windows 服务器

您好,我已经尝试让这个脚本工作一段时间了,但我一直遇到问题。该脚本是针对多台服务器编写的,虽然我目前只是在一个测试系统上对其进行测试。

接下来我会发布脚本。

#List of Servers
$sqlServer = @('testserver.net')
$ApplicationServer = @('testserver.net')
$Servers = @($sqlServer,$ApplicationServer)
$ServiceName = @('tomcat9','MSsqlSERVER')

#information Message
Write-Host "Starting update process for the following Servers:" $Servers "and Services:" $ServiceName

#Gets status of Servers
ForEach ($Index in $Servers)
{
    Invoke-command {Get-Service tomcat9} -comp $Servers[$Index]
    Invoke-command {Get-Service -Name 'MSsqlSERVER'} -comp $Servers[$Index]
}

#information Message
Write-Host "Stopping the specified Services:" $ServiceName

#Stops services
#Sequentially stops TomCat and then DB Applications
ForEach ($Index in $Servers)
{
    $ArrayService = Invoke-Command {Get-Service tomcat9} -comp $Servers[$Index]
    if ($ArrayService.Status -ne "Stopped")
    {
    Invoke-Command {Stop-Service tomcat9} -comp $Servers[$Index]
    }

    $ArrayService = Invoke-Command {Get-Service -Name 'MSsqlSERVER'} -comp $Servers[$Index]
    if ($ArrayService.Status -ne "Stopped")
    {
    Invoke-Command {Stop-Service -Name 'MSsqlSERVER'} -comp $Servers[$Index]
    }
}

#information Message
Write-Host "Will Now start Windows Updates on servers and reboot"

#Will Update Windows,automatically reboot and out the log in the specified directory. This will be done to all listed Servers
ForEach ($Index in $Servers)
{
    $Scriptblock = {
        Invoke-WUJob -Comp $Servers[$Index] -RunNow -Confirm:$false -Verbose -Script {
            Install-WindowsUpdate -MicrosoftUpdate -NotCategory "SilverLight" -Nottitle "Preview" -AcceptAll -AutoReboot | Out-File C:\PSWindowsUpdate.log
            }
        }
    Invoke-Command -Comp $Servers[$Index] -ScriptBlock $Scriptblock
}

#information Message
Write-Host "Starting up Services again"

#Starting sql Server and checking if its running,if not running it will give more time before the Script continues
ForEach ($Index in $sqlServer)
{
    $ArrayService = Invoke-Command {Get-Service -Name $ServiceName} -comp $sqlServer[$Index]

    $FilteredService = $ArrayService | where {$_ -like 'Msql'}
    while ($FilteredService.Status -ne 'Running')
    {
        Invoke-Command {Start-Service $FilteredService} -comp $sqlServer[$Index]
        Write-Host $FilteredService.Status
        Write-Host "sql Service starting on " $sqlServer[$Index] 
        function Check-Status 
        {
            Start-Sleep -seconds 30
            $FilteredService.Refresh()
            if ($FilteredService.Status -eq 'Running')
            {
                Write-Host "sql Service running"
                #Invoke-Command {Start-Service $ArrayService | where {$_ -like 'tomcat9'}} -comp $ApplicationServer
            }
            else
            {
                Check-Status
            }
        }
    }
}

#Starting TomCat Service again
ForEach ($Index in $ApplicationServer)
{
    Write-Host "MUMService starting on " $ApplicationServer[$Index]
    #Invoke-Command {Start-Service $ArrayService | where {$_ -like 'tomcat9'}} -comp $ApplicationServer[$Index]
    $ServiceStatus = Invoke-Command {Get-Service tomcat9} -comp $ApplicationServer[$Index]
    if ($ServiceStatus.Status -eq 'Running')
    {
        Invoke-Command {Start-Service $ArrayService | where {$_ -like 'tomcat9'}} -comp $ApplicationServer[$Index]
        Write-Host "MUM Service running"
    }
    else 
    {
        Invoke-Command {Start-Service $ArrayService | where {$_ -like 'tomcat9'}} -comp $ApplicationServer[$Index]
    }
}


ForEach ($Index in $Servers)
{
    invoke-command {Get-Service mum_software_tomcat9} -comp $Servers[$Index]
    invoke-command {Get-Service -Name 'MSsqlSERVER'} -comp $Servers[$Index]
}

要么我的逻辑真的错误,要么这在 powershell 中不起作用,但是当我尝试按原样执行脚本时,参数“ComputerName”出现错误。它表示参数为 null 或为空。提供一个非 null 或空的参数,然后重试该命令。显然它不喜欢 $sqlServer[$Index]。

虽然更新功能本身似乎可以独立工作。

这有什么问题吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。