Due to the diversity of target agents that I end up working with, and taking a "least amount of change" approach, here's what I have found to be the most reliable and effective way of calling PowerShell scripts. With this method, you will never have to change the execution policy, as Martyn has mentioned:
LabTech script functions:
1. FILE WRITE function - Write out the contents of the PowerShell script you'd like to execute against the target agent. This enables you to use all of your LabTech expansion variables, and you can also use the latest version of PowerShell that is on that remote agent. Unfortunately at the current point in time, the "PowerShell Command / PowerShell Command as Admin / Execute Script" functions will only natively support PowerShell 2.0 and below.
2. SHELL / SHELL as Admin function - Use a shell command to execute the file you've just written down to the target machine. The following command will accomplish this:
%windir%\system32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy ByPass -File %windir%\temp\MyPowerShellFile.ps1 -NonInteractive
You can also execute commands inline should you only need to run a few commands by switching the -FILE parameter over to a -Command parameter.
%windir%\system32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy ByPass -NonInteractive -Command "get-service | ? {$_.Status -eq 'Stopped' -AND $_.DisplayName -like 'LTAge*'} | start-service;"
Hope this helps clear up some of the confusion out there. I'll have a bunch more information for you guys at Automation Nation!
Another note: If you're using the PowerShell Command / PowerShell Command as Admin / Execute Script functions, you need to convert your write-host statements to write-output for capturing the standard output. If you call the PowerShell script via the aforementioned PowerShell.exe method, you can leave the scripts as is. Execute Script is definitely a cool new function, and is surely a good method if you've got a quick script that needs executing.
