• Skip to primary navigation
  • Skip to main content
Carbon60

Carbon60

The Managed Cloud Difference

  • Show Search
  • Contact Us
  • Get Started
Hide Search
  • Cloud Adoption
    Cloud Adoption

    Starting or continuing on your cloud journey — whether public, private or hybrid — is a complex undertaking. But no matter your company size or industry, our cloud consulting experts can help with end-to-end solutions to plan, migrate and operate your business in the cloud.

    • Cloud Readiness Assessment

      Chart a new course for your IT environment with a Cloud Readiness Assessment. With a proven process, we take into consideration your technology, people and business strategy and tailor a public, private or hybrid cloud environment that will set your organization up for success.

      Learn more
    • Cloud Migration Services

      Cloud migration can transform your business and give you a competitive edge – when done properly. Our cloud experts will help you move complex workloads to the right cloud environment, the right way – tailored for your specific needs.

      Learn more
    • Cloud Launchpad
    • Modernization
    • Cloud Security & Compliance
  • Managed Cloud
    Managed Cloud

    Get the most out of the cloud and keep your IT team out of the weeds. Gain predictability and control around security, compliance, agility, reliability, performance – and cost – by tapping into our standard-setting managed cloud services.

    • Managed Private Cloud

      Safe, stable, fast, compliant, secure and fully managed – get a reliable and data sovereign cloud infrastructure platform for your applications.

      Learn more
    • Managed Public Cloud

      Experience matters. Whether you choose Managed AWS, Azure or Google Cloud, we can take care of the heavy lifting while you focus on your business.

      Learn more
    • Cloud Backup
    • Cloud Disaster Recovery
    • Security
    • Managed Public Cloud
    • Cloud Disaster Recovery
    • Applications
  • Industries
    Industries

    When it comes to financial services, healthcare, public sector and technology – there is simply no room for error or uncertainty when it comes to data security and compliance. There are specific and important considerations that we are well-versed in navigating. We’re trusted by governments and organizations to safely house mission-critical functions every day.

    • Financial Services

      Increase go-to-market speed, while meeting SOC2, PCI-DSS and OSFI B10 regulatory and organizational obligations.

      Learn more
    • Healthcare

      Get secure, scalable high-performance data, while improving the patient experience and addressing every compliance and privacy requirement.

      Learn more
    • Public Sector
    • Technology
  • Partners
    Partners

    Simply put – the right technology and the right platform is the one that’s right for your business. We’re highly certified and experienced in the major public clouds – so regardless of complexity, customization or preference – we’re well-equipped to have your back every step of the way.

    • AWS

      AWS Premier Partner with 100+ AWS certifications and counting.

      Learn more
    • Microsoft Azure

      Gold Microsoft Azure Partner with core competencies and certifications.

      Learn more
    • Google Cloud
    • VMware
  • Insights
    Insights

    Sharing knowledge and expertise is a big part of how we’ve evolved - and how we help our customers.

    • Blog

      Stay up to date with the latest trends and developments in the fast-moving world of digital transformation.

      Read
    • Events

      Make sure to join us for our next event and connect with cloud experts who have a lot to share.

      Attend
    • Resources

      Your toolkit to do a deeper dive with case studies, info sheets, checklists and more.

      Explore
  • About
    About

    We’re on a mission to bring digital transformation to more businesses, by making forward-thinking cloud strategy – and high-performance cloud services – more accessible.

    • Leadership

      Meet the leadership team who are driving our vision forward.

      Learn more
    • About Carbon60

      Learn more about who we are, and how we help our customers evolve with confidence.

      Learn more
    • Careers
    • News
  • Contact Us
  • Get Started

Remotely Executing “Real” Exchange 2010 SP2 PowerShell Scripts

July 29, 2012

You can probably find several blog posts out there about remotely executing simple commands and scripts against Exchange servers, but trying to implement these examples on a “real” functional script, can introduce some annoying problems that nobody seems to mention. This was the case when I tried to develop a web page that was supposed to assist with managing a multi-tenant exchange 2010 SP2 environment. I got some startup scripts from Jacob Dixon’s blog and after some minor modifications I was able to execute them locally on the Exchange server without any problems, it was only when I tried to call these scripts from my web page that problems started:

Problem 1:  Following a TechNet library post (http://technet.microsoft.com/en-us/library/dd335083), I tried, firstly, to open a remote PowerShell session from a C# application, to the Microsoft.Exchange configuration on a CAS server. I connected successfully but found that the execution context was very limited and restricted as it didn’t even allow assigning PowerShell variables, meaning you almost can’t do any scripting with it.

Problem 2:  So I tried connecting to the default PowerShell  WSMAN (http://{ServerName}:5985/wsman) and executing the script, only to find that although I have full permissions, when trying to execute an exchange’s command, it asked to provide the server context as it didn’t recognize it executing on an exchange server.  I didn’t want to change my scripts, so I didn’t continue with this approach.

Problem 3:  So I tried to open a local PowerShell session and from this session I used the Import-Session commandlet to connect to the remote Microsof.Exchange configuration.  I connected successfully and had full permissions with the right execution context.  However,  while re-trying to execute the script I found that the commandlet New-Mailbox doesn’t exist.   Quick solution:  Executed the “Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010” command and magically the command is available.

Problem 4:  Trying again to execute the script, I got a new error: “New-Mailbox : Load balancing failed to find a valid mailbox database.” Ok – so I tried to specify the database explicitly with the –Database argument and got this error: “New-Mailbox : Couldn’t find database “{DB Name}”. Make sure you have typed it correctly. ”. I googled it and found some forums that suggested to make sure the database exists and is mounted, of course my mailbox database did exist and was mounted as the script worked from the local exchange server.  So I tried a different approach.  I  opened a remote PowerShell session to the default PowerShell WSMAN and from that remote session I imported a new session to the Microsoft.Exchange configuration. This approach eliminated the error message and the mailbox was successfully created.

Problem 5:   The PS script also included some Active Directory commands for creating a new AD account with the mailbox. This required loading the active directory module. When I tried to import the module I got this warning: Error initializing default drive: ‘Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.’  Not really a helpful message but googling it I understood that I got into a double hop problem as the AD is installed on a different machine. So I decided to open the first remote session with the CredSSP authentication method (which enables double hops). This wasn’t a trivial task but luckily, Drew has an excellent blog post about this topic (http://werepoint.blogspot.ca/2012/03/setting-up-ps-remoting-for-all-that.html) that helped me to enable it.

Problem 6:  After successfully creating a remote PowerShell session with the CredSSP authentication, I tried again to import the active directory module and got “Out of Memory” Exception.  To solve this one I executed this command on the remote server: winrm set winrm/config/winrs @{MaxMemoryPerShellMB=”1024″}

Problem 7:  Now my script was executing successfully but with all the parameters still hardcoded on the script, when I tried to make it more generic and pass the remote script parameter values from the C# application, I found that I could not use the following code: powershell.Runspace.SessionStateProxy.SetVariable(“{Param name}”, “{Param value}”); As the SessionStateProxy object doesn’t initialize with a remote PowerShell sessions. To overcome this, I changed my code to iterate the parameters and for each, add a “Set-Variable” Command with the relevant values like

foreach (var parameter in parameters)
{
Command command = new Command("Set-Variable");
command.Parameters.Add("Name", parameter.Key);
command.Parameters.Add("Value", parameter.Value);
this.powerShell.Commands.AddCommand(command);
}
this.powerShell.Invoke();

So, after a challenging day I ended up with a basic but complete demo for remotely running a script on Exchange 2010 SP2 server that creates a new Tenant’s mailbox.  Hopefully this post will save you some time implementing similar tasks.

Source for this demo web-page – file size 41 KB

IT Advice Exchange 2010

Subscribe to receive Carbon60 news

Stay up to date on insights, blog articles, events and services from Carbon60 delivered to your inbox.

Subscribe
Carbon60
  • Cloud Adoption
    • Cloud Readiness Assessment
    • Cloud Migration Services
    • Cloud Launchpad
    • Modernization
    • Cloud Security & Compliance
  • Managed Cloud
    • Managed Private Cloud
    • Managed Public Cloud
    • Cloud Backup
    • Cloud Disaster Recovery
    • Security
    • Applications
  • Industries
    • Financial Services
    • Healthcare
    • Public Sector
    • Technology
Follow us on LinkedIn Follow us on Twitter Follow us on YouTube

© Copyright Carbon60 2023

  • Privacy Policy
  • Terms & Conditions
  • Contact Us