locked
Start-Job not working with a script block RRS feed

  • Question

  • Hi,

    I'm trying to make a script to log off a user from all his remote sessions in RDS.

    The script worked well before I convert it to a job.

    The working script:

    $useraccount = "username"
    $connectionBrocker = “cb01.domain.com”
    $userid = Get-RDUserSession -ConnectionBroker $connectionBrocker | where UserName -eq $useraccount 
    foreach($user in $userid)
    {
    $user | Send-RDUserMessage -MessageTitle "Message from Administrator" -MessageBody "Please save your work. You will be logged off in 10 minutes"
    sleep -Seconds 60
    $user | Invoke-RDUserLogoff -Force
    }

    When I tried to convert it to a job like:

    $useraccount = "username"
    $connectionBrocker = “cb01.domain.com”
    $userid = Get-RDUserSession -ConnectionBroker $connectionBrocker | where UserName -eq $useraccount 
    foreach($user in $userid)
    {
    $scriptblock = {
        $user | Send-RDUserMessage -MessageTitle "Message from Administrator" -MessageBody "Please save your work. You will be logged off in 10 minutes"
        sleep -Seconds 60
        $user | Invoke-RDUserLogoff -Force
        }
        Start-Job -ScriptBlock $scriptblock
    }

    I'm getting that the job is running, but it's not doing anything.

    Is there anything I'm missing.

    Regards,

    Housam Smadi


    If you found this post helpful, please give it a "Helpful" vote. If it answered your question, remember to mark it as an "Answer". This posting is provided "AS IS" with no warranties and confers no rights! Always test ANY suggestion in a test environment before implementing!

    Thursday, August 9, 2018 7:39 PM

Answers

  •     $scriptblock = {
            Param($user)
            $user | Send-RDUserMessage -MessageTitle "Message from Administrator" -MessageBody "Please save your work. You will be logged off in 10 minutes"
            sleep -Seconds 60
            $user | Invoke-RDUserLogoff -Force
        }
        Start-Job -ScriptBlock $scriptblock -ArgumentList $user
    }


    \_(ツ)_/

    • Marked as answer by Housam Smadi Thursday, August 9, 2018 8:12 PM
    Thursday, August 9, 2018 8:08 PM

All replies

  •     $scriptblock = {
            Param($user)
            $user | Send-RDUserMessage -MessageTitle "Message from Administrator" -MessageBody "Please save your work. You will be logged off in 10 minutes"
            sleep -Seconds 60
            $user | Invoke-RDUserLogoff -Force
        }
        Start-Job -ScriptBlock $scriptblock -ArgumentList $user
    }


    \_(ツ)_/

    • Marked as answer by Housam Smadi Thursday, August 9, 2018 8:12 PM
    Thursday, August 9, 2018 8:08 PM
  • Thank you!

    If you found this post helpful, please give it a "Helpful" vote. If it answered your question, remember to mark it as an "Answer". This posting is provided "AS IS" with no warranties and confers no rights! Always test ANY suggestion in a test environment before implementing!

    Thursday, August 9, 2018 8:13 PM