Flufy
Flufy

Reputation: 319

Script runs manually but not via c#

I wrote a simple console app, the app runs with administrator privilege, the code is based on this link, the user inputs a .ps1 (powershell) script path and the app execute this script that in this path.

First I tried the app on "Hello World" script and it works fine, but when I try other script, the powershell gives an error.

I have this in the .ps1 scipt:

install-WindowsFeature smtp-server
Read-Host -Prompt “Press Enter to exit”

The surprising thing is that if I run this command install-WindowsFeature smtp-server manually it works fine.

but I need that the app would do it, and not manually.

Here is the powershell error:

install-WindowsFeature : The term 'install-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.
At c:\users\administrator\desktop\EnableSMTP.ps1:1 char:1
+ install-WindowsFeature smtp-server
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (install-WindowsFeature:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Press Enter to exit:

Upvotes: 1

Views: 776

Answers (3)

Nehorai Elbaz
Nehorai Elbaz

Reputation: 2452

This happened to me in the past.

In solution explorer, right click on the project-->properties-->Build

Channge platform target from "Any CPU" to x64

enter image description here

Upvotes: 3

jbsmith
jbsmith

Reputation: 1666

Install-WindowsFeature is a cmdlet provided by the ServerManager module. When you run the script manually, you must have this module imported into your session already. When run via the program, the module is not imported and therefore it does not recognize the cmdlet name. Try adding Import-Module ServerManager as the first line in your script and see if that resolves your problem.

Upvotes: 1

Duncan Carr
Duncan Carr

Reputation: 250

I think you might need a capital "I" ... Install-WindowsFeature

Upvotes: -1

Related Questions