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

[Powershell]发布基于.NET Framework的WebAPI和Job控制台程序项目的更多相关文章

  1. [Powershell]使用Msbuild构建基于.NET Framework的WebAPI项目

    查找最高版本的MsBuildTools. 清理缓存垃圾. 还原NuGet包. 构建解决方案. 按项目发布程序到本地. 按项目ZIP打包. <# .NOTES ================== ...

  2. 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目

    原文: 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目 本主题介绍如何在以下产品中使用 一键式发布 发布(部署)Web 应用程序项目: Visual Studio ...

  3. 基于 SailingEase WinForm Framework 开发优秀的客户端应用程序(1:概述)

    本系统文章将详细阐述客户端应用程序的设计理念,实现方法. 本系列文章以  SailingEase WinForm Framework 为基础进行设计并实现,但其中的设计理念及方法,亦适用于任何类型的客 ...

  4. Apworks框架实战(六):使用基于Entity Framework的仓储基础结构

    在前面的章节中,我们已经设计了一个简单的领域模型,接下来我们希望能够实现领域模型的持久化及查询.在Apworks中,实现了面向Entity Framework.NHibernate以及MongoDB的 ...

  5. 如何使用 Docker 部署一个基于 Play Framework 的 Scala Web 应用?

    本文作者 Jacek Laskowski 拥有近20年的应用程序开发经验,现 CodiLime 的软件开发团队 Leader,曾从 IBM 取得多种资格认证.在这篇博文中,Jacek 分享了 Wars ...

  6. 将基于 .NET Framework 的 WPF 项目迁移到基于 .NET Core 3

    在 Connect(); 2018 大会上,微软发布了 .NET Core 3 Preview,以及基于 .NET Core 3 的 WPF:同时还发布了 Visual Studio 2019 预览版 ...

  7. C#事件(Event): 发布符合 .NET Framework Guidelines 的事件

    本文翻译整理自:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-publish-event ...

  8. CefSharp基于.Net Framework 4.0 框架编译

    CefSharp基于.Net Framework 4.0 框架编译 本次源码使用的是Github上CefSharp官方的79版本源码 准备 IDE Visual Studio 2017 Enterpr ...

  9. 基于 SailingEase WinForm Framework 开发优秀的客户端应用程序(目录)

    本系统文章将详细阐述客户端应用程序的设计理念,实现方法. 本系列文章以  SailingEase WinForm Framework 为基础进行设计并实现,但其中的设计理念及方法,亦适用于任何类型的客 ...

随机推荐

  1. 改变src图片不更新

    var url = response + "?r" + Math.random();[完美解决修改src不更新图片]$('#loginimage').attr("src& ...

  2. 百度站长平台HTTPS认证所遇到的坑

    坑1: 百度站长平台https认证失败,提示:请确保您网站的所有链接均支持https访问,且未使用不安全协议(如:SSL2.SSL3等协议). 解决办法: 1.  友情链接检查, 要检查所有的友情链接 ...

  3. mvc 返回json格式时间格式化

    protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEnco ...

  4. 怎么进入bios设置界面,电脑如何进入BIOS进行设置,怎么进入BIOS的方法集合

    怎么进入bios设置界面,电脑如何进入BIOS进行设置,怎么进入BIOS的方法集合 开机出现电脑商家图标时,按住F10键进入BIOS界面.进入BIOS界面一般都是开机后按<del,Esc,F1, ...

  5. SQLMAP 速查手册

    /pentest/database/sqlmap/txt/ common-columns.txt 字段字典 common-outputs.txt common-tables.txt 表字典 keywo ...

  6. 互联网渗透测试之Wireshark的高级应用

    互联网渗透测试之Wireshark的高级应用 1.1说明 在本节将介绍Wireshark的一些高级特性 1.2. "Follow TCP Stream" 如果你处理TCP协议,想要 ...

  7. 5-剑指offer: 和为S的两个数字

    题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. 代码 ...

  8. jq中

    1.jquery位置信息 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  9. vue-cli 3 和 vue-cli 2的区别

    分享文章: 浅谈vue-cli 3 和 vue-cli 2的区别 https://blog.csdn.net/weixin_42080056/article/details/81631661 vue- ...

  10. Python内容

    1.Python介绍.计算机硬件.网络.变量.数据类型:列表+元组+字典+布尔值+字符串+数字+集合.格式化输出.if判断.for循环.while循环. 2.三元运算.字符编码.文件处理:r/rb(读 ...