Artificially splitting steps in pipeline duration report - azure-devops

I have a pipeline where single step is particularly long. Its process is generated dynamically and I cannot split it. I hoped that I could add some log output that would create new entries for parts of it.
I tried to use task.logdetail to make it work. Something like:
- pwsh: | # New entry
$guid1 = (New-Guid).ToString()
Write-Output "GUID: ${guid1}"
Write-Output "##vso[task.logdetail id=${guid1};name=project1;type=build;order=1]start build"
Start-Sleep -s 1
Write-Output "update build"
Write-Output "##vso[task.logdetail id=${guid1};progress=15;state=InProgress;]update build"
Write-Output "start restore"
$guid11 = (New-Guid).ToString()
Write-Output "##vso[task.logdetail id=${guid11};parentid=${guid1};name=project1;type=build;order=1]start restore"
Start-Sleep -s 10
Write-Output "finish restore"
Write-Output "##vso[task.logdetail id=${guid11};state=Completed;]finish restore"
$guid12 = (New-Guid).ToString()
Write-Output "start build"
Write-Output "##vso[task.logdetail id=${guid12};parentid=${guid1};name=project1;type=build;order=1]start build"
Start-Sleep -s 10
Write-Output "finish build"
Write-Output "##vso[task.logdetail id=${guid12};state=Completed;]finish build"
Write-Output "finish build"
Write-Output "##vso[task.logdetail id=${guid1};state=Completed;]finish build"
$guid2 = (New-Guid).ToString()
Write-Output "start test"
Write-Output "##vso[task.logdetail id=${guid2};name=project1;type=test;order=1]start test"
Start-Sleep -s 10
Write-Output "finish test"
Write-Output "##vso[task.logdetail id=${guid2};state=Completed;]finish test"
displayName: Long process
Unfortunately it does not seem to work as intended as I dont see any new entries in my pipeline analitycs.
Is there a way to split a step into multiple entries on Pipeline duration report

Related

PowerShell: Do/Until loop until multiple jobs are completed

Morning folks,
I'm working on a Do/Until loop that tracks an invoke-command job against multiple servers. What I don't know how to capture in the Until block is to continue doing the loop until all servers report a Completed status.
below is my test code
Invoke-Command -ComputerName Server01,Server02,Server03 -ScriptBlock{sleep -s 120} -AsJob -JobName "Test Job"
Do{
Write-Host "Job is still running"
sleep -s 10
}Until((Get-Job "Test Job").state -eq "Completed"
I'm thinking I need to put the following in the Until block but I'm not sure
Until(ForEach($job in (Get-Job "Test Job")){
$Job.State -eq "Completed"
})
I don't have the means to test this at the moment, but if you're after a progress output, I don't see why this wouldn't work.
$jobs = Invoke-Command -ComputerName Server01,Server02,Server03 -ScriptBlock{sleep -s 120} -AsJob
do {
foreach ($job in ($jobs.ChildJobs | ? 'State' -EQ 'Running')
{
Write-Host -Object (
"Job {0} is still running on computer {1}." -f $job.Name, $job.Location
)
}
Start-Sleep -Seconds 1
} until ($jobs.ChildJobs.State -notcontains 'Running')
As mentioned above, I don't have the means to test this, nor do I recall if the State property you'd get from the parent job takes into account for the child jobs running. Technically speaking, what you tried would have worked as well given it had the closing parenthesis.
Do{
Write-Host "Job is still running"
sleep -s 10
}Until((Get-Job "Test Job").state -eq "Completed")
I didn't account for every issue you may come across but one thing you may be wary of is that the State property may not always be in a "Completed", or "Running" state. This could lead to an indefinite loop testing for a "Completed" status.

Powershell Write-Progress and Adding Timer

I have a PowerShell script for Microsoft Teams that looks at a list of Users in a .csv file and returns a few properties of the user, then exports it to another .csv with the results. This works fine except the progress bar is stuck displaying the last entry until I hit enter. How do you make the last entry disappear and only show "Done Executing Script" and return to C:>
Im trying to add the time at the end of the line when each of the lines from the CSV is executed, Please help me with that. Example below:
Processing User Number: 1 [Currently Processing: User1#abc.com] - Policies applied in "5s"
Processing User Number: 2 [Currently Processing: User2#abc.com] - Policies applied in "2s"
Processing User Number: 3 [Currently Processing: User3#abc.com] - Policies applied in "10s"
Processing User Number: 4 [Currently Processing: User4#abc.com] - Policies applied in "8s"
Processing User Number: 5 [Currently Processing: User5#abc.com] - Policies applied in "25s"
CODE FOR POWERSHELL SCRIPT:
Write-host "Connecting to Microsoft Teams....." -ForegroundColor Yellow
Connect-MicrosoftTeams
Write-host "Successfully connected to Microsoft Teams" -ForegroundColor Green
$CsvFilePath = Import-CSV -Path "C:\Users\\Desktop\MicrosoftTeams\precheckbatch2.csv"
$Count=0
$results = foreach ($UPN in $CsvFilePath) {
$user = $UPN.UserPrincipalName
Get-CsOnlineUser $user | Select-Object Displ*, UserPri*, IsSipEnabled, TeamsUpgradeE*,
Enterprise*, #{l="FeatureTypes";e={$_.FeatureTypes -join "; "}}, #{l="AssignedPlan";e=
{$_.AssignedPlan -join "; "}}
Write-Progress -Activity "Processing User: $Count" "Currently Processing: $user"
$Count++
}
$results | Export-Csv C:\Users\\Desktop\MicrosoftTeams\precheckdataresults1.csv
Write-host "Done!"
This seems to be an issue of VSCode only. When I run this simplified script in the console, the progress bar disappears automatically, when the script ends. When I run it from VSCode, the progress bar stays visible, reproducing the issue.
foreach( $count in 1..3 ) {
$user = "User$count"
Write-Progress -Activity "Processing User: $Count" "Currently Processing: $user"
Start-Sleep 1
}
To fix the problem for VSCode, add a Write-Progress -Completed line, to explicitly remove the progress bar:
foreach( $count in 1..3 ) {
$user = "User$count"
Write-Progress -Activity "Processing User: $Count" "Currently Processing: $user"
Start-Sleep 1
}
Write-Progress -Activity "Processing User: $Count" -Completed

PowerShell - provide console output to user while waiting for jobs (start-job) to finish

I use this script to run some jobs:
#========================================================================
#Get User stats with ADInfo < --- need to see if this can be converted to native PowerShell
$XMLConfig = Get-ChildItem c:\ADInfo\XML_Config_Files
$Jobs = #()
#loop through each config file and run ADAudit - there is one file per domain
foreach ($config in $XMLConfig) {
write-host "Starting a background job for $($config.name)"
$Jobs += start-job -ScriptBlock {c:\ADInfoCmd.exe /config $args[0] } -ArgumentList $config.fullname.tostring()
}
write-host "`nJobs are running"
#=======================================================================
#End of script
Some jobs take much longer than others and I would like to be able to send a user friendly update to the console when any one of the started jobs are still running to show the script hasn't stalled.
I tried something like this
do{
write-host "working..."
}
while (wait-job $jobs)
but it writes once and then waits for the jobs to finish
I tried this
$joblist = get-job $jobs | where state -eq running
while ($joblist){
write-host "working..."
}
but I get an error for all the jobs get-job : The command cannot find the job because the job name System.Management.Automation.PSRemotingJob was not found and $joblist is never assigned a value.
Is there a way to do this?
I had passed the entire PS Object to get-job. It worked when I passed only the job ID
This is what I ended up using and provides enough feedback to the user to demonstrate the script is still working.
write-host "`nJobs are running" -ForegroundColor Yellow -NoNewline
$RunningJobs = Get-Job $jobs.id | where state -eq running
while($runningjobs){
write-host "." -NoNewline
$RunningJobs = Get-Job $jobs.id | where state -eq running
Start-Sleep -Seconds 3
}
Write-host "Background Jobs Complete"
Write-Host "Script Ends" -ForegroundColor Yellow

Increment package number using powershell

I have a script to push a package to Octopus which works. I want to push the package a number of times say 10. But to do this I need to increment the package number by 1 each time. I cant upload the same package number twice. The pushing to Octopus works but im not sure where to start with the increment of the package from MyTestPackage.1.1.0.zip to MyTestPackage.1.1.1.zip. The 0 at before the .zip will need to increment by 1 each time.
I was think of a for loop to upload package 10 times?? Any help would be great
function PushPackage {
param(
$OctopusUri,
$ApiKey,
$Package
)
try{
Write-Host "Pushing $Package to Octopus"
Start-Process -NoNewWindow -FilePath $OctopusExePath
& $OctopusExePath push --package $Package --server $OctopusUri --apiKey $ApiKey --logLevel 'verbose'
Write-Host "Pushing $Package to Octopus - Success"
}
catch{
Write-Host "PUSH PACKAGE Exception--------- $_"
}
}
$OctopusUri = "https://MyOctopusTest.local"
$ApiKey = "API-CRW9JBMJGUTEUEYEYUE"
$Package = "C:\temp\OctoTest1\MyTestPackage.1.1.0.zip"
PushPackage $OctopusUri $ApiKey $Package

Move-AzureDeployment taking too long to swap servers?

I'm trying to swap servers using Move-AzureDeployment cmdlet by running it in my powershell. It seems to take around 4 mins for it to swap from staging to production. Thats 4 mins of downtime and it's not really acceptable. When I swap the servers manually from the Azure Portal it happens almost instantaneously.
I was wondering why it takes longer using the cmdlet and what can I do to fix this issue because I want to be able to swap my Staging and Production Servers using powershell.
Here is my powershell Script:
try
{
$ErrorActionPreference = "stop"
Write-Host "Deploying build build no. $env:build_number to $_serviceName"
#import azure cmdlets module
Write-Host "Importing azure service management modules (i.e for the old portal)"
Import-Module "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1"
Write-Host "Started Command for Switching Slots"
#Switch slots from Staging to Production
Move-AzureDeployment -ServiceName $_serviceName
Write-Host "Finished Command for Switching Slots"
#make sure deployment is in running state
$deployment = Get-AzureDeployment -servicename $_serviceName -slot $_slotName
Write-Host "$_serviceName is in state $($deployment.status)"
$StopWatch = [System.Diagnostics.Stopwatch]::StartNew() #declare stopwatch
while (($deployment.Status -ne "running") -and ($StopWatch.Elapsed.Hours -lt 2)) #running the loop for a maximum of 2 hours
{
Write-Host "wait 5 seconds before trying again"
Start-Sleep -s 5
$deployment = Get-AzureDeployment -servicename $_serviceName -slot $_slotName
Write-Host "$_serviceName is in state $($deployment.status)"
}
#make sure all roles are in ready state
$nonReadyInstances = (Get-AzureDeployment $_serviceName -Slot $_slotName).RoleInstanceList | Where-Object { $_.InstanceStatus -ne "ReadyRole" } | ft -Property RoleName, InstanceName, InstanceStatus
$nonReadyInstances
$StopWatch = [System.Diagnostics.Stopwatch]::StartNew() #declare stopwatch
while (($nonReadyInstances -ne $null) -and ($StopWatch.Elapsed.Hours -lt 2)) #running the loop for a maximum of 2 hours
{
Write-Host "wait 5 seconds before trying again"
Start-Sleep -s 5
$nonReadyInstances = (Get-AzureDeployment $_serviceName -Slot $_slotName).RoleInstanceList | Where-Object { $_.InstanceStatus -ne "ReadyRole" }
$nonReadyInstances
}
#output deployment id
#$deploymentid = Check-Deployment -serviceName $_serviceName -slotName $_slotName
#Write-Host "Deployed to $_serviceName with deployment id $deploymentid and slot $_slotName"
exit 0
}
catch [System.Exception]
{
Write-Host $_.Exception.ToString()
exit 1
}
From my understanding a the swap between staging slot and production is a VIP swap as they are both running. Microsoft says you shouldn't see any downtime on the site/app itself. Requests will still hit the old production server until the change is complete. Are you seeing downtime on the site during the swap?