Azure Release Pipeline - 从远程服务器执行 PowerShell 脚本

问题描述 投票:0回答:1

我需要创建一个发布管道来复制两个 .dll 文件并远程执行 PowerShell 脚本。

我做了什么,

  1. 编写了一个 bash 脚本来从 GitHub 下载 .dll 文件和 PowerShell 脚本文件。

我有一个 onPrem 代理服务器 (onPrem1/172.24.243.193),文件已下载到该服务器 服务器(D:gent_work 65 \DeploymentPackage\ReleaseFiles) - 成功

  1. 使用 Windows 计算机文件复制 (WinRM) 将 .dll 文件复制到远程服务器 (AppServer1/172.26.143.22)。地点:D: 释放\ - 成功
  2. 使用 PowerShell v2.* 执行下载的 PowerShell 脚本 - 失败

onPrem 代理位于不同的网络 (172.24.243.193),远程服务器位于另一个网络 (172.26.143.22)。

PowerShell 脚本包括;

  • 检查远程位置是否存在.dll 文件。
  • 如果存在现有文件,请将该文件复制到 backup 文件夹。
  • 从 D: 复制新的 .dll 文件 释放\ 至 D: pp\
  • 重新启动 AKServ Windows 服务。

由于我从 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 .. $_"

}

enter image description here

powershell azure-devops azure-pipelines azure-pipelines-release-pipeline powershell-remoting
1个回答
0
投票

由于我从 onPrem 代理服务器运行 PowerShell 脚本,并且 .dll 文件位于不同的网络中,我该如何解决此问题。

您可以将 PowerShell 脚本文件连同 .dll 文件复制到远程服务器,然后使用 PowerShellOnTargetMachines@3 任务直接在远程服务器上运行 PowerShell 脚本。

设置方法如下:

  1. 使用 bash 脚本任务将 .dll 文件和 PowerShell 脚本文件从 GitHub 下载到 onPrem 代理服务器 (onPrem1/172.24.243.193)。
  2. 使用 Windows 计算机文件复制 (WinRM) 任务将 .dll 文件和 PowerShell 脚本文件复制到远程服务器 (AppServer1/172.26.143.22)。
  3. 使用“目标计算机上的 PowerShell”任务在目标计算机上运行 PowerShell 脚本。 然后输入目标计算机的 IP 地址或主机名,提供必要的凭据,并指定 PowerShell 脚本的路径。

请检查任务的先决条件,并确保在目标计算机上正确配置Windows 远程管理 (WinRM),并且必要的网络配置已到位以允许计算机之间进行通信。

© www.soinside.com 2019 - 2024. All rights reserved.