Kaseya Community

Executing Powershell commands with quotes

  • [I Previosly posted this in the wrong forum - apologies for the cross/double=post]

    Hi,

    We're currently on Kaseya VSA 6.5 (upgrading to 9 soon) an I've done a lot of work recently with Powershell.  I've not had much trouble using ps1 files with Kaseya as it seems to work beautifully.  but today I was trying something that I thought would be simpler, and it seems it's not.  I'm trying to create a procedure to clear out different temp files related to accounts & browsers.  I wasn't having much trouble until I tried to run  this command through Kaseya:

    Remove-Item "$env:LOCALAPPDATA\Google\Chrome\User Data" -recurse -force

    This command works fine when I run it through a PS shell, but when I try to run int using "Execute Powershell Command (64-bit, Run As User)" I always get an error thrown (using the #global:psresult# variable, I can see it) sayiong that "Data" is not a valid argument - which is true.  the trouble is, "Data" is part of a file path, and is contained within quotes.  So it shouldn't be seen as an argument, but pat of that path.  what I notice when I read further into the error is that the quotes I put around that path are not being read by the command.  See:

    I did an export of my AP, and this is the portion for the line in question:

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

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

    <Parameter name="Parameter2" value="Remove-Item "$env:LOCALAPPDATA\Google\Chrome\User Data" -recurse -force" xsi:type="StringParameter"/>

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

    </Statement>

    My question is - do I need to do something special to escape the quotes or the space in the path name?  I cannot believe that Kaseya's AP would be unable to parse things like commands that use paths with spaces, considering so many of Windows' paths have them (Program Files, My Documents, etc)

    Thanks,

    -=Justin=-

  • I will admit that my PowerShell skills mainly consists of splicing bits and pieces I find online together, I tried to figure this out as my boss wants me to do more Powershell stuff.

    I think the problem that you running into is to do with the double quotes, the inbuilt Kaseya procedure Powershell commands also uses double quotes when it executes your powershell  command.

    You can fix this by using single quotes or doubling up on double quotes or using the ASCII code for the double quotes.

    The next problem that I got stuck on is the environment variable would not work after I fixed the quotes issue.

    You could cheat and use a standard command shell RMDIR command to do the same thing which is much simpler and works;

    IF EXIST "%LOCALAPPDATA%\Google\Chrome\User Data" RMDIR /Q /S "%LOCALAPPDATA%\Google\Chrome\User Data"

    Now the bigger problem that I can see with this commandshell command and your powershell command is that they will work if the person is currently logged on. If nobody is logged on to the target computer it will try and run against the System user which would not have the path specified.

    So to fix this you would need to create a more complicated script that would recurse through each user profile and clear the target chrome user data folder.

    Alternatively you could save some time and use the portable version of CCleaner to do the same thing and you can make it run at user logon.



    typo
    [edited by: HardKnoX at 4:22 PM (GMT -7) on Sep 24, 2015]
  • The tip for using single quotes instead of double quotes in the powershell command was very helpful.  Thank you.