Wierd bug with Variable

Hi Guys,

So i have a script that ran fine, all of a sudden, it seems like it isn’t passing a variable into my loop. Let me explain, i have this piece of code:

$sessionhost = @("***","***","***")
$filestore = "***"
$user = Read-Host "Please enter username"

foreach($sessionhosts in $sessionhost) {

$session = ((quser /server:$sessionhosts | ? { $_ -match $user }) -split ' +')[2]

logoff $session /server:$server 


Which finds a user based on input, and logs them off the target RDS server. The issue im having is, whenever i run the script and enter a user, it just logs out whoever is running the script at that time! I think it isn’t passing the $user variable and is just running the logoff command locally, which is far from ideal. But it seemed to break itself (it’s code, so surely it can’t) but that’s what it seems like! Is there an error here that i’m blatantly missing? This script has ran successfully before, many times by the way!

Even if i can see what gets passed to $user, so i can confirm this, debugging is a skill i’m trying to get better at!

"user is $user"

Hi JS,

I figured that out in the end, shamefully took me longer than it should. It seemed to pass the captured variable, which is odd, because it should only send the logoff command to a user that matches $user, but in this case it seems to be ignoring it and just logging off the person running the script! :frowning: