我需要创建一个发布管道来复制两个 .dll 文件并远程执行 PowerShell 脚本。
我做了什么,
我有一个 onPrem 代理服务器 (onPrem1/172.24.243.193),文件已下载到该服务器 服务器(D:gent_work 65 \DeploymentPackage\ReleaseFiles) - 成功
onPrem 代理位于不同的网络 (172.24.243.193),远程服务器位于另一个网络 (172.26.143.22)。
PowerShell 脚本包括;
由于我从 onPrem 代理服务器运行 PowerShell 脚本,并且 .dll 文件位于不同的网络中,我该如何解决此问题。
function Backup-file {
param(
[String] $serverName,
[String] $fileName
)
$releaseFolderPath = "\\$serverName\d$\app\"
$backupFileDirectory = "$releaseFolderPath\Backups\"
Write-Host "Backing up $releaseFolderPath to $backupFileDirectory"
$releaseFileFullPath = Join-Path -Path $releaseFolderPath -ChildPath $fileName
if (Test-Path -Path $releaseFileFullPath){
Write-Host "File Exists $releaseFileFullPath "
try {
# Create the Backup folder if it doesn't exist
if (-not (Test-Path -Path $backupFileDirectory)) {
New-Item -ItemType Directory -Path $backupFileDirectory
Write-Host "Backup folder created on $backupFileDirectory"
}
#Rename & Move the exsisting release file
$backupFileName = "ABC_" + (Get-Date -Format "yyyyMMddHHmmss") + ".dll"
$backupFileFullPath = Join-Path -Path $backupFileDirectory -ChildPath $backupFileName
$tempFile = Join-Path -Path $releaseFolderPath -ChildPath $backupFileName
Write-Host "Temp file $tempFile"
Rename-Item -Path $backupFileFullPath -NewName $backupFileName
Move-Item -Path $tempFile -Destination $backupFileDirectory
Write-Host "File backup and rename successful in Server !!!"
} catch {
Write-Host "Error: $_"
}
}else{
Write-Host "Error: File not found $releaseFileFullPath!!!"
}
}
function Copy-Source-Files {
try{
Write-Host "Copy-Source-Files started .."
$releaseFilePath = Join-Path $sourceFolderPath -ChildPath $fileName
Write-Host "Release path: $releaseFilePath appServerReleaseFolderPath: $appServerReleaseFolderPath"
Copy-Item -Path $releaseFilePath -Destination $appServerReleaseFolderPath
}
catch{
Write-Host "Error occured in Copy-Source-Files $_"
}
}
function Restart-Service {
param(
[String] $ServerName,
[String] $ServiceName
)
Write-Host "Restarting $ServiceName of $ServerName"
$securePassword = ConvertTo-SecureString $plaintextPassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $securePassword)
$scriptBlock = {
param($Service)
Restart-Service -Name $Service -Force
}
Invoke-Command -ComputerName $ServerName -ScriptBlock $scriptBlock -ArgumentList $ServiceName -Credential $credential
}
function Restart-Service {
$appService = "AKServ"
Restart-Service -ServerName $ServerName -ServiceName $appService
}
try{
$isValid = Validate-Source-File
if(!$isValid){
Write-Host "File is not valid $fileName"
return
}
Write-Host "Processing Started...."
Backup-file -serverName $appServerName -fileName $fileName
#Copy-Source-Files
Restart-Services
Write-Host "Release completed ..."
}catch{
Write-Host "Error occured .. $_"
}
由于我从 onPrem 代理服务器运行 PowerShell 脚本,并且 .dll 文件位于不同的网络中,我该如何解决此问题。
您可以将 PowerShell 脚本文件连同 .dll 文件复制到远程服务器,然后使用 PowerShellOnTargetMachines@3 任务直接在远程服务器上运行 PowerShell 脚本。
设置方法如下:
请检查任务的先决条件,并确保在目标计算机上正确配置Windows 远程管理 (WinRM),并且必要的网络配置已到位以允许计算机之间进行通信。