Powershell不读取.txt行



我不能让PowerShell运行这个。txt文件,我做错了什么?我试着改变。txt的名称,并检查通过的脚本,一切似乎都是一样的,但我一直得到一个错误,说invalidate ">

中的"。txt
$POSName = "$PSScriptRootBex.txt"
foreach ($POS in (Get-Content $POSName)) {
$Bex = Get-Service -ComputerName $POSName | Where-Object { $_.name -eq "BexServ" }
}
If ($Bex -eq $null) {
# Service does not exist
Write-Host " doesn't exist." -ForegroundColor Red
} 
Else {
# Service does exist
Write-Host "The $($Bex.Name) service found." -ForegroundColor Green

If ($Bex.Status -eq "Running") {
# Stop Service
Set-Service -status stopped -ComputerName $POSName -name $Box.Name -ErrorAction Stop
Write-Host "The $($Bex.Name) successfully stopped."  -ForegroundColor Green 
}
else {
#service already stopped
If ($Bex.Status -eq "Stopped") {
Write-Host "The $($Bex.Name) service already Stopped." -ForegroundColor Green
}
}
}

如前所述,您在循环中使用了错误的变量。代码正在读取文本文件,是Get-Service无法处理-ComputerName参数中文件的路径。

另外,if..else的位置应该在里面

$POSName = "$PSScriptRootBex.txt"
foreach ($POS in (Get-Content $POSName)) {
$Bex = Get-Service -ComputerName $POS | Where-Object { $_.name -eq "BexServ" }
If (!$Bex) {
# Service does not exist
Write-Host " doesn't exist." -ForegroundColor Red
} 
Else {
# Service does exist
Write-Host "The $($Bex.Name) service found." -ForegroundColor Green

If ($Bex.Status -eq "Running") {
# Stop Service
Set-Service -status stopped -ComputerName $POSName -name $Box.Name -ErrorAction Stop
Write-Host "The $($Bex.Name) successfully stopped."  -ForegroundColor Green 
}
else {
#service already stopped
If ($Bex.Status -eq "Stopped") {
Write-Host "The $($Bex.Name) service already Stopped." -ForegroundColor Green
}
}
}
}

Write-Host行中输出计算机名($POS)也是一个好主意

相关内容

  • 没有找到相关文章

最新更新