[Powershell]发布基于.NET Framework的WebAPI和Job控制台程序项目
- 获取要发布的定时计划任务。
- 禁用和停止定时计划任务。
- 批量强制结束Job进程。
- 打印定时计划任务状态。
- 备份项目文件夹。
- 发布项目文件夹。
- 删除部署包。
- 启用定时计划任务。
<#
.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控制台程序项目的更多相关文章
- [Powershell]使用Msbuild构建基于.NET Framework的WebAPI项目
查找最高版本的MsBuildTools. 清理缓存垃圾. 还原NuGet包. 构建解决方案. 按项目发布程序到本地. 按项目ZIP打包. <# .NOTES ================== ...
- 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目
原文: 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目 本主题介绍如何在以下产品中使用 一键式发布 发布(部署)Web 应用程序项目: Visual Studio ...
- 基于 SailingEase WinForm Framework 开发优秀的客户端应用程序(1:概述)
本系统文章将详细阐述客户端应用程序的设计理念,实现方法. 本系列文章以 SailingEase WinForm Framework 为基础进行设计并实现,但其中的设计理念及方法,亦适用于任何类型的客 ...
- Apworks框架实战(六):使用基于Entity Framework的仓储基础结构
在前面的章节中,我们已经设计了一个简单的领域模型,接下来我们希望能够实现领域模型的持久化及查询.在Apworks中,实现了面向Entity Framework.NHibernate以及MongoDB的 ...
- 如何使用 Docker 部署一个基于 Play Framework 的 Scala Web 应用?
本文作者 Jacek Laskowski 拥有近20年的应用程序开发经验,现 CodiLime 的软件开发团队 Leader,曾从 IBM 取得多种资格认证.在这篇博文中,Jacek 分享了 Wars ...
- 将基于 .NET Framework 的 WPF 项目迁移到基于 .NET Core 3
在 Connect(); 2018 大会上,微软发布了 .NET Core 3 Preview,以及基于 .NET Core 3 的 WPF:同时还发布了 Visual Studio 2019 预览版 ...
- C#事件(Event): 发布符合 .NET Framework Guidelines 的事件
本文翻译整理自:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-publish-event ...
- CefSharp基于.Net Framework 4.0 框架编译
CefSharp基于.Net Framework 4.0 框架编译 本次源码使用的是Github上CefSharp官方的79版本源码 准备 IDE Visual Studio 2017 Enterpr ...
- 基于 SailingEase WinForm Framework 开发优秀的客户端应用程序(目录)
本系统文章将详细阐述客户端应用程序的设计理念,实现方法. 本系列文章以 SailingEase WinForm Framework 为基础进行设计并实现,但其中的设计理念及方法,亦适用于任何类型的客 ...
随机推荐
- ocelot性能测试
网上搜索发现多篇文章指出ocelot的性能有问题,可是在ocelot项目issue提问中,维护者指出,ocelot的性能问题不大.瓶颈在于.net的httpclient. 我参考文章 https:// ...
- 同时读取两个文件进行while循环
知识点:文件对象提供了三个“读”方法: .read()..readline() 和 .readlines().每种方法可以接受一个变量以限制每次读取的数据量,但它们通常不使用变量. 问题描述: 我们的 ...
- 数据解析模块BeautifulSoup简单使用
一.准备环境: 1.准备测试页面test.html <html> <head> <title> The Dormouse's story </title> ...
- Java自学-集合框架 HashSet
Java集合框架 HashSet 示例 1 : 元素不能重复 Set中的元素,不能重复 package collection; import java.util.HashSet; public cla ...
- 将多个sass文件合并到一个文件中
将多个sass文件合并到一个文件中 应用场景:制作angular npm包的时候,定义的一些全局样式,自定义主题色这类的情况下,多个scss文件会要合并成一个文件并写到dist文件里,发布到仓库中. ...
- 记录几个 Android x86 系统的官网
首先是官网:https://www.android-x86.org/ 国内: 凤凰OS(Android 7.1):http://www.phoenixos.com/download_x86 技德Rem ...
- Innodb整体架构
如下图展示了Innodb内存中和磁盘的结构: 内存中结构主要有如下几种: buffer pool change buffer adaptive hash index (自适应的hash索引) Log ...
- mysql主从配置步骤
主服务器配置: 1)登陆MySQL数据库 mysql>mysql -uroot -p123 2)给从服务器设置授权用户 mysql>grant all slave on *.* to us ...
- org.apache.commons.httpclient工具类(封装的HttpUtil)
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...
- c# 第六节 c#的程序结构,以及vs的文件结构
本节内容: 1:c#的程序结构 2:深入了解vs的文件 1:c#的程序结构 实例: 2:深入了解vs的文件 三者的关系: 3:命令空间是什么 使用别名: