我写了一个
的脚本看起来我选择了长程序来做到这一点。请建议我是否可以修改脚本。
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
答案 0 :(得分:0)
我同意这会更好地放在Code Review上,但由于它仍然在这里开放,我认为我会建议一些改进:
[cmdletbinding()]
并将变量放在Param()
块的顶部。 Cmdletbinding意味着您可以访问一组通用参数,这些参数允许您使用一系列内置功能(例如Write-Verbose
,我将在后面提到)。将变量放在Param块中意味着您可以通过在函数中调用它们来更改它们,就像使用内置cmdlet一样。Begin
,Process
和End
块。此时我看不到任何需要在Begin或End中执行任何操作,但其余代码可以存在于Process中。这意味着如果您愿意,可以稍后更改此功能以支持管道输入。Write-Host
语句更改为Write-Verbose
。 Write-Host is considered harmful因为它干扰了自动化。 Write-Verbose意味着您可以在需要时但不是默认情况下查看信息消息,并将这些消息发送到单独的输出流,然后不会干扰函数的输出。要查看消息,请执行Get-Kettle -Verbose
。这样做的另一个好处是,它还会打开您正在使用的标准cmdlet内置的-Verbose
消息(例如Copy-Item
或Start-Service
)。Write-Host
语句(在else
块中)更改为Write-Warning
条消息,然后始终显示这些语句(无论-verbose
等等。当你需要再次通知用户时,不要干扰默认的输出管道。Exit
语句,因为它们没有真正实现任何目标。您可能会考虑的其他改进包括:
[string]
等)。 -confirm
and -whatif
通过添加supportsshouldprocess并将这些内容放在脚本中进行更改的部分。所有这些都是所谓的高级功能或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