Solved

Invoking powershell command

Posted on 2008-06-26
6
5,259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

738 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