[Powershell]发布基于.NET Framework的WebAPI和Job控制台程序项目

  1. 获取要发布的定时计划任务。
  2. 禁用和停止定时计划任务。
  3. 批量强制结束Job进程。
  4. 打印定时计划任务状态。
  5. 备份项目文件夹。
  6. 发布项目文件夹。
  7. 删除部署包。
  8. 启用定时计划任务。
<#	
	.NOTES
	===========================================================================
	 Created with: 	Visual Studio 2019
	 Created on:   	2019/8/21 18:17
	 Created by:   	Allen.Cai
	 Organization: 	Allen.Cai
	 Filename:     	deploy_full.ps1
	===========================================================================
	.DESCRIPTION
		ERP full applications devops scripts.
#>

# 声明全局变量
# 定义方法开始时输出的前景色
$LogForegroundColorStart = "Green"
# 定义方法结束时输出的前景色
$LogForegroundColorEnd = "Cyan"
# 定义方法输出警告时的前景色
$LogForegroundColorWarning = "DarkYellow"
# 定义发布目标根路径
$ProjectRoot = "D:\project"
# 定义备份文件夹根路径
$BackDirectoryRoot = "D:\backup"
# 定义发布包根路径
$DeployDirectoryRoot = "D:\updatasource"
# 定义项目文件夹相对路径
$ProjectDirectorys = "API1", "API2", "API3", "Job\ALLJob"
# 定义定时计划任务根路径
$ScheduledTaskPathRoot = "\Microsoft\ERPJob\"
# 定义定时计划任务集合
$ScheduledTasks = New-Object System.Collections.ArrayList
# 定义日志文件路径
$LogDate = Get-Date
$LogFile = "D:\backup\Deploy_$($LogDate.ToString("yyyyMMddHHmmss")).log"

Start-Transcript -Path $LogFile

# 获取要发布的定时计划任务
Function Get-ScheduledTasks
{
	Write-Host "`nGet the scheduled tasks to be published." -Foreground $LogForegroundColorStart
	Get-ScheduledTask -TaskPath $ScheduledTaskPathRoot | ForEach-Object {
		$taskFullPath = $_.TaskPath + $_.TaskName
		Write-Host $ScheduledTasks.Add($taskFullPath) $taskFullPath
	}
	
	# 屏幕输出 要执行 禁用、替换、启用 流程的定时计划任务
	Write-Host "`nFind $($ScheduledTasks.Count) scheduled tasks :"
	Write-Output $ScheduledTasks
	# 输出到文件,记录下待执行的定时计划任务
	$ScheduledTasks | Out-File "d:\task.list"
	Write-Host "Get the scheduled tasks end." -Foreground $LogForegroundColorEnd
}
Get-ScheduledTasks

# 禁用和停止定时计划任务
Function Stop-ScheduledTasks
{
	Write-Host "`nDisable and Stop scheduled tasks begin." -Foreground $LogForegroundColorStart
	foreach ($taskFullPath in $ScheduledTasks)
	{
		Write-Host "Disabling and stopping the $taskFullPath scheduled task..."
		# 禁用
		Disable-ScheduledTask "$($taskFullPath)"
		Start-Sleep -m 1000
		
		# 停止
		Stop-ScheduledTask "$($taskFullPath)"
		Start-Sleep -s 3
		Write-Host "Disabled and stopped the $taskFullPath scheduled task."
	}
	Write-Host "Disable and Stop scheduled tasks end." -Foreground $LogForegroundColorEnd
}
Stop-ScheduledTasks

# 批量强制杀掉Job进程,避免上面的Stop-ScheduledTask命令没结束Job进程
Function Kill-Job
{
	try
	{
		Write-Host "`nBatch force killing of the Job process." -Foreground $LogForegroundColorStart
		# 屏幕输出Job进程状态信息, 如果没有正在运行的Job进程,会停止执行后面命令并且抛出异常
		Get-Process -Name Job -ErrorAction Stop
		# 强制终止Job进程, 如果没有正在运行的Job进程,会停止执行后面命令并且抛出异常
		Stop-Process -Name Job -Force -ErrorAction Stop
		Start-Sleep -s 5
		Write-Host "Batch force killing of the Job process successed." -Foreground $LogForegroundColorEnd
	}
	catch [Microsoft.PowerShell.Commands.ProcessCommandException]
	{
		Write-Host "The Job process that needs to be forced to kill was not found." -Foreground $LogForegroundColorWarning
	}
}
Kill-Job

# 屏幕输出定时计划任务状态,看是否已全部禁用和停止
Function Print-ScheduledTasks
{
	Write-Host "`nPrinting the status of scheduled tasks." -Foreground $LogForegroundColorStart
	# 逐个输出任务状态
	#	foreach ($taskFullPath in $ScheduledTasks)
	#	{
	#		$lastIndex = $taskFullPath.LastIndexOf("\") + 1;
	#		$taskPath = $taskFullPath.SubString(0, $lastIndex)
	#		$taskName = $taskFullPath.SubString($lastIndex)
	#		
	#		Get-ScheduledTask -TaskPath "$($taskPath)" -TaskName "$($taskName)"
	#	}
	# 一次性输出所有任务状态
	Get-ScheduledTask -TaskPath $ScheduledTaskPathRoot
	Write-Host "Print the status of scheduled tasks end." -Foreground $LogForegroundColorEnd
}
Print-ScheduledTasks

# 备份项目文件夹
Function Backup-Directory
{
	Write-Host "`nBackuping project folders." -Foreground $LogForegroundColorStart
	
	# 定义备份文件夹所需要的参数
	$CurrentDate = Get-Date
	$BackupTime = $CurrentDate.ToString("yyyyMMddHHmmss")
	$DisplayTime = $CurrentDate.ToString("yyyy-MM-dd HH:mm:ss")
	$BackDirectoryCurrent = $BackDirectoryRoot + "\" + $BackupTime
	
	foreach ($item in $ProjectDirectorys)
	{
		Write-Host "Backup the $item project..."
		# 创建备份文件夹
		New-Item -ItemType "directory" -Path "$BackDirectoryCurrent\$item" -ErrorAction SilentlyContinue
		# 拷贝项目文件夹到备份文件夹
		Copy-Item "$ProjectRoot\$item\*" -Destination "$BackDirectoryCurrent\$item\" -Recurse -Force
		Write-Host "Backup $item project successfully."
	}
	
	Write-Host "$DisplayTime $ProjectRoot backup successful. details:"
	
	# 屏幕输出备份文件夹的目录路径和大小
	$BackDirectoryChildItems = Get-ChildItem $BackDirectoryCurrent | Where-Object { $_.PsIsContainer -eq $true }
	$BackDirectoryChildItems | ForEach-Object {
		$item = $_
		$subFolderItems = (Get-ChildItem $item.FullName -Recurse | Where-Object { -not $_.PSIsContainer } | Measure-Object -property length -sum)
		$item.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + "MB"
	}
	
	Write-Host "Backup project folders end." -Foreground $LogForegroundColorEnd
}
Backup-Directory

# 发布项目文件夹
Function Deploy-Projects
{
	Write-Host "`nThe $($ProjectDirectorys.Length) projects are being deployed." -Foreground $LogForegroundColorStart
	foreach ($item in $ProjectDirectorys)
	{
		Write-Host "Deploying the $item project..."
		# 先假设目标路径不存在,尝试创建新目录,如果存在,则跳过
		New-Item -ItemType "directory" -Path "$ProjectRoot\$item\" -ErrorAction SilentlyContinue
		$projectParentPath = Split-Path -Parent "$ProjectRoot\$item"
		Copy-Item "$DeployDirectoryRoot\$item" -Destination $projectParentPath -Recurse -Force
		Write-Host "Deployed $item project successfully."
	}
	Write-Host "The $($ProjectDirectorys.Length) projects were successfully deployed." -Foreground $LogForegroundColorEnd
}
Deploy-Projects

# 删除部署包
Function Remove-DeployPackages
{
	Write-Host "`n$($ProjectDirectorys.Length) packages are being removed." -Foreground $LogForegroundColorStart
	foreach ($item in $ProjectDirectorys)
	{
		Write-Host "Removing the $item package..."
		Remove-Item -Path "$DeployDirectoryRoot\$item" -Recurse -Force -ErrorAction SilentlyContinue
		Write-Host "Removed $item package successfully."
	}
	Write-Host "$($ProjectDirectorys.Length) packages were successfully removed." -Foreground $LogForegroundColorEnd
}
Remove-DeployPackages

# 启用定时计划任务
Function Enable-ScheduledTasks
{
	Write-Host "`nEnable scheduled tasks begin." -Foreground $LogForegroundColorStart
	foreach ($taskFullPath in $ScheduledTasks)
	{
		Enable-ScheduledTask "$($taskFullPath)"
	}
	Write-Host "Enable scheduled tasks end." -Foreground $LogForegroundColorEnd
}
Enable-ScheduledTasks

# 再次屏幕输出定时计划任务状态
Print-ScheduledTasks

Stop-Transcript
作者:VAllen
出处:http://www.cnblogs.com/vallen
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
唯有偏执者得以生存。
posted @ 2019-09-07 16:48  VAllen  阅读(303)  评论(0编辑  收藏  举报