locked
Powershell, Exchange 2010 and PS1 dilemma RRS feed

  • Question

  • Hi,
     
    I am trying to execute a ps1 script (prepare-moverequest.ps1) using c# and exchange 2010 remote powershell. If I run the file locally on a development machine which has the Exchange 2007 management tools installed it executes the PS1 file but when i try and do it remotely I always get the error *The term 'Prepare-MoveRequest.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program*. I have tried to put explicit paths when I add the command below i.e. c:\prepare-moverequest.ps1, .\prepare-moverequest.ps1 etc but nothing seems to work.
            using (Runspace ShellRunSpace = RunspaceFactory.CreateRunspace(new WSManConnectionInfo(connection.powershellURI, connection.Endpoint, connection.credential)))
            {
              ShellRunSpace.Open();
              if (ShellRunSpace.RunspaceStateInfo.State == RunspaceState.Opened)
              {
                using (Pipeline ExchangePipeLine = ShellRunSpace.CreatePipeline())
                {
                  ExchangePipeLine.Commands.Add(Command.Command);
                  RetVal = ExchangePipeLine.Invoke();
                  if (ExchangePipeLine.Error != null && ExchangePipeLine.Error.Count > 0)
                  {
                    throw new PowershellException(ExchangePipeLine.Error.ReadToEnd().ToString());
                  }
                }
              }
            }
    
    I can connect and execute built in cmdlets fine but just can't seem to execute ps1 scripts. Should I be reading the script in and then adding it to the command and setting isScript = true? If I do this how do I set the params for the ps1 script?

    Does anyone know why this wouldn't be working or am i doing something wrong? Any ideas welcome :)
     
    If anyone wants to see the script it can be found here http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17741
     
    Thanks,
    Niall
    Wednesday, August 3, 2011 2:50 PM

Answers

All replies

  • Have a look at http://blogs.msdn.com/b/akashb/archive/2010/03/26/how-to-call-ps1-script-from-managed-code-using-remote-powershell-exchange-2010.aspx which gives a sample or using reading the script in and then using AddScript to execute it in a remoting session.

    Cheers
    Glen

    • Marked as answer by GrayNiall Thursday, August 4, 2011 11:08 AM
    Thursday, August 4, 2011 7:30 AM
  • Thanks for the link Glen, helped a lot. Do you know how I can pass parameters to the script?

    I tried to use the SetVariable but doesn't seem to work.

    Below is the params that the script requires.

    param([parameter(Position=0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, mandatory=$true)][string]$Identity, 
       [parameter(Position=1, mandatory=$true)][string]$RemoteForestDomainController, 
       [parameter(Position=2, mandatory=$true)][Management.Automation.PSCredential]$RemoteForestCredential, 
       [string]$LocalForestDomainController,
       [Management.Automation.PSCredential]$LocalForestCredential,
       [string]$TargetMailUserOU,
       [string]$MailboxDeliveryDomain,
       [switch]$LinkedMailUser,
       [switch]$DisableEmailAddressPolicy,
       [switch]$UseLocalObject,
       [switch]$OverwriteLocalObject)
    
    

    Any ideas. Looked around on the interent but had no luck.

    Cheers

    Niall

    Thursday, August 4, 2011 9:52 AM
  • not to worry, figured it out. Used powershell.AddParameter to add all necessary parameters.

    Thanks again.

    Thursday, August 4, 2011 11:08 AM
  • Hi Niall

    Is there any chance you can provide us with an example of the code that you ended up with.

    Im having an issue trying to run the Prepare-MoveRequest script from a client remotely. Issue seems to occur when passing the PSCredential for -LocalForestCredential & RemoteForestCredential, but im not too sure. I'd like to verify it against somebody elses code who's had success with it if possible.

    Wednesday, January 18, 2012 7:58 AM
  • Hi Daniel,

    I will see if I can find the code we used in the end.

    Niall

     

    Thursday, January 19, 2012 5:48 PM
  • Hi Niall

    I dont suppose you managed to find the code? I myself am having a dilemma, dont really want to face migrating 1600+ mailboxes manually.

    Thanks

    Monday, January 23, 2012 2:03 PM
  • Hmmm.. the link above didnt really seem to help. 

    I wonder if this is a general security issue using WinRM rather than the powershell itself?

    Thursday, February 9, 2012 3:43 PM