1

I have a multitenant Active Directory setup, and I connect to it remotely from a jumpbox server using PowerShell with the following script:

Relationship diagram

$cred = Get-Credential
$Session = New-PSSession -ComputerName XXXXXX -Credential $cred -Port XXXX
Enter-PSSession $Session

The jumpbox server has the Exchange Server Management tool (MS 0365) installed, and the Active Directories are synchronized with MS O365.

After connecting to PowerShell, I run the following commands to enable a mailbox:

Add-PSSnapin *recipientmanagement
Enable-RemoteMailbox -Identity "SamAccountName" -RemoteRoutingAddress [email protected]

While the above commands work perfectly when run directly from PowerShell. But unable to connect to PowerShell using C#. Need a equivalent code in C#.

Tried below code, need to fix this

// Create a PowerShell pipeline
 PowerShell ps = PowerShell.Create();
 ps.Runspace = runspace;
 // Create a PSSession
 ps.AddCommand("New-PSSession");
 //ps.AddParameter("ConfigurationName", "Microsoft.Exchange");
 ps.AddParameter("ComputerName", "XXXXXXXX");
 ps.AddParameter("Credential", new PSCredential("XXXXXX", ConvertPassword("XXXXXXX")));
 ps.AddParameter("Port", XXXX);
 
 var sessionResult = ps.Invoke();
 ps.Commands.Clear();
 // Add the Session
 ps.AddCommand("Import-PSSession")
 .AddParameter("Session", sessionResult[0]);
 ps.Invoke();
 // Add the *recipientmanagement snap-in
 ps.Commands.Clear();
 ps.AddCommand("Add-PSSnapin")
     .AddParameter("Name", "*recipientmanagement");
 ps.Invoke();
 ps.Invoke();

 // Enable Remote Mailbox
 ps.Commands.Clear();
 ps.AddCommand("Enable-RemoteMailbox");
 ps.AddParameter("Identity", "SamAccountName"); 
 ps.AddParameter("RemoteRoutingAddress", "[email protected]");  
 ps.Invoke();

Error:

at :// Add the Session
System.Management.Automation.CmdletInvocationException: 'Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files.'

at : // Add the *recipientmanagement snap-in
System.Management.Automation.CommandNotFoundException: 'The term 'Add-PSSnapin' is not recognized as a name of a cmdlet, function, script file, or executable program.

at: // Enable Remote Mailbox
System.Management.Automation.CommandNotFoundException: 'The term 'Enable-RemoteMailbox' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'
5
  • Are you running c# from inside Visual Studio or running the executable? Try the executable without Visual Studio or start VS by right click on VS shortcut and select Run As Admin. Commented Aug 13, 2024 at 19:56
  • Using Visual studio 2022 and running as Admin. Commented Aug 14, 2024 at 10:01
  • Did you see following : stackoverflow.com/questions/4037939/… Commented Aug 14, 2024 at 12:19
  • Set-ExecutionPolicy RemoteSigned was ran manually on the remote Server and allowed all remote session. Commented Aug 14, 2024 at 12:36
  • Why use powershell for this at all? C# can use the ms graph api or AD directory services objects directly Commented Aug 15, 2024 at 21:52

1 Answer 1

1

Preface:

  • The answer below explains the errors you encountered and provides general tips on how to fix them.

  • However, I strongly suspect that there's a more modern approach to connection remotely to an Exchange server, as detailed in the Connect to Exchange servers using remote PowerShell help topic.

    • If you use this approach, you will still have to set the local execution policy via the PowerShell SDK, as mentioned below and as detailed in this answer.

at :// Add the Session
System.Management.Automation.CmdletInvocationException: 'Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files.'

This suggests that calling Import-PSSession - which uses implicit remoting to import commands from a remote session into your local session via local proxy functions - involves local execution of files that are subject to PowerShell's execution policy.

You must therefore configure your C# project to permit script execution, via setting up an initial session state that includes the desired execution policy, as described in this answer.

However, note that passing only a -Session argument to Import-PSSession will try to import all commands from the remote session, which is not your intent. Instead, constrain import to the commands of interest via the -Module and/or -Command parameters.

at : // Add the *recipientmanagement snap-in
System.Management.Automation.CommandNotFoundException: 'The term 'Add-PSSnapin' is not recognized as a name of a cmdlet, function, script file, or executable program.

Note that you're trying to run this command locally, whereas your intent is to run it on the remote server. To do the latter, you must call Invoke-Command with your session passed to -Session and a -ScriptBlock argument that performs your Add-PSSnapin call.

As an aside, the unavailability of Add-PSSnapin locally implies that you're using a PowerShell (Core) 7 SDK version, because said command isn't part of PowerShell 7 anymore.

at: // Enable Remote Mailbox
System.Management.Automation.CommandNotFoundException: 'The term 'Enable-RemoteMailbox' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'

This is merely a follow-on error, resulting from the fact that your Import-PSSession call failed and that no proxy functions were therefore created for cmdlets such as Enable-RemoteMailbox.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.