当我出于某种原因运行以下脚本时,$Service 是否为空?
我知道该服务存在,但 PowerGUI 脚本编辑器的变量部分中没有出现任何内容。这弄乱了脚本中的 IF-ELSE 语句,如果服务启动,它会向最终用户显示“已启动”。
它是服务名称中的空格吗?如果我只是自己执行服务变量,那么它会显示服务的全部内容,所以它不像找不到服务..
这是 NULL 变量为空的图片:
脚本:
clear
#$ErrorActionPreference = "silentlycontinue"
Import-Module PSRemoteRegistry
$Logfile = "C:\temp\netbackup_version.log"
Function LogWrite
{
param([string]$logstring)
Add-Content $Logfile -Value $logstring
}
$computer = Get-Content -Path c:\temp\netbackup_servers1.txt
foreach ($computer1 in $computer){
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer1)
$RegKey= $Reg.OpenSubKey("SOFTWARE\\Veritas\\NetBackup\\CurrentVersion")
$NetbackupVersion1 = $RegKey.GetValue("PackageVersion")
if (test-connection $computer1 -quiet)
{
$Service = Get-WmiObject Win32_Service -Filter "Name = 'NetBackup Client Service'" -ComputerName $computer1
if($Service.state -eq 'Running')
{
LogWrite "$computer1 STARTED $NetbackupVersion1"
}
else
{
LogWrite "$computer1 STOPPED $NetbackupVersion1"
}
}
else
{
LogWrite "$computer1 is down" -foregroundcolor RED
}
}