Solved

Invoking powershell command

Posted on 2008-06-26
6
5,238 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

810 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