Solved

Invoking powershell command

Posted on 2008-06-26
6
5,222 Views
Last Modified: 2010-04-21
HI Experts

Please see code snipped , I am trying to call a powershell command from C# and got the following error:


"Cannot invoke this function because the current host does not implement it."

I have followed http://forums.microsoft.com/TechNet/ShowPost.aspx?PostID=2365137&SiteID=17 , but still have the same error.

Thanks
// create Powershell runspace

            Runspace runspace = RunspaceFactory.CreateRunspace();

            // open it

            runspace.Open();         

            Command cmd = new Command("Get-Credential");

            cmd.Parameters.Add("Credential", "user");

            // create a pipeline and feed it the command

            Pipeline pipeline = runspace.CreatePipeline();

            pipeline.Commands.Add(cmd);

            // execute the commnand

            Collection<PSObject> results = pipeline.Invoke();

            // close the runspace

            runspace.Close();

Open in new window

0
Comment
Question by:elimesika
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:BSonPosh
ID: 21878150
I am not sure what your going for, but Get-Credential is going to want to prompt you for a password and that cannot be passed via parameter.

Why do you want to use Get-Credential?
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 21878270
0
 
LVL 19

Author Comment

by:elimesika
ID: 21892436
HI

thanks for your respond.

I am trying to implement my own Login cndlet which has the following format

Login-Application [-User <user>] [-Password <password>] [-Domain <domain>]

If the user does not give any parameter and runs only

Login-Application

I want to prompt him to enter the user, password, domain manually
I thought that I can use Get-Credentials for that.

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 18

Accepted Solution

by:
BSonPosh earned 500 total points
ID: 21892668
Naaa... You dont need to for this. You can do a prompt like you normally would do in C# and store the creds in a system.management.automation.pscredential.pscredential Object. Tring to use Get-Credentials for this would be overly complicated as you have to handle the prompts.
0
 
LVL 19

Author Closing Comment

by:elimesika
ID: 31470979
Thanks a lot for your time.
0
 
LVL 1

Expert Comment

by:JElster
ID: 33619911
How did you do this?  thanks
store the creds in a system.management.automation.pscredential.pscredential Object. Tring to use Get-Credentials for this would be overly complicated as you have to handle the prompts.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now