Powershell - Trouble with incrementing counter for label making script

Question!

I'm an extremely new person to Powershell (started 4 hours ago). I'm trying to make a small script that takes user input and outputs to a file then prints that file to a label printer. From current testing I got everything to work, except the incrementing counter. it never seems to go above 0 thus causing a infinite print loop. I want the increment becasue say if I have 10 pallets of product I want 10 labels that read "1 of 10" through "10 of 10" I've researched this for about and hour and tried a few suggestions and they don't seem to work. This is the script I have right now just with generic names(sorry for post format):

$i = 0
$counter = $global:i + 1


Write-Host "========== Print Comapny Labels =========="
Write-Host "Question will be on the left, type in answer and hit ENTER"

$location = Read-Host "Which location? (LOC1 or LOC2)"
$pro = Read-Host "What is the Pro#?"
$quote = Read-Host "What is the quote number?"
$pallet = Read-Host "How many pallets are there?"
$printer = Read-Host "What printer do you want?"

While ($counter -le $pallet)
{
    Set-Content -Path "C:\temp\Label.txt" -Value "^XA^MCY^XZ^XA^SZ2^MMT^MTT~JSN^LT0^MD0^MNY^PR5^PON^PMN^CI0^LRN
                                  ^FT300,1300^A0B,350,160^FDPRO #: $pro^FS
                                  ^FT480,1300^A0B,175,150^FD$location^FS
                                  ^FT480,900^A0B,175,100^FDQuote #: $quote^FS
                                  ^FT800,1300^A0B,350,225^FDPallet $counter of $pallet^FS
                                  ^PQ01~*QUANT,04~,0,~*COPIES,04~,N^MCY^XZ"

    Get-Content -Path "C:\temp\Label.txt" | Out-Printer -Name "$printer"

    $global:i++
}

Any help would be greatly appreciated!

Thanks



Answers

It looks to me like the problem has to do with inconsistent use of the global: prefix.

You initialize $i as a local variable, but then use and increment it as $global:i. Seems like the global never gets initialized. I'd just drop the global: prefix everywhere unless you have a specific reason for using it.



You can get updates of user and updates of user's friends by doing something similar to this.

SELECT u.*
FROM updates u INNER JOIN 
   (SELECT friendsuserid as userid FROM friends WHERE userid = [youruserid]
    UNION
    SELECT userid FROM friends WHERE friendsuserid = [youruserid]
   )f
ON u.userid = f.userid
By : Tin Tran


I think you should look into JOINs and subqueries. You should make a "subquery" - select the user that is logged in then join on the friends table (to get all of the friends userIds) and then use that list to filter in updates table

basic joins explanation

subquery info

By : flynorc


This video can help you solving your question :)
By: admin