Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-09-08
5
Medium Priority
?
1,894 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 42

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 42

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
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…

721 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