Solved

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

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

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 40

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
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…

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