Solved

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

Posted on 2010-09-08
5
1,859 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
  • 3
  • 2
5 Comments
 
LVL 38

Expert Comment

by:Adam Brown
Comment Utility
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 38

Expert Comment

by:Adam Brown
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
     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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.
This article will help you understand what HashTables are and how to use them in PowerShell.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

13 Experts available now in Live!

Get 1:1 Help Now