Solved

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

Posted on 2010-09-08
5
1,866 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
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 38

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

14 Experts available now in Live!

Get 1:1 Help Now