Solved

Cannot bind parameter 'LinkedCredential'. Cannot convert the "System.Management.Automation.PSCredential"

Posted on 2010-09-08
5
1,885 Views
Last Modified: 2012-05-10
Hi..
I'm trying to run a script from C#.

the script looks like this...

     new-mailbox -database abc -name test1 -linkeddomaincontroller abc1
-linkedmasteraccount abc\test02 -organizationalunit "ou=XYZ" -userprincipalname test02@test.com -linkedCredential:(Get-Credential  tester1)


 Using the following code.

   
            RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
            PSSnapInException snapInException = null;
            PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
            Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
            myRunSpace.Open();


            Pipeline pipeLine = myRunSpace.CreatePipeline();
            Command myCommand = new Command("Get-Command");
            pipeLine.Commands.Add(myCommand);
            Collection<PSObject> commandResults = pipeLine.Invoke();


            // create a pipeline and feed it the script text
            Pipeline pipeline = myRunSpace.CreatePipeline();
            pipeline.Commands.AddScript(_scriptText);


            pipeline.Commands.Add("Out-String");

            // execute the script
            Collection<PSObject> results = pipeline.Invoke();


But I get the above error.. how can I pass in the Credentials / Object ?  
thanks!

0
Comment
Question by:JElster
[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
5 Comments
 
LVL 41

Expert Comment

by:Adam Brown
ID: 33632034
Like this:

$credential = get-credential tester1
new-mailbox -database abc -name test1 -linkeddomaincontroller abc1
-linkedmasteraccount abc\test02 -organizationalunit "ou=XYZ" -userprincipalname test02@test.com -linkedCredential $credential

Open in new window

0
 
LVL 41

Expert Comment

by:Adam Brown
ID: 33632050
Of course, the new-mailbox command and switches need to be on a single line for it to work :D
0
 
LVL 1

Author Comment

by:JElster
ID: 33635907
How do I pass this to my _scriptText var?

like this?

_scriptText = "$credential = get-credential tester1

                      new-mailbox -database abc -name test1 -linkeddomaincontroller abc1...";


On 2 lines?  

thanks
0
 
LVL 1

Author Comment

by:JElster
ID: 33636682
or..

I have to build the script and pass it in.....

1.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
2.        PowerShell powershell = PowerShell.Create();
3.        PSCommand command = new PSCommand();
4.        command.AddCommand("New-PSSession");
5.        command.AddParameter("ConfigurationName", "Microsoft.Exchange");
6.        command.AddParameter("ConnectionUri", new Uri(liveIdconnectionUri));
7.        command.AddParameter("Credential", cred);
8.        command.AddParameter("Authentication", "Basic");
9.        powershell.Commands = command;
0
 
LVL 1

Accepted Solution

by:
JElster earned 0 total points
ID: 33730633
     string initString = "Qwerty123";
                         SecureString secString = new SecureString();
                         foreach (char ch in initString)
                             secString.AppendChar(ch);
                       
                        PSCredential cred = new PSCredential("XX\\JSmith",secString);
                       
                        thisPipeline.Commands[0].Parameters.Add("linkedCredential", cred);
0

Featured Post

IoT Devices - Fast, Cheap or Secure…Pick Two

The IoT market is growing at a rapid pace and manufacturers are under pressure to quickly provide new products. Can you be sure that your devices do what they're supposed to do, while still being secure?

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

628 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