[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 为基础进行设计并实现,但其中的设计理念及方法,亦适用于任何类型的客 ...
随机推荐
- CentOS6 + MapServer7.4编译
先升级gcc 1.python3.6 ./configure --enable-shared --enable-profiling make –j 20 make install 2.proj-4.9 ...
- java中级,知识点归纳(一)
一.接口和抽象类的区别 抽象类中可以含有构造方法,而接口内不能有. 抽象类中可以有普通成员变量,而接口中不能有. 抽象类中可以包含非抽象的普通方法,而接口中所有方法必须是抽象的,不能有非抽象的普通方法 ...
- Winform中怎样跨窗体获取另一窗体的控件对象
场景 Winform中实现跨窗体获取ZedGraph的ZedGraphControl控件对象: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/de ...
- Redis命令geoXXX
1. Redis命令geoXXX 1.1. 介绍 自Redis 3.2开始,Redis基于geohash和有序集合提供了地理位置相关功能. Redis Geo模块包含了以下6个命令: GEOADD: ...
- 点击除指定区域外的空白处,隐藏div
<script> $(document).click(function (e) { var $target = $(e.target); //点击.zanpl和.quanzipl以外的地方 ...
- python3使用模块
Python内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env python3 # -*- codi ...
- Oracle 11g 手工建库
假设数据库软件已经安装好,现在没有图形界面无法用dbca安装数据库,那么用手工建库,数据库名为edw 创建目录 [oracle@localhost ~]$ mkdir -p /u01/app/orac ...
- 13.生产环境中的 redis 是怎么部署的?
作者:中华石杉 面试题 生产环境中的 redis 是怎么部署的? 面试官心理分析 看看你了解不了解你们公司的 redis 生产集群的部署架构,如果你不了解,那么确实你就很失职了,你的 redis 是主 ...
- MariaDB设置主从复制
主从复制包含两个步骤: 在 master 主服务器(组)上的设置,以及在 slave 从属服务器(组)上的设置. 配置主服务器 master 如果没有启用,则需要 激活二进制日志. 给 master ...
- C++学习视频和资料
我在学习c++时,比较迷茫,而且当时学完c++primer时不知道该学习什么, 犹豫了好久,最后找到了一些关于c++学习路线的视频,包含源代码,我感觉还不错,分享给大家. 下载地址 https://d ...