Run .PS1 script with task

I simply want to run an existing powershell script from a task. The .PS1 is located on each PC I want to run it on and each PC has the correct shell version I need. I just can’t get it to execute.
I’ve visited most of the past posts on this but they seem way to complicated to simply run a script. Can this not be done the same way that you could run a batch script using a distribution package or something like that?

For scenario sake, lets say the script is located C:\psscript.ps1

Thank you

Actually I figured this out. I used software distribution and in the installation command put

powershell.exe -file “script.ps1”

I had to actually include powershell.exe in the distribution package because Powershell is one more layer deep than the default system folder.

You can run a powershell script in the same general way that you would run a BAT script. Basically anything you can do on the command line as the SYSTEM account should work in actionscript, with a few rare exceptions.

I would not recommend including the powershell exe and I would not recommend uploading a PS1 file and downloading it to the clients to run in most cases. You can use the absolute path to powershell.exe but you can also dynamically query the absolute path using relevance, like the following:

See this: http://bigfix.me/fixlet/details/3860

waithidden { pathname of file ((it as string) of value "Path" of key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" of native registry) } -ExecutionPolicy Bypass -File powershell.ps1

Related:

Powershell Help
Run Powershell Command in Bigfix

This worked absolutely perfectly! Geesh, I went about that all wrong didn’t I…

Building on this, here is my PS script:

Get-Childitem C:* -recurse -include *.prt, *.asm, *.dft, *.pwd, *.dwg | select fullname,length | export-csv C:\CADfiles.csv -notype

This script works perfectly with no issues in that fixlet. However whenever I enter another pipe with {}",it fails when it attempts to create the file. I’m assuming because these {} mean something different when encountered. How can I work around this. THis is ironically the most important part of this script to ensure it gathers the correct info. (There is a '' in C:* but it’s not showing up, so ignore that)

Get-Childitem C:* -recurse -include *.prt, *.asm, *.dft, *.pwd, *.dwg | where {$_ -notmatch ‘Siemens’} | select fullname,length | export-csv C:\CADfiles.csv -notype

You have to escape curly braces in actionscript because otherwise it tells the client to do relevance substitution in that area, which it will fail to do because that isn’t proper relevance inside the braces and that is not what you want it to do.

Try this:

Get-Childitem C:\* -recurse -include *.prt, *.asm, *.dft, *.pwd, *.dwg | where {{$_ -notmatch 'Siemens'} | select fullname,length | export-csv C:\CADfiles.csv -notype

Also, in general, it is not a great idea to recursively search all locations of the hard drive. This could take quite a while to execute and cause a lot of load on the system that could disrupt users. This should definitely be run in a low priority as well to help mitigate this. Do you really need to search the entire file system, or could you restrict your search to a particular set of folders? You should exclude the C:\Windows folder at least.

So add this to the beginning:

action launch preference low-priority

Related: BES Client Setting: CPU Usage with Solaris
Example: http://bigfix.me/fixlet/details/3967

1 Like