PowerShell服务在循环中找不到SQL服务,但在get-service下运行时服务存在

zed5wv10  于 2023-06-23  发布在  Shell
关注(0)|答案(1)|浏览(134)

我们在供应商工具中跟踪SQL服务,我们不再可以访问这些服务。所以我正在尝试PowerShell,我是新来的。当我运行这个命令时,SQL服务出错,说Get-Service:找不到服务名为“MSSQLSERVER”且“SQLSERVERAGENT”的任何服务。我并不是真的需要VSS和Spooler,但添加它们是为了确保它能正常工作。其他服务似乎都很好。当我运行Get-Service -computername 'server 1'时,SQL服务在服务列表中显示其状态。相同的powershell窗口/用户/权限。我也试过以管理员的身份运行,但没有成功。SQL服务是否以不同的方式运行?循环似乎可以与其他服务一起工作,只是不能与SQL一起工作。

$servers = 'server1','server2','server3','server4', 'server5', 'server6'
$path = 'c:\temp\DatabaseServicesStatusReport.html' 

$header = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>System Status Report</title>
<style type="text/css">
<!--
body {
background-color: #E0E0E0;
font-family: sans-serif
}
table, th, td {
background-color: white;
border-collapse:collapse;
border: 1px solid black;
padding: 5px
}
-->
</style>
"@

$body = @"
<h1>SQL Services Status</h1>
<p>As of: $(get-date).</p>
"@

$results = foreach ($server in $servers) 
{ 
    [PSCustomObject]@{
        ServerName = $server
        VSS = (Get-Service -name vss).Status
        Spooler = (Get-Service -name spooler).Status
        MSSQLSERVER = (Get-Service -name MSSQLSERVER).Status
        SQLSERVERAGENT = (Get-Service -name SQLSERVERAGENT).Status
    }
} 

$results | ConvertTo-Html -head $header -body $body | foreach {
    $PSItem -replace "<td>Stopped</td>", "<td style='background-color:#FF8080'>Stopped</td>"
} | Out-File C:\temp\DatabaseServicesStatusReport.html

Invoke-Expression c:\temp\DatabaseServicesStatusReport.html
toe95027

toe950271#

您需要运行相同的脚本,但添加**-ComputerName**参数以检入指定的服务器:

$results = foreach ($server in $servers) 
{ 
    

    [PSCustomObject]@{
        ServerName = $server
        VSS = (Get-Service -ComputerName $server -name vss).Status
        Spooler = (Get-Service -ComputerName $server -name spooler).Status
        MSSQLSERVER = (Get-Service -ComputerName $server -name MSSQLSERVER).Status
        SQLSERVERAGENT = (Get-Service -ComputerName $server -name SQLSERVERAGENT).Status
    }
}

再看一个类似的问题:How to check if a particular service is running in a remote computer using PowerShell

相关问题