Kaseya Community

Powershell - Execute PS1 script

This question is not answered

I've seen quite a few previous posts about powershell and those threads are not able to answer my question.

So, here we go.

The script fails on step 1. If I can't get past step 1 then the rest of the script does not matter. Please feel free to add your suggestions.

<Statement name="Execute Powershell Command (64-bit, Run As System)" continueOnFail="false">
<Parameter xsi:type="StringParameter" name="Parameter1" value=""/>
<Parameter xsi:type="StringParameter" name="Parameter2" value="$ComputerName = $env:computername"/>
<Parameter xsi:type="StringParameter" name="Parameter3" value="False"/>
Basically I'm trying to create $ComputerName - variable from $env:computername
This works perfectly fine in powershell, but not through kaseya agent procedure.
I'm skipping the first parameter because I do not want to reference PS1 script until I get the computer name. Maybe I need to change my perspective and get the computer name a different way?
I need to reference this important step 1 variable later in powershell to get my PS1 script to run correctly.
Thoughts? Jokes? Suggestions? Do powershell agent procedures even work?
All Replies
  • Why not just call your PS1 with $env:computername as the parameter?

    Keep in mind that each step runs as a standalone operation - setting $ComputerName in line 1 of your script then expecting to use $ComputerName as a powershell variable in step 2 won't work as it's a brand new instance of powershell each step.

  • Hello Johnathan, Sadly I was never able to get mine working either.

    Therefore i wrote the following procedure to copy a ps1 file to the users desktop and run the script as system from there.

    First place the Powershell Script (created through notepad and saved as .PS1 extension) in the shared files area on Manage Procedures.

    Then create the below procedure.

    getVariable("Agent Working Directory Path", " ", "kworking", "All Windows Operating Systems", "Halt on Fail")

    writeFile("Scriptname.PS1", "#kworking#\Scriptname.PS1", "All Windows Operating Systems", "Halt on Fail")

    executePowershell ("#kworking#\Scriptname.PS1", " ", false, "All Windows Operating Systems", "Halt on Fail")

    Hope this helps.

  • That really does answer my hidden concerns. Thanks Combo. I was curious if it was creating a new instance for each step.

  • Thank you Daniel. I will try your suggestion.

  • Combo and Daniel,

    I need to get the computer name as a variable with Kaseya beforehand. The PS1 script I have pulls mailbox statistics, email address, and much more. The way the script is built is to use the server name in the arguement/switch to initialize the script itself.

    So step 2 looks like this:

    Get-Mailbox.ps1 -server $computername -SendEmail -MailFrom jlascari@ml-cc.com -MailTo jlascari@ml-cc.com -MailServer smtp.mailrunner.com

  • <If description="">

    <Condition name="True"/>


    <Statement name="GetVariable" continueOnFail="false">

    <Parameter xsi:type="EnumParameter" name="VariableType" value="AgentTempDirectory"/>

    <Parameter xsi:type="StringParameter" name="SourceContent" value=""/>

    <Parameter xsi:type="StringParameter" name="VariableName" value="kworking"/>


    <Statement name="WriteFile" continueOnFail="false">

    <Parameter xsi:type="StringParameter" name="Path" value="#kworking\Get-MailboxHTML.ps1"/>

    <Parameter xsi:type="StringParameter" name="ManagedFile" value="VSASharedFiles\Get-MailboxHTML.ps1"/>

    <Parameter xsi:type="BooleanParameter" name="DeleteAfter" value="False"/>


    <Statement name="GetVariable" continueOnFail="false">

    <Parameter xsi:type="EnumParameter" name="VariableType" value="RegistryValue"/>

    <Parameter xsi:type="StringParameter" name="SourceContent" value="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\ComputerName"/>

    <Parameter xsi:type="StringParameter" name="VariableName" value="computername"/>


    <Statement name="Execute Powershell Command (64-bit, Run As System)" continueOnFail="false">

    <Parameter xsi:type="StringParameter" name="Parameter1" value="#kworking#\Get-MailboxHTML.ps1"/>

    <Parameter xsi:type="StringParameter" name="Parameter2" value=" -server #computername# -SendEmail -MailFrom jlascari@ml-cc.com -MailTo jlascari@ml-cc.com -MailServer spam.ml-cc.com"/>

    <Parameter xsi:type="StringParameter" name="Parameter3" value="False"/>


  • 1. you are missing the trailing # on your kworking variable in the write file step.

    2. you could probably just execute powershelll command with Parameter2 being: -server $env:computername -SendEmail -etc etc.. this avoids needing to look up the computername from registry. You could also use a built-in Kaseya database field - in this case #vMachine.ComputerName#  - see here for more examples help.kaseya.com/.../9050000

  • With out knowing the particulars, if you are trying to pass the computer's name, pass the Kaseya variable, #vMachine.machName#.

    Assuming that "Force machine ID to always be computer name" has been set true, for the org, in which the computer resides, then #vMachine.machName# should be the same string as the computer's "short name". The NetBIOS name in Windows speak. In Kaseya, check System, Sys Prefs, Naming Polic,  for the setting above.

  • So I haven't had a chance to try your suggestions yet. I will say that our Kaseya Machine ID rarely matches the Computer Name so that variable will not work. I'll try the $env:computername inside the parameter2 next and hopefully get some action.

  • Why don't you just create C:\temp folder, copy there your .ps1 script and execute it with shellcommand?

    Command itself can look something like this: "cmd.exe /c powershell -ExecutionPolicy Bypass -file path to your script"