jesper

jesper

  • NA
  • 89
  • 54.2k

Powershell, help with incrementing inside foreach and scriptblock

Oct 17 2020 6:03 PM
Hi,
 
I hope someone can spot the error:
 
This is a small function inside a bigger script.
 
I want it to increment $i everytime it iterate the foreach, but i cant get it to work. If i run only the line where $i++ is present, it will increment.
 
The message arriving to the users, so it works fine, its just ignoring the $i++
 
$i = 0
function Message {
start-job -Name Col01 -InitializationScript{Import-Module RemoteDesktop} -ScriptBlock{$Users = Get-RDUserSession -ConnectionBroker $ActiveConnectionBroker -CollectionName "col01"
foreach ($User in $Users) {
Send-RDUserMessage -UnifiedSessionID $User.UnifiedSessionId -HostServer $User.HostServer -MessageTitle "Info" -MessageBody $using:message
$i++
}
}
}

Answers (8)