停止服务并备份受尊重的文件夹项并启动服务

时间:2016-02-15 14:55:06

标签: powershell service

我写了一个

的脚本
  1. 停止两个或更多服务器的不同服务(一个服务器包含两个服务),
  2. 将备受尊重的文件夹(以作业开头的文件夹名称)备份到备份文件夹
  3. 将最新文件从分段位置复制到目标文件夹
  4. 在服务器中启动不同的服务。
  5. 看起来我选择了长程序来做到这一点。请建议我是否可以修改脚本。

    Function Get-Kettle
    {
        $DestinationFolder = "D:\AppCode\Kettle"
        $BackUpFolder = "D:\AppCode\Kettle\BackUp"
        $StagingFolder = "\\server001\e$\Packages\RTO\RTO\ETL"
        $ServerList = @("server222")
        $ServicesList = @("WindowsScheduler","WindowsSchedulerLogon")
        Foreach ($Server in $ServerList)
        {
            $CheckStagingFolder = Get-ChildItem $StagingFolder
            if($CheckStagingFolder.count)
            {
                Write-Host "StagingFolder contains files.. Continue with Deployment"
                if((Test-Path "$DestinationFolder") -and (Test-Path "$BackUpFolder"))
                {
                    Write-Host "Taking BackUp"
                    Copy-Item "$DestinationFolder" -Destination "$BackUpFolder"
                    Write-Host "BackUp is completed"
                    Write-Host "Stopping the service WindowsSchedulerLogon"
                    Stop-Service $Server.WindowsSchedulerLogon -Force
                    Write-Host "WindowsSchedulerLogon service is stopped"
                    Write-Host "Stopping the service WindowsScheduler"
                    Stop-Service WindowsScheduler -Force
                    Write-Host "WindowsScheduler service is stopped"
                    Copy-Item "$StagingFolder" -Destination "$DestinationFolder" -Recurse
                    Write-Host "Starting the service WindowsSchedulerLogon"
                    Start-Service WindowsSchedulerLogon -Force
                    Write-Host "WindowsSchedulerLogon service is Started"
                    Write-Host "Starting the service WindowsScheduler"
                    Start-Service WindowsScheduler -Force
                    Write-Host "WindowsScheduler service is started"
                    Write-Host "Deployment is completed"
                }
                else
                {
                    Write-Host "No Destination and BackUp Folder..Script Exiting...!"
                    Exit;
                }
            }
            else
            {
                Write-Host "StagingFolder does not contains files.. Exiting with Deployment"
                exit;
            }
        }
    }
    Get-Kettle
    

1 个答案:

答案 0 :(得分:0)

我同意这会更好地放在Code Review上,但由于它仍然在这里开放,我认为我会建议一些改进:

  • 添加[cmdletbinding()]并将变量放在Param()块的顶部。 Cmdletbinding意味着您可以访问一组通用参数,这些参数允许您使用一系列内置功能(例如Write-Verbose,我将在后面提到)。将变量放在Param块中意味着您可以通过在函数中调用它们来更改它们,就像使用内置cmdlet一样。
  • 添加BeginProcessEnd块。此时我看不到任何需要在Begin或End中执行任何操作,但其余代码可以存在于Process中。这意味着如果您愿意,可以稍后更改此功能以支持管道输入。
  • 将所有Write-Host语句更改为Write-VerboseWrite-Host is considered harmful因为它干扰了自动化。 Write-Verbose意味着您可以在需要时但不是默认情况下查看信息消息,并将这些消息发送到单独的输出流,然后不会干扰函数的输出。要查看消息,请执行Get-Kettle -Verbose。这样做的另一个好处是,它还会打开您正在使用的标准cmdlet内置的-Verbose消息(例如Copy-ItemStart-Service)。
  • 您可以将最后两个Write-Host语句(在else块中)更改为Write-Warning条消息,然后始终显示这些语句(无论-verbose等等。当你需要再次通知用户时,不要干扰默认的输出管道。
  • 您可以删除Exit语句,因为它们没有真正实现任何目标。

您可能会考虑的其他改进包括:

所有这些都是所谓的高级功能或Cmdlet的功能。如果您还没有阅读,请查看Learn PowerShell Toolmaking in a Month of Lunches,因为它涵盖了这些主题以及更多内容。

以下是我建议实施的第一组改进代码的副本:

Function Get-Kettle
{
    [cmdletbinding()]
    Param(
        $DestinationFolder = "D:\AppCode\Kettle",
        $BackUpFolder = "D:\AppCode\Kettle\BackUp",
        $StagingFolder = "\\server001\e$\Packages\RTO\RTO\ETL",
        $ServerList = @("server222"),
        $ServicesList = @("WindowsScheduler","WindowsSchedulerLogon")
    )
    Begin {}
    Process {
        Foreach ($Server in $ServerList)
        {
            $CheckStagingFolder = Get-ChildItem $StagingFolder
            if($CheckStagingFolder.count)
            {
                Write-Verbose "StagingFolder contains files.. Continue with Deployment"
                if((Test-Path "$DestinationFolder") -and (Test-Path "$BackUpFolder"))
                {
                    Write-Verbose "Taking BackUp"
                    Copy-Item "$DestinationFolder" -Destination "$BackUpFolder"
                    Write-Verbose "BackUp is completed"
                    Write-Verbose "Stopping the service WindowsSchedulerLogon"
                    Stop-Service $Server.WindowsSchedulerLogon -Force
                    Write-Verbose "WindowsSchedulerLogon service is stopped"
                    Write-Verbose "Stopping the service WindowsScheduler"
                    Stop-Service WindowsScheduler -Force
                    Write-Verbose "WindowsScheduler service is stopped"
                    Copy-Item "$StagingFolder" -Destination "$DestinationFolder" -Recurse
                    Write-Verbose "Starting the service WindowsSchedulerLogon"
                    Start-Service WindowsSchedulerLogon -Force
                    Write-Verbose "WindowsSchedulerLogon service is Started"
                    Write-Verbose "Starting the service WindowsScheduler"
                    Start-Service WindowsScheduler -Force
                    Write-Verbose "WindowsScheduler service is started"
                    Write-Verbose "Deployment is completed"
                }
                else
                {
                    Write-Warning "No Destination and BackUp Folder..Script Exiting...!"
                }
            }
            else
            {
                Write-Warning "StagingFolder does not contains files.. Exiting with Deployment"
            }
        }
    }
    End {}
}

Get-Kettle -Verbose