I am trying to execute on powershell script while deployment on a server SO i used Azure CI/CD task named "PowerShell on Target Machines v2.*" but i am not getting any logs from that powershell script.
Do we have any command or way to receive all logs/output from powershell script executed on target server to CI/CD logging window.
Script is executing fine but whenever any error is thrown i want that error message to be shown in CI/CD logs.
i have tried version 1 and then moved to version 2, and under exception handling i had set Exit code 1 . but still exception is not getting received in pipeline logs
I can reproduce the same with you with task V2, you can use V3, it will show the output/error.
My powershell script:
try {
# Create a new directory
New-Item -Path C:\Users\Test -Name NewDir -ItemType Directory -ErrorAction Stop
} catch {
# Display the error details
Write-Host "An error occurred:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
Write-Host $_.ErrorDetails.Message -ForegroundColor Red
}
In V2 task, the output is below:
On V3 task, the output is below:
Edit: The PowerShell on Target Machines task actually runs enter-pssession
to connect to remote machine. If you are not able to run V3 task, you can use powershell task instead. Script sample below:
$username = 'username'
$password = 'password'
$computername = $(computernameorip)
$secureString = ConvertTo-SecureString -AsPlainText -Force -String $password
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$secureString
$cred = [PSCredential]::New($username,$secureString)
$session = New-PSSession -ComputerName $(computernameorip) -Credential $cred
Enter-PSSession -Session $session
Invoke-Command -FilePath C:\test\test2.ps1 -Session $session
Exit-PSSession
Pipeline result: