[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 为基础进行设计并实现,但其中的设计理念及方法,亦适用于任何类型的客 ...
随机推荐
- LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
题目 我是按照边进行二分的 class Solution { public: int sum[100005]; int a[305][305]; int maxSideLength(vector< ...
- System.InvalidOperationException: 'Cannot create more than one System.Windows.Application instance in the same AppDomain.'
System.Windows.Application is a singleton: its constructor must only be invoked once (including App. ...
- Java自学-I/O 控制台输入流System.in
Java 控制台输入流 System.in和Scanner System.out 是常用的在控制台输出数据的 System.in 可以从控制台输入数据 步骤 1 : System.in package ...
- 深入浅出JVM的锁优化案例
锁优化 适应性自旋(Adaptive Spinning) 线程阻塞的时候,让等待的线程不放弃cpu执行时间,而是执行一个自旋(一般是空循环),这叫做自旋锁. 自旋等待本身虽然避免了线程切换的开销,但它 ...
- Eureka服务下线源码解析
我们知道,在Eureka中,可以使用如下方法使Eureka主动下线,那么本篇文章就来分析一下子这个下线的流程 public synchronized void shutdown() { if (isS ...
- Vue笔记--同局域网下访问本地项目
正常开发中有时间提测比较麻烦.通常让测试小姐姐连接开发本地开启的服务器访问本地项目(在同一局域网下). 其实一般项目IDE已经实现这些功能例如webstorm和vscode,有时候需要单独配置下. 但 ...
- golang多个项目时如何配置GOPATH,使用gb包依赖管理工具,不同项目配置不同的GOPATH的
golang多个项目时如何配置GOPATH,使用gb包依赖管理工具,不同项目配置不同的GOPATH的 1:执行脚本setGoPath.sh#!/bin/bashif [[ $GOPATH =~ .*$ ...
- 011.maven 继承与聚合
聚合:对于聚合模块来说,它知道有哪些被聚合的模块,而对于被聚合的模块来说,它们不知道被谁聚合了,也不知道它的存在: 继承:对于继承关系的父POM来说,它不知道自己被哪些子模块继承了,对于子POM来说, ...
- Django 练习班级管理系统一
创建项目 user_manager 和 app为 app01 models.py 为 from django.db import models # Create your models here. c ...
- (九)OpenStack---M版---双节点搭建---Swift(单节点)安装和配置
↓↓↓↓↓↓↓↓视频已上线B站↓↓↓↓↓↓↓↓ >>>>>>传送门 本次搭建仅采用Compute单节点做swift组件 1.Controller安装并配置控制节点 ...