{"id":691,"date":"2011-03-14T20:57:23","date_gmt":"2011-03-14T20:57:23","guid":{"rendered":"http:\/\/www.kozeniauskas.com\/itblog\/?p=691"},"modified":"2011-05-26T10:50:46","modified_gmt":"2011-05-26T09:50:46","slug":"powershell-command-would-not-run","status":"publish","type":"post","link":"http:\/\/www.kozeniauskas.com\/itblog\/2011\/03\/14\/powershell-command-would-not-run\/","title":{"rendered":"Powershell command would not run"},"content":{"rendered":"<p>Here is my first Powershell problem. I&#8217;m not good in powershell as I don&#8217;t use it too often but ussually i find my way pretty quickly. Well except this time&#8230;<br \/>\nSo here is my problem:<br \/>\nI have old Altiris Deployment server that I use for deploying server images. When I&#8217;m imaging 2008R2 servers I need to install server Roles and Features. Powershell here comes pretty handy as I only have to use <strong>Add-WindowsFeature<\/strong> command plus a list of Roles or Features that I want to add.<br \/>\nI created a file called powershell.ps1 that adds all the roles and features that I need plus does some configuration. I ran <strong>powershell -command powershell.ps1<\/strong> on the test server to see if everything is installed and configured as expected. Everything went OK.<br \/>\nSo next in Altiris I create a new job that copies a powershell.ps1 file and then runs it.<!--more--><br \/>\nDuring server deployment the job ran OK but when I&#8217;ve logged in I&#8217;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&#8217;ve check if the script was actually copied and it was, then I&#8217;ve tried to run it manually and it ran OK.<br \/>\nI&#8217;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&#8217;s starting but again nothing happened.<br \/>\nTo diagnose the problem I&#8217;ve added logging in the script using <strong>start-transcript<\/strong> 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&#8217;ve found the following error:<br \/>\n<em>The term &#8216;Add-WindowsFeature&#8217; 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.<\/em><\/p>\n<p>This cannot be right as the command runs ok when I enter it manually. Next I&#8217;ve added a simple <strong>Get-Date<\/strong> 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&#8217;ve realized that command <strong>Add-WindowsFeature<\/strong> must be a module specific\u00a0 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&#8217;ve added one line at the top\u00a0<strong>Import-Module ServerManager<\/strong> and after this everything was running as expected.<\/p>\n<p>Hope this post will save time for someone.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is my first Powershell problem. I&#8217;m not good in powershell as I don&#8217;t use it too often but ussually i find my way pretty quickly. Well except this time&#8230; So here is my problem: I have old Altiris Deployment server that I use for deploying server images. When I&#8217;m imaging 2008R2 servers I need [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[363],"_links":{"self":[{"href":"http:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/posts\/691"}],"collection":[{"href":"http:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/comments?post=691"}],"version-history":[{"count":4,"href":"http:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/posts\/691\/revisions"}],"predecessor-version":[{"id":695,"href":"http:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/posts\/691\/revisions\/695"}],"wp:attachment":[{"href":"http:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/media?parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/categories?post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/tags?post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}