Kaseya Community

Help with Powershell Issue

  • I have a powershell script that is checking the existence of a Custom system variable. When i run the script via kaseya, it always sees it as empty or null. If i try checking for the variable $env:computername, that behaves correctly but if i try $env:MyCustomVariable it does not.

    I have run the script locally as admin and that behaves as expected because MyCustomVariable does exist and has a value. Ive even tried connecting to cmd va psexec as system and runing the ps1 file from there, works fine. 

    It seems to be only when kaseya runs the script as a procedure that it doesnt seem to behave.

    Script is below

    If ([string]::IsNullOrEmpty($env:MyCustomVariable)){
    New-Item -Path 'C:\empty.txt' -ItemType File
    }
    Else{
    New-Item -Path 'C:\not-empty.txt' -ItemType File
    }

    In this situation, kaseya procedure would create c:\empty.txt but running it manually would create c:\not-empty.txt

  • In your procedure are you using use credential? or run as user/system?

  • The agent runs in the System context so it will not have access to any environment variables that are not set at the system level.  Your options are to create the environment variable at the system level or to execute your script in the same context (and shell) as the account the variable belongs to.

  • I'm running the procedure as System

    the variable has been set as a system variable

  • You could query the variable through the registry, as at the end of the day that is where it lives. If the PS is being problematic, you could do the query through the kaseya procedure itself with the GetRegistryValue / Get64BitRegistryValue commands, and then pass off the results to your ps1 file.



    Link was malformed
    [edited by: qvanorder@swatsystems.com at 11:26 AM (GMT -7) on Jun 7, 2019]