I hope someone can spot the error:
This is a small function inside a bigger script.
I want it to increment $i
every time it iterate the foreach
, but I can't get it to work. If I run only the line where $i++
is present, it will increment.
The message is arriving to the users, so it works fine, it's 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++
}
}
You have two variables with the same name "$i".
One of them is global and second one is local.
Here is an example which shows what is going on and how you can change global variable from inside a function (when you specify scope qualifier):
Here is an output which is produced by above mentioned code
Have a look at documentation about scopes