Kaseya Community

Need Powershell help - Logoff Procedure with variable

  • Hello everyone,

    I am a new to Kaseya and also powershell in general, I am hoping for some guidance with what I am trying to do. I am in need of a procedure to do the following:

    • Prompt for a username when scheduled, via getVariable
    • ExecuteShellCommand to export to a .ps1 file against the variable
    • Then ExecutePowershell file to logoff a specific user if they are logged in.

    I have used a procedure GetVariable before, but not via powershell. I need help with the ExecuteShellCommand to properly make use of the variable, here is my procedure and what I have for the shellcommand is: Echo Get-LoggedOnUser^| ForEach-Object {IF($_.UserName -like "#username#") {($Logoff)}} >> #vAgentConfiguration.agentTempDir#\logoff.ps1

    Thank you in advance, any help would be greatly appreciated!

    Best,

    Tarik



    clarified code
    [edited by: Tarik at 10:11 AM (GMT -8) on Nov 16, 2018]
  • is this what you're looking for:

    add this line at top of your ps1 script:

    "Param([parameter (Mandatory=$true)][String]$USERNAME)"

    At the end of the shellcommand, after the .ps1 you have to put #username#

    This way, the variable username is used inside the powershell script

  • Thank you for the suggestion. I was able to use your suggestion perfectly! But We are now attempting to use a pre-build powershell, and I am looking to enter variable with use of $args(0)

    The powershell script has:

    $user=$args[0]

    if (!$user){

       Write-Host "username needed"

       exit

    I am able to do the following to execute the powershell script manually against specific servers and specific user: .\logoffscript.ps1 username

    I still have the same get variable #user# in the procedure, how would I go about adding the #user# variable to be used to mirror .\logoffscript.ps1 username? I attempted to an ExecutePowerShell, entering the path of ps1 and enter #user# in command line, this did not work.

    Thank you in advance for your help.

  • Hello Tarik,

    Save this into an XML file and import it as a new procedure.

    <ScriptExport><Procedure name="LogOffUser" treePres="3" id="2106091490" folderId="954461154413623" treeFullPath="Vantage Drilling.Tools"><Body description=""><Statement name="GetVariable" continueOnFail="false"><Parameter xsi:type="EnumParameter" name="VariableType" value="Prompt"/><Parameter xsi:type="StringParameter" name="SourceContent" value="Enter the user name to logoff (leave empty for all)"/><Parameter xsi:type="StringParameter" name="VariableName" value="UserNameToLogoff"/></Statement><If description=""><Condition name="CheckVariable"><Parameter xsi:type="StringParameter" name="VariableName" value="#UserNameToLogoff#"/><Parameter xsi:type="EnumParameter" name="Condition" value="GreaterThan"/><Parameter xsi:type="StringParameter" name="Value" value=" "/></Condition><Then><Statement name="Execute Powershell" continueOnFail="false"><Parameter xsi:type="StringParameter" name="Parameter1" value=""/><Parameter xsi:type="StringParameter" name="Parameter2" value="quser '#UserNameToLogoff#' | Select-Object -Skip 1 | ForEach-Object { if (($_.Trim() -Split '\s+')[2] -eq 'Disc'){rwinsta ($_.Trim() -Split '\s+')[1]} else {rwinsta ($_.Trim() -Split '\s+')[1]}}"/><Parameter xsi:type="StringParameter" name="Parameter3" value="False"/></Statement></Then><Else><Statement name="Execute Powershell" continueOnFail="false"><Parameter xsi:type="StringParameter" name="Parameter1" value=""/><Parameter xsi:type="StringParameter" name="Parameter2" value="quser | Select-Object -Skip 1 | ForEach-Object { if (($_.Trim() -Split '\s+')[2] -eq 'Disc'){rwinsta ($_.Trim() -Split '\s+')[1]} else {rwinsta ($_.Trim() -Split '\s+')[1]}}"/><Parameter xsi:type="StringParameter" name="Parameter3" value="False"/></Statement></Else></If></Body></Procedure></ScriptExport>

    You will get a new "Logoff" user procedure that accepts one parameter (the user name to log off)

    You can also pass empty as a parameter in which case it will logoff ALL user on the machine where this is scheduled.

    Best Regards