It might be really handy to run a Powershell script on your clients for doing a broad range of tasks.
In the past, vbscript was used to perform these tasks but considering to use powershell for the same tasks might be interesting
The big advantage of using Powershell is that it’s been built on top of .NET which means that it utilizes the base classes and is capable of interacting with some applications that cannot be manipulated using vbscript. Powershell also supports a wide range of cmdlets that facilitate tasks that might be difficult to perform when using vbscript.
Running a Powershell script using a package + program within SCCM 2007 ( or Configuration Manager 2012) can be done following the steps below:
- Create a package which contains the script you want to run eg. myscript.ps1
- Create a program for that package that sets the ExecutionPolicy of Powershell to unrestricted. This can be done by using this commandline: “powershell set-ExecutionPolicy Unrestricted -force” (without quotes)
- Create another program which runs the script that contains all the actions that need to be done on the client machines. In this example the command line should be: “powershell .\myscript.ps1” (again without the quotes). Be sure to add the “.\”-part because it doesn’t seem to run without that.
- Create a last program which sets the Executionpolicy back to restricted (or remotesigned) by using the commandline: “powershell set-ExecutionPolicy Restricted -force” (once again without the quotes)
Now link the 3 programs together by using the “Run program first” checkbox from the program wizard in SCCM as follows:
- Program mentioned at 4. has a run program first which points to the program mentioned at 3.
- Program mentioned at 3. has a run program first which points to the program mentioned at 2.
As a final step create an advertisement (or deployment in ConfigMgr 2012) which uses the program mentioned at 4. and target all devices needed.
This should do the trick.
I can imagine there are other ways of achieving the same, feel free to comment with your opinion about this.
EDIT: Running a Powershell script should also be possible by using the following commandline as a program: “Powershell.exe -ExecutionPolicy Bypass -file .\myscript.ps1” (without quotes). This is a lot easier then using the 3 programs mentioned above, I’ll try and test this as soon as possible and get the result back at you guys.
EDIT2: the method of using “Powershell.exe -executionpolicy Bypass -file .\myscript.ps1” seems to work.