Author Posts

October 3, 2013 at 5:17 am

Hello again, folks! So again I seemed to have run into an issue with perhaps the logic or I a missing some key code (the code listed below is simply for testing at this point and does not represent its completeness.) So basically what I am trying to do is have a buttonClick event fire of a Foreach event and display its progress – simple. It seems to work – mostly, however, the progress bar when progressing through foreach statement is completing before the script is complete. Basically, whether an error exists on each item ($? = $true) or not ($? = $false) then performstep(). What am I missing and would this be the mos efficient way of writing the code?

$buttonStart_Click={
$progressbaroverlay1.TextOverlay = 'Uninstalling...'
$progressbaroverlay1.Value = 0
$progressbaroverlay1.Maximum = 100
$this.Enabled = $false
$installedAppList = Get-Content "\\SCCM-MPS\F\SoftwareDeployments\PowershellScripts\referenceFiles\appList_MSO2013Upgrade.txt"

# for($i = 0; $i -lt $progressbaroverlay1.Maximum; $i++){

foreach ($appInstalled in $installedAppList){

[System.Windows.Forms.Application]::DoEvents()

$app = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq $appInstalled}
$app.Uninstall()

if($? -eq $false){
$progressbaroverlay1.PerformStep()
}

if($? -eq $true){
Write-Host "$appInstalled is not installed."
$progressbaroverlay1.PerformStep()
}
}
$progressbaroverlay1.Value=100
$progressbaroverlay1.TextOverlay='Uninstall complete!'
$this.enabled = $true
# }
}

Again, thank is advance!

October 3, 2013 at 5:31 am

How many items are in your $installedAppList collection? Your progress bar is hard-coded for 100 "steps" right now, but ideally, you'd set the Maximum to $installedAppList.Count.

I'd recommend changing this code:

$app.Uninstall()

if($? -eq $false){
    $progressbaroverlay1.PerformStep()
}

if($? -eq $true){
    Write-Host "$appInstalled is not installed."
    $progressbaroverlay1.PerformStep()
}

The problem here is that the value of $? changes as soon as you execute $progressbaroverlay1.PerformStep(), so you'll wind up executing the code in both conditionals every time. At the very least, change it to an if/else construct, or even better, just move the call to PerformStep() out of the conditionals (since it's done no matter what):

$app.Uninstall()

if ($? -eq $true) {
    Write-Host "$appInstalled is not installed."
}

$progressbaroverlay1.PerformStep()

Opinions vary on whether to use $? for error checking at all, but this is at least a step in the right direction.

October 3, 2013 at 5:57 am

Thanks, Dave. In changing it the way you described ( and it makes sense to move it outside of the conditional) I now get no progress in the progress bar. Any more ideas? I really appreciate your help on this – thank you.

$buttonStart_Click={

$progressbaroverlay1.TextOverlay = 'Uninstalling...'
$progressbaroverlay1.Value = 0
$progressbaroverlay1.Maximum = $installedAppList.Count
$this.Enabled = $false
$installedAppList = Get-Content "\\SCCM-MPS\F\SoftwareDeployments\PowershellScripts\referenceFiles\appList_MSO2013Upgrade.txt"

foreach ($appInstalled in $installedAppList){

[System.Windows.Forms.Application]::DoEvents()

$app = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq $appInstalled}
$app.Uninstall()

if($? -eq $false){
Write-Host "$appInstalled is not installed."
}

$progressbaroverlay1.PerformStep()
}
$progressbaroverlay1.Value=100
$progressbaroverlay1.TextOverlay='Uninstall complete!'
$this.enabled = $true
}

October 3, 2013 at 5:59 am

I guess "code" is not the tag to use... What is the tag you used to display the code in your post?

October 3, 2013 at 6:18 am

The CODE tag is fine for single line stuff, but mostly I use PRE (and I have to type the tags manually, since none of the buttons on the forum do that).

The only problem I see right now is that you're calling this line:


$progressbaroverlay1.Maximum = $installedAppList.Count

Before this one:


$installedAppList = Get-Content "\\SCCM-MPS\F\SoftwareDeployments\PowershellScripts\referenceFiles\appList_MSO2013Upgrade.txt"

You need to create the $installedAppList collection before you check its Count. 🙂

October 3, 2013 at 6:34 am

Ah! The tag make pre makes sense (can you tell I do not post often?) 🙂

So after your recommendation (logic gets me every time) something strange is happening; after the first foreach loop the progress bar does not move, however, after the second loop processes the bar moves, it just moves a 1/4th of the way up the bar and continues that jump through the next 2-3 loops at which the bar is then maxed. There are 33 items in the $installedAppList so it should definitely not move 1/4th. When I originally set the $progressbaroverlay.maximum value I was thinking in terms of percent (thank goodness for guys like you.)

$buttonStart_Click={
 
	$progressbaroverlay1.TextOverlay = 'Uninstalling...'
        $progressbaroverlay1.Value = 0
	$this.Enabled = $false
	$installedAppList = Get-Content "\\SCCM-MPS\F\SoftwareDeployments\PowershellScripts\referenceFiles\appList_MSO2013Upgrade.txt"
	$progressbaroverlay1.Maximum = $installedAppList.Count

	foreach ($appInstalled in $installedAppList){
				
		[System.Windows.Forms.Application]::DoEvents()
			
		$app = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq $appInstalled} 
		$app.Uninstall()
				
			if($? -eq $false){
				Write-Host "$appInstalled is not installed."
			}
		
			$progressbaroverlay1.PerformStep()		
	}
	$progressbaroverlay1.Value=100
	$progressbaroverlay1.TextOverlay='Uninstall complete!'
	$this.enabled = $true
}

October 3, 2013 at 6:41 am

Now you're going to make me go learn about Windows Forms or something. 😛 My answers so far have been just general observations of the code, not based on actual tests.

Looks like you need to set the Step property of your progress bar to 1; the default is 10.

$buttonStart_Click={
    $progressbaroverlay1.TextOverlay = 'Uninstalling...'
    $progressbaroverlay1.Value = 0
    $progressbaroverlay1.Step = 1
    $this.Enabled = $false
    $installedAppList = Get-Content "\\SCCM-MPS\F\SoftwareDeployments\PowershellScripts\referenceFiles\appList_MSO2013Upgrade.txt"
    $progressbaroverlay1.Maximum = $installedAppList.Count
    
    foreach ($appInstalled in $installedAppList){
        [System.Windows.Forms.Application]::DoEvents()
        $app = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq $appInstalled} 
        $app.Uninstall()

        if($? -eq $false){
            Write-Host "$appInstalled is not installed."
        }
	
        $progressbaroverlay1.PerformStep()		
    }
    
    $progressbaroverlay1.Value = $progressbaroverlay1.Maximum
    $progressbaroverlay1.TextOverlay='Uninstall complete!'
    $this.enabled = $true
}

October 3, 2013 at 7:01 am

You, sir, are a genius. It pays to be resourceful and not just have someone give you the answers... Next time I will use my resources. I was using the Sapien forums and documentation but just couldn't figure it out. We are now progressing through the loop! Thank you very much for all your assistance.