PowerShell Deploy

📅 2026/7/22 6:01:40
PowerShell Deploy
安装把此代码下载或通过git clone到本地然后在目录下面以管理员方式运行Install.ps1git clone https://github.com/ZEQP/ZEQP-PSDeploy.gitcd .\ZEQP-PSDeploy.\Install.ps1创建/关闭PSSession#通过弹出框输入密码$Credential Get-Credential -UserName “Administrator” -Message “请输入部署服务器的用户名和密码”#默认用HTTP-5985端口$Session New-PSSession -ComputerName 10.100.12.100 -Credential $Credential#中间过程处理 TODO#处理完成后关闭连接Remove-PSSession -Session $Session#直接创建PSSession$Password ConvertTo-SecureString “{PassWord}” -AsPlainText -Force$Credential New-Object System.Management.Automation.PSCredential(“Administrator”, $Password)##默认用HTTPS-5986端口$SessionOption New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck$Session New-PSSession -ComputerName 10.100.12.100 -Port 5986 -Credential $Credential -UseSSL -SessionOption $SessionOption#中间过程处理 TODO#处理完成后关闭连接Remove-PSSession -Session $Session自动编译然后发布到指定服务器IIS站点示例为dotnet后端项目实际支持任意言语项目只用写特定语言的编译发布命令就行Start-DeployIIS -SessionKaTeX parse error: Expected }, got EOF at end of input: …ck { param(o) dotnet publish -o $o -c “Debug” -f net8.0 --no-self-contained -v m --nologo /p:EnvironmentNameTest} -OutputPath .\bin\publish\ -RemotePath “D:\Publish”前端项目示例为nodejs的项目用yarn编译实际支持任意言语项目只用写特定语言的编译发布命令就行Start-DeployIIS -Session $Session -WebSiteName MyUIWebSite -WebSitePort 8081 -ScriptBlock {yarn run build:test} -OutputPath .\dist\ -RemotePath “D:\Publish”自动编译然后发布为服务Start-DeploySvc -SessionKaTeX parse error: Expected }, got EOF at end of input: …k { param(o) dotnet publish -o $o -c “Debug” -f net8.0 --no-self-contained -v m --nologo -p:EnvironmentNameTest} -OutputPath .\bin\publish\ -RemotePath “D:\Publish”直接部署文件#直接把DeployFile目录复制到服务器Publish目录下面,实际路径为D:\Publish\DeployFileStart-DeployFile -Session $Session -Path D:\DeployFile\ -RemotePath “D:\Publish” -Compress#直接把DeployFile目录下所有文件复制到服务器D:\Publish\DeployFile目录下面Start-DeployFile -Session $Session -Path D:\DeployFile* -RemotePath “D:\Publish\DeployFile” -Compress示例直接部署IIS站点在目录下创建PowerShell脚本文件Deploy.ps1$Credential Get-Credential -UserName “Administrator” -Message “请输入部署服务器的用户名和密码”$SessionOption New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck$Session New-PSSession -ComputerName 10.100.12.100 -Port 5986 -Credential $Credential -UseSSL -SessionOption $SessionOptionStart-DeployIIS -SessionKaTeX parse error: Expected }, got EOF at end of input: …ck { param(o) dotnet publish -o $o -c “Release” -f net8.0 --no-self-contained --no-restore -v m --nologo /p:EnvironmentNameProduction} -OutputPath .\bin\publish\ -RemotePath “D:\Publish”Remove-PSSession -Session $Session直接部署Windows服务在目录下创建PowerShell脚本文件Deploy.ps1$Credential Get-Credential -UserName “Administrator” -Message “请输入部署服务器的用户名和密码”$SessionOption New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck$Session New-PSSession -ComputerName 10.100.12.100 -Port 5986 -Credential $Credential -UseSSL -SessionOption $SessionOptionStart-DeploySvc -SessionKaTeX parse error: Expected }, got EOF at end of input: …k { param(o) dotnet publish -o $o -c “Release” -f net8.0 --no-self-contained --no-restore -v m --nologo -p:EnvironmentNameProduction} -OutputPath .\bin\publish\ -RemotePath “D:\Publish”Remove-PSSession -Session $Session批量部署多个服务在目录下创建PowerShell脚本文件Deploy.ps1$Credential Get-Credential -UserName “Administrator” -Message “请输入部署服务器的用户名和密码”$SessionOption New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck$Session New-PSSession -ComputerName 10.100.12.100 -Port 5986 -Credential $Credential -UseSSL -SessionOption $SessionOption#进入MyAPPWebSite目录下编译Set-Location -Path $MyAPPWebSitePathStart-DeployIIS -SessionKaTeX parse error: Expected }, got EOF at end of input: …ck { param(o) dotnet publish -o $o -c “Release” -f net8.0 --no-self-contained --no-restore -v m --nologo /p:EnvironmentNameProduction} -OutputPath .\bin\publish\ -RemotePath “D:\Publish”#进入MyAPPService目录下编译Set-Location -Path $MyAPPServicePathStart-DeploySvc -SessionKaTeX parse error: Expected }, got EOF at end of input: …k { param(o) dotnet publish -o $o -c “Release” -f net8.0 --no-self-contained --no-restore -v m --nologo -p:EnvironmentNameProduction} -OutputPath .\bin\publish\ -RemotePath “D:\Publish”Remove-PSSession -Session $Session部署示例方法描述Start-DeployIISStart-DeployIIS 用于本地构建并自动部署到远程 IIS 网站参数说明如下-Session 远程 PowerShell 会话对象必须已打开并连接到目标服务器。脚本会在该会话中创建、停止、启动 IIS 站点并上传 ZIP 文件。-ScriptBlock 本地构建脚本块用于生成部署产物。ScriptBlock 内通常使用 param($o) 接收输出目录并将构建结果写入 -OutputPath 指定的目录。-OutputPath 本地输出目录。脚本会检查路径是否存在不存在则创建存在则清空目录后执行构建并将输出文件压缩为 ZIP 包。-WebSiteName 目标 IIS 网站名称。脚本会尝试查找该站点若不存在则新建部署时会将内容解压到远程目录中的同名子目录。-WebSitePort 目标 IIS 网站端口用于创建新站点时绑定端口。若站点已存在则不会修改现有绑定。-RemotePath 目标服务器的远程发布目录默认值为 D:\Publish\。ZIP 文件会上传到此目录然后在目标服务器上解压到 RemotePath\WebSiteName。脚本执行流程检查并准备本地 OutputPath。执行 ScriptBlock 生成部署文件。压缩 OutputPath 内容为 ZIP。上传 ZIP 到远程服务器 RemotePath。查找或创建指定 WebSiteName 的 IIS 站点。停止网站、清理旧文件、解压新文件并重新启动网站。Start-DeploySvcStart-DeploySvc 用于本地构建并自动部署到远程 Windows 服务参数说明如下-Session 远程 PowerShell 会话对象必须已打开并连接到目标服务器。脚本会在该会话中创建、停止、启动服务并上传 ZIP 文件。-ScriptBlock 本地构建脚本块用于生成部署产物。ScriptBlock 内通常使用 param($o) 接收输出目录并将构建结果写入 -OutputPath 指定的目录。-OutputPath 本地输出目录。脚本会检查路径是否存在不存在则创建存在则清空目录后执行构建并将输出文件压缩为 ZIP 包。-ServiceName 目标 Windows 服务名称。脚本会尝试查找该服务若不存在则新建部署时会将内容解压到远程目录中的同名子目录。-BinaryName 服务可执行文件名称及启动参数例如 “Giant.DI.exe --environment Test”。该值用于创建服务时的 ImagePath。-RemotePath 目标服务器的远程发布目录默认值为 D:\Publish\。ZIP 文件会上传到此目录然后在目标服务器上解压到 RemotePath\ServiceName。脚本执行流程检查并准备本地 OutputPath。执行 ScriptBlock 生成部署文件。压缩 OutputPath 内容为 ZIP。上传 ZIP 到远程服务器 RemotePath。查找或创建指定 ServiceName 的 Windows 服务。停止服务、清理旧文件、解压新文件并重新启动服务。Start-DeployFileStart-DeployFile 用于将本地文件或目录直接发布到远程服务器可选压缩上传并支持本地/远程前后置脚本参数说明如下-Session 远程 PowerShell 会话对象必须已打开并连接到目标服务器。该参数用于远程文件复制和执行远程脚本。-Path 本地文件或目录路径。可以是目录、文件或通配符路径例如 D:\Deploy