Solved

Invoking powershell command

Posted on 2008-06-26
6
5,245 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 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