From a88c546d8d3bf0d8e48aacc72d8ee98c047a18e1 Mon Sep 17 00:00:00 2001 From: Ian Morozoff Date: Thu, 16 Feb 2023 20:43:51 -0600 Subject: [PATCH 1/3] WIP: Add Foreach with Status snippet --- snippets/PowerShell.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/snippets/PowerShell.json b/snippets/PowerShell.json index ea576e4f9b..a73c359350 100644 --- a/snippets/PowerShell.json +++ b/snippets/PowerShell.json @@ -161,6 +161,23 @@ "}" ] }, + "Foreach with Progress": { + "prefix": "foreach-progress", + "description": "Insert a foreach loop with Write-Progress initialized", + "body": [ + "\\$total = $${1:array}.count", + "\\$i = 1", + "Foreach ($${2:item} in $${1:array}) {", + " \\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$total,4) * 100)", + " Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$total - \\$progPercent% Complete:\" -PercentComplete \\$progPercent", + " # Insert Code Here", + " ${0}", + " ", + " \\$i++", + "}", + "" + ] + }, "ForEach-Object -Parallel": { "prefix": "foreach-parallel", "description": "[PS 7+] Process multiple objects in parallel using runspaces. This has some limitations compared to a regular ForEach-Object. More: Get-Help ForEach-Object", From 995039ef112805b2238824f9bbe0ff8cf5e51449 Mon Sep 17 00:00:00 2001 From: ian-mor <78066011+ian-mor@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:37:59 -0500 Subject: [PATCH 2/3] Update total handling in foreach-progress snip Co-authored-by: Justin Grote --- snippets/PowerShell.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/PowerShell.json b/snippets/PowerShell.json index 602c9f8531..ae30e60649 100644 --- a/snippets/PowerShell.json +++ b/snippets/PowerShell.json @@ -165,7 +165,9 @@ "prefix": "foreach-progress", "description": "Insert a foreach loop with Write-Progress initialized", "body": [ - "\\$total = $${1:array}.count", +\\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$array.count,4) * 100)", + +Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$array.count - \\$progPercent% Complete:\" -PercentComplete \\$progPercent", "\\$i = 1", "Foreach ($${2:item} in $${1:array}) {", " \\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$total,4) * 100)", From 978ca37d1c326b05019ed19160e9720c86227c84 Mon Sep 17 00:00:00 2001 From: Andy Jordan <2226434+andyleejordan@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:36:04 -0700 Subject: [PATCH 3/3] Update snippets/PowerShell.json --- snippets/PowerShell.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/PowerShell.json b/snippets/PowerShell.json index ae30e60649..e4f7dacab8 100644 --- a/snippets/PowerShell.json +++ b/snippets/PowerShell.json @@ -169,7 +169,7 @@ Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$array.count - \\$progPercent% Complete:\" -PercentComplete \\$progPercent", "\\$i = 1", - "Foreach ($${2:item} in $${1:array}) {", + "foreach ($${2:item} in $${1:array}) {", " \\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$total,4) * 100)", " Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$total - \\$progPercent% Complete:\" -PercentComplete \\$progPercent", " # Insert Code Here",