I am trying to run powershell scripts as post installation tasks on the kace 2000.

I add the scripts as applications and give the folowing parameters
powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file "script.ps1"

The above comes from this: http://www.itninja.com/question/how-to-run-a-powershell-command

During installation all the task will fail, and by examining the xml file i see the problem is the path where the command is run.
The path is something like c:\kace\task_name\powershell.exe and it should be C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

I tried to add this to the parameters like this:
"C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe" -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file "script.ps1"

But this just gave the following path in the XML file
c:\kace\task_name\C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

How should this be fixed?


Bok








Answer Summary:
Cancel
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Answer Chosen by the Author

1
I have found the most reliable method is to create a zip archive that contains the ps1 file and a batch script. The batch script just runs powershell. I then call the batch script as the parameter for the post-install task.

Answered 06/28/2016 by: chucksteel
Red Belt

  • Thank you, this seem to work.
Please log in to comment

Answers

1

best way to trouble shoot is look at the tasks.xml to see how it is formatted.


by entering this for the parameter:
powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file "script.ps1"

you get

<Task ID="482">
<Name>powershell example</Name>
<WorkingDirectory>%systemdrive%\KACE\Applications\482</WorkingDirectory>
<CommandLine><![CDATA["%systemdrive%\KACE\Applications\482\powershell.exe" -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file "script.ps1"]]></CommandLine>
<Parameters></Parameters>
<PostTaskAction>None</PostTaskAction>
<KACETaskType>Application</KACETaskType>
<FileType>Exe</FileType>
<Type>PO</Type>
<Guid>1577137778ebd2</Guid>
</Task>

By adding double quotes around the entire command:

"powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file script.ps1"

you get

<Task ID="482">
<Name>powershell example</Name>
<WorkingDirectory>%systemdrive%\KACE\Applications\482</WorkingDirectory>
<CommandLine><![CDATA["powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file script.ps1"]]></CommandLine>
<Parameters></Parameters>
<PostTaskAction>None</PostTaskAction>
<KACETaskType>Application</KACETaskType>
<FileType>Exe</FileType>
<Type>PO</Type>
<Guid>15771389517259</Guid>
</Task>

just make sure you have no spaces in the ps1 name.


Answered 06/27/2016 by: SMal.tmcc
Red Belt

  • Great! Thanks. I won't be able to test it out till tomorrow as im off work now.
    Yet i am thinking, whn i call iy this way, will poweshell be able to locate the script file?
    What i mean is poweshell is opened from system32 folder and script is located in /kace/application folder
    • this takes care of that

      <WorkingDirectory>%systemdrive%\KACE\Applications\482</WorkingDirectory>
      • it is executing in this dir and calling to system via path search
      • Thanks for helping out
  • The quotations partly fixed the problem.
    Now the new problem is that the command is run with the quotations, which also results in failure.
    I made the task pass by editing the XML manually from this:
    <CommandLine><![CDATA["powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file script.ps1"]]></CommandLine>

    to this:
    <CommandLine><![CDATA[powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file script.ps1]]></CommandLine>

    How do i tell kace not to include the working dir, and not to include quotetaions?
Please log in to comment
Answer this question or Comment on this question for clarity
Admin Script Editor
Admin Script Editor is an integrated scripting environment available free here at ITNinja

Share