Powershell command would not run

14 Mar

Here is my first Powershell problem. I’m not good in powershell as I don’t use it too often but ussually i find my way pretty quickly. Well except this time…
So here is my problem:
I have old Altiris Deployment server that I use for deploying server images. When I’m imaging 2008R2 servers I need to install server Roles and Features. Powershell here comes pretty handy as I only have to use Add-WindowsFeature command plus a list of Roles or Features that I want to add.
I created a file called powershell.ps1 that adds all the roles and features that I need plus does some configuration. I ran powershell -command powershell.ps1 on the test server to see if everything is installed and configured as expected. Everything went OK.
So next in Altiris I create a new job that copies a powershell.ps1 file and then runs it.
During server deployment the job ran OK but when I’ve logged in I’ve noticed that none of the roles were installed. The user that the script is running under has local admin rights on the server. Powershell execution policy is set to Remote Signed but that should not have any affect as the scripts are running from the local machine. I’ve check if the script was actually copied and it was, then I’ve tried to run it manually and it ran OK.
I’ve removed all the changes that the script did and ran it again from Altiris server. In task manager I saw powershell.exe process popping quickly, so I know that it’s starting but again nothing happened.
To diagnose the problem I’ve added logging in the script using start-transcript command that dumped all the output of what was happening when the script was running. I ran the script from deployment server again and checked the log file, to my surprise I’ve found the following error:
The term ‘Add-WindowsFeature’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

This cannot be right as the command runs ok when I enter it manually. Next I’ve added a simple Get-Date command to the script to see what is going to happen. When the script ran I got an output of the date in the log file. This is when I’ve realized that command Add-WindowsFeature must be a module specificĀ  and I must to import Server manager module before I could use it. The module is imported automatically when I was running command manually bu this is not the case when this is done using batch file. So in my script I’ve added one line at the topĀ Import-Module ServerManager and after this everything was running as expected.

Hope this post will save time for someone.

Tags:

2 Responses

  1. Wayne says:

    We are trying this also but without success. We’re doing this from an Altiris NS and running the task under system account. It seems there is something with running a powershell script (particularly Import-Module) under system account that makes it fail.

    I get an error stating to the effect there were no valid modules to load. When in fact the modules are there and the PSModulePath variable is valid.

    Any thoughts as to running powershell scripts under system account on Windows 2008 would make this fail?

    • Andrius says:

      Hi Wayne,
      I also run the script under systme account and it working fine. Here is how I do it.
      I have a job with 2 tasks in Altiris. First task copies the PS script onto the client to c:\temp. The second task is CMD line that runs PS script by calling it ‘powershell -noninteractive -command c:\temp\script.ps1’

Leave a Reply

IT Blog

Just another blog on Kozeniauskas.com Network