将脚本文件移动到另一个位置后,PowerShell脚本不起作用?

时间:2019-02-20 17:59:00

标签: powershell

每当将脚本文件从一个位置移动到另一位置时,我都很难弄清为什么我的脚本不起作用。

是不是我使用绝对路径而不是相对路径?

下面的代码有效,但是每当我将文件移动到其他位置并更改路径位置($ServerPath$ServicesPath)时,它都不会完全执行脚本。

#stop and start Services for Windows Patching
[CmdletBinding()]
Param (
    [Parameter(Mandatory=$true)][string] $ComputerName,
    [Parameter(Mandatory=$true)][string] $Action
)

Write-Host "" -ForegroundColor Cyan

try {
    if ($ComputerName -eq "archive" -and $Action -eq "start") {
        $ServerPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\sbxnames.start.otcs.txt"
        $ServicesPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\sbxservice.start.otcs.txt"
    } elseif ($ComputerName -eq "archive" -and $Action -eq "stop") {
        $ServerPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\sbxnames.stop.otcs.txt"
        $ServicesPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\sbxservice.stop.otcs.txt"
    } elseif ($ComputerName -eq "admin" -and $Action -eq "start") {
        $ServerPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\adminservers.start.otcs.txt"
        $ServicesPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\adminnames.start.otcs.txt"
    } elseif ($ComputerName -eq "admin" -and $Action -eq "stop") {
        $ServerPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\adminservers.stop.otcs.txt"
        $ServicesPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\adminnames.stop.otcs.txt"
    } elseif ($ComputerName -eq "front end" -and $Action -eq "start") {
        $ServerPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\frontendservers.start.otcs.txt"
        $ServicesPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\frontendnames.start.otcs.txt"
    } elseif ($ComputerName -eq "front end" -and $Action -eq "stop") {
        $ServerPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\frontendservers.stop.otcs.txt"
        $ServicesPath = "C:\temp\OTCS Non-Prod\CSIRT Final\otcs resources\frontendnames.stop.otcs.txt"
    }

    $ComputerName = Get-Content -Path $ServerPath

    if ($ComputerName -ne $null) {
        $ServiceNames = Get-Content -Path $ServicesPath
        foreach ($c in $ComputerName) {
            foreach ($s in $ServiceNames) {
                $sn = Get-Service -Name $s -ComputerName $c -ErrorAction SilentlyContinue

                if ($Action -eq "start") {
                    if ($sn.Status -eq "Running") {
                        Write-Host $s "is already running on $c" -ForegroundColor Green
                        Write-Host "---------------------"
                    } elseif ($sn.Status -eq "stopped") {
                        Write-Host "Starting $s on $c..." -ForegroundColor Green
                        Start-Service $sn -ErrorAction SilentlyContinue
                        $sn.WaitForStatus("Running")
                        Start-Sleep -s 3
                        $sn.Refresh()
                        Write-Output $sn
                        Write-Host "---------------------"
                    } else {
                        Write-Host $sn.Status
                        Write-Host "---------------------"
                    }
                } elseif ($Action -eq "stop") {
                    if ($sn.Status -eq "Running") {
                        Write-Host "Stopping $s on $c..." -ForegroundColor Green
                        Stop-Service $sn -ErrorAction SilentlyContinue
                        $sn.WaitForStatus("Stopped")
                        Start-Sleep -s 3
                        $sn.Refresh()
                        Write-Output $sn
                        Write-Host "---------------------"
                    } elseif ($sn.Status -eq "stopped") {
                        Write-Host $s "is already stopped on $c"  -ForegroundColor Green
                        Write-Host "---------------------"
                    } else {
                        Write-Host $sn.Status
                        Write-Host "---------------------"
                    }
                } else {
                    Write-Host "Invalid Action.  Please type either START or STOP." -ForegroundColor Red
                }
            }
        }
    } else {
        Write-Host "Invalid Server Name" -ForegroundColor Red
    }
} catch {
    Write-Host $_
}
Read-Host -Prompt "Press any key to exit..."

脚本运行时,将显示此内容

PS C:\temp\OTCS Non-Prod\CSIRT Final\Windows Patching> .\Maintain- 
OTCSServices.ps1
cmdlet Maintain-OTCSServices.ps1 at command pipeline position 1
Supply values for the following parameters:
ComputerName: archive
Action: stop

Get-Service : Cannot find any service with service name 'Ascent Capture 
Service'.
At C:\temp\OTCS Non-Prod\CSIRT Final\Windows Patching\Maintain- 
OTCSServices.ps1:49 char:23
+                 $sn = Get-Service -Name $s -ComputerName $c <#-ErrorA ...
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Ascent Capture Service:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand

---------------------

Press any key to exit...: 

0 个答案:

没有答案