Home
%3CLINGO-SUB%20id%3D%22lingo-sub-597729%22%20slang%3D%22en-US%22%3EProgrammatic%20Access%20via%20Remote%20PowerShell%20in%20Exchange%20Server%202010%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-597729%22%20slang%3D%22en-US%22%3E%3CP%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3EThe%20management%20experience%20given%20by%20Exchange%202010%20through%20PowerShell%20has%20been%20moved%20all%20the%20way%20from%20Local%20to%20Remote.%20This%20will%20mean%20that%20enterprise%20Admins%20will%20have%20to%20adjust%20their%20regular%20scripts%20to%20connect%20to%20Remote%20PowerShell%20instead%20of%20creating%20a%20local%20session.%20%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3EHere%20are%20some%20examples%20on%20how%20can%20this%20be%20achieved%20and%20the%20differences%20that%20may%20have%20to%20be%20done%20in%20order%20to%20create%20the%20connection%20and%20run%20the%20cmdlets.%20%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CB%3E%3CFONT%20color%3D%22%230000ff%22%20size%3D%222%22%3EUsing%20programmatic%20API%3C%2FFONT%3E%3C%2FB%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3EThe%20programmatic%20API%20is%20the%20simplest%20method%20that%20will%20allow%20you%20to%20make%20a%20remote%20connection%20requiring%20only%20the%20Uri%20for%20the%20connection%20and%20a%20set%20of%20suitable%20credentials%20that%20need%20to%20be%20provided%20through%20a%20method.%20%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3ESCredential%20credential%20%3D%20new%20PSCredential(username%2C%20password)%3B%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3E%3CB%3ENote%3A%20%3C%2FB%3EThe%20password%20here%20must%20be%20of%20type%20SecureString.%20%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3EAfter%20you%20just%20need%20to%20create%20the%20connection%20Information%20that%20will%20allow%20the%20creation%20of%20the%20runspace.%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%2F%2F%20Set%20the%20connection%20Info%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20WSManConnectionInfo%20connectionInfo%20%3D%20new%20WSManConnectionInfo(%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20new%20Uri(liveIdconnectionUri)%2C%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%3CA%20href%3D%22http%3A%2F%2Fschemas.microsoft.com%2Fpowershell%2FMicrosoft.Exchange%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttp%3A%2F%2Fschemas.microsoft.com%2Fpowershell%2FMicrosoft.Exchange%3C%2FA%3E%2C%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecredential)%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20connectionInfo.AuthenticationMechanism%20%3D%20AuthenticationMechanism.Basic%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%2F%2F%20create%20a%20runspace%20on%20a%20remote%20path%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%2F%2F%20the%20returned%20instance%20must%20be%20of%20type%20RemoteRunspace%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3ERunspace%20runspace%20%3D%20System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo)%3B%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3EFrom%20here%20it%20is%20just%20you%20only%20need%20to%20create%20a%20PowerShell%20instance%20and%20fill%20it%20with%20your%20cmdlets%20and%20then%20invoke%20it%20through%20the%20run%20space%20we've%20just%20created.%20Here%20is%20a%20simple%20example%20with%20get-mailbox.%20%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3EPowerShell%20powershell%20%3D%20PowerShell.Create()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20PSCommand%20command%20%3D%20new%20PSCommand()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20command.AddCommand(%22Get-Mailbox%22)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20command.AddParameter(%22Identity%22%2C%20mailboxName)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20powershell.Commands%20%3D%20command%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20try%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20open%20the%20remote%20runspace%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20runspace.Open()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20associate%20the%20runspace%20with%20powershell%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20powershell.Runspace%20%3D%20runspace%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20invoke%20the%20powershell%20to%20obtain%20the%20results%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20return%20powershell.Invoke()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20finally%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20dispose%20the%20runspace%20and%20enable%20garbage%20collection%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20runspace.Dispose()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20runspace%20%3D%20null%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20Finally%20dispose%20the%20powershell%20and%20set%20all%20variables%20to%20null%20to%20free%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20up%20any%20resources.%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20powershell.Dispose()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20powershell%20%3D%20null%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CB%3E%3CFONT%20color%3D%22%230000ff%22%20size%3D%222%22%3EUsing%20Programmatic%20API%20and%20Certificate%20Thumbprint%3C%2FFONT%3E%3C%2FB%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3EThis%20uses%20the%20exact%20same%20syntax%20than%20the%20Programmatic%20API%20except%20that%20we%20would%20need%20to%20connect%20to%20a%20Uri%20that%20is%20has%20a%20Certificate%20Thumbprint%20enabled%20when%20we%20create%20the%20WSMAN%20connection%20in%20this%20syntax.%20%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3EWSManConnectionInfo%20connectionInfo%20%3D%20new%20WSManConnectionInfo(%3CBR%20%2F%3E%3C%2FFONT%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%22E75C847ADF7B355DAAC2C6D1A4EDD8284A0C0FDC%22%2C%3CBR%20%2F%3E%3C%2FFONT%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Enew%20Uri(certconnectionUri)%2C%3CBR%20%2F%3E%3C%2FFONT%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%22%3CA%20href%3D%22http%3A%2F%2Fschemas.microsoft.com%2Fpowershell%2FMicrosoft.Exchange%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttp%3A%2F%2Fschemas.microsoft.com%2Fpowershell%2FMicrosoft.Exchange%3C%2FA%3E%22)%3B%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CB%3E%3CFONT%20color%3D%22%230000ff%22%20size%3D%222%22%3ERemote%20Request%20using%20a%20local%20run%20space%20(Scripting%20the%20remote%20class)%3C%2FFONT%3E%3C%2FB%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3EThis%20is%20the%20best%20to%20create%20scripts%20and%20run%20your%20cmdlets%20using%20remote%20PowerShell.%20For%20this%20case%2C%20we%20have%20to%20script%20in%20the%20code%20a%20call%20to%20create%20a%20New-PSSession%20using%20our%20credential%2C%20the%20connection%20Uri%20and%20method%20of%20authentication.%20This%20is%20basically%20using%20the%20cmdlet%3A%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3ENew-PSSession%20-ConnectionUri%20Microsoft.Exchange%20-ConnectionUri%20%24Uri%20-Credential%20%24LiveCred%20-Authentication%20Basic%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3ETo%20do%20this%2C%20we%20have%20to%20create%20the%20Run%20space%20in%20which%20we%20will%20run%20the%20cmdlet%20and%20then%20create%20a%20PowerShell%20instance%20to%20add%20the%20cmdlet.%20%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3ERunspace%20runspace%20%3D%20System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20PowerShell%20powershell%20%3D%20PowerShell.Create()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3EPSCommand%20command%20%3D%20new%20PSCommand()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand.AddCommand(%22New-PSSession%22)%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand.AddParameter(%22ConfigurationName%22%2C%20%22Microsoft.Exchange%22)%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand.AddParameter(%22ConnectionUri%22%2C%20new%20Uri(liveIdconnectionUri))%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand.AddParameter(%22Credential%22%2C%20cred)%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand.AddParameter(%22Authentication%22%2C%20%22Basic%22)%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Epowershell.Commands%20%3D%20command%3B%3C%2FFONT%3E%3C%2FP%3E%20%3CP%3E%3CFONT%20size%3D%222%22%20face%3D%22ver%22%3ENow%20invoke%20this%20cmdlet%20and%20set%20it%20as%20a%20variable%20on%20the%20local%20Run%20Space%20that%20will%20be%20used%20to%20do%20the%20remote%20calls.%20%3C%2FFONT%3E%3C%2FP%3E%20%3CP%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Etry%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%2F%2F%20open%20the%20remote%20runspace%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Erunspace.Open()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%2F%2F%20associate%20the%20runspace%20with%20powershell%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Epowershell.Runspace%20%3D%20runspace%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%2F%2F%20invoke%20the%20powershell%20to%20obtain%20the%20results%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3ECollection%3CREMOTERUNSPACEINFO%3E%20result%20%3D%20powershell.Invoke%3CREMOTERUNSPACEINFO%3E()%3B%3CBR%20%2F%3E%3C%2FREMOTERUNSPACEINFO%3E%3C%2FREMOTERUNSPACEINFO%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Eforeach%20(ErrorRecord%20current%20in%20powershell.Streams.Error)%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3EConsole.WriteLine(%22The%20following%20Error%20happen%20when%20opening%20the%20remote%20Runspace%3A%20%22%20%2B%20current.Exception.ToString()%20%2B%20%22%20%7C%20InnerException%3A%20%22%20%2B%20current.Exception.InnerException)%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Eif%20(result.Count%20!%3D%201)%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ethrow%20new%20Exception(%22Unexpected%20number%20of%20Remote%20Runspace%20connections%20returned.%22)%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%2F%2F%20Set%20the%20runspace%20as%20a%20local%20variable%20on%20the%20runspace%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20powershell%20%3D%20PowerShell.Create()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand%20%3D%20new%20PSCommand()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand.AddCommand(%22Set-Variable%22)%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand.AddParameter(%22Name%22%2C%20%22ra%22)%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand.AddParameter(%22Value%22%2C%20result%5B0%5D)%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Epowershell.Commands%20%3D%20command%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Epowershell.Runspace%20%3D%20runspace%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Epowershell.Invoke()%3B%3C%2FFONT%3E%3C%2FP%3E%20%3CP%3E%3CFONT%20size%3D%222%22%20face%3D%22ver%22%3EAfter%20we%20have%20done%20this%2C%20we%20can%20now%20do%20the%20remote%20calls%20that%20will%20be%20executed%20on%20the%20server%20side%20as%20script%20blocks.%20%3C%2FFONT%3E%3C%2FP%3E%20%3CP%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Epowershell%20%3D%20PowerShell.Create()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand%20%3D%20new%20PSCommand()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ecommand.AddScript(%22Invoke-Command%20-ScriptBlock%20%7B%20Get-Mailbox%20-Identity%3A%22%20%2B%20mailboxName%20%2B%20%22%20%7D%20-%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3ESession%20%24ra%22)%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Epowershell.Commands%20%3D%20command%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Epowershell.Runspace%20%3D%20runspace%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Ereturn%20powershell.Invoke()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%7D%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Efinally%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%7B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%2F%2F%20dispose%20the%20runspace%20and%20enable%20garbage%20collection%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Erunspace.Dispose()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Erunspace%20%3D%20null%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%2F%2F%20Finally%20dispose%20the%20powershell%20and%20set%20all%20variables%20to%20null%20to%20free%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%2F%2F%20up%20any%20resources.%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Epowershell.Dispose()%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3Epowershell%20%3D%20null%3B%3CBR%20%2F%3E%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3E%3CFONT%20size%3D%222%22%20face%3D%22Courier%20New%22%3E%7D%3C%2FFONT%3E%3C%2FP%3E%20%3CP%3E%3CFONT%20size%3D%222%22%3E-%20%3CA%20href%3D%22http%3A%2F%2Fmsexchangeteam.com%2Farticles%2F452867.aspx%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EMario%20Trigueros%20Solorio%3C%2FA%3E%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E

The management experience given by Exchange 2010 through PowerShell has been moved all the way from Local to Remote. This will mean that enterprise Admins will have to adjust their regular scripts to connect to Remote PowerShell instead of creating a local session.

Here are some examples on how can this be achieved and the differences that may have to be done in order to create the connection and run the cmdlets.

Using programmatic API

The programmatic API is the simplest method that will allow you to make a remote connection requiring only the Uri for the connection and a set of suitable credentials that need to be provided through a method.

SCredential credential = new PSCredential(username, password);

Note: The password here must be of type SecureString.

After you just need to create the connection Information that will allow the creation of the runspace.

// Set the connection Info
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
      new Uri(liveIdconnectionUri),
      http://schemas.microsoft.com/powershell/Microsoft.Exchange,
      credential);

    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

// create a runspace on a remote path
// the returned instance must be of type RemoteRunspace

Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

From here it is just you only need to create a PowerShell instance and fill it with your cmdlets and then invoke it through the run space we've just created. Here is a simple example with get-mailbox.

PowerShell powershell = PowerShell.Create();
      PSCommand command = new PSCommand();
      command.AddCommand("Get-Mailbox");
      command.AddParameter("Identity", mailboxName);
      powershell.Commands = command;
      try
      {
          // open the remote runspace
          runspace.Open();
          // associate the runspace with powershell
          powershell.Runspace = runspace;
          // invoke the powershell to obtain the results
          return powershell.Invoke();
      }
      finally
      {
          // dispose the runspace and enable garbage collection
          runspace.Dispose();
          runspace = null;
          // Finally dispose the powershell and set all variables to null to free
          // up any resources.
          powershell.Dispose();
          powershell = null;
      }

Using Programmatic API and Certificate Thumbprint

This uses the exact same syntax than the Programmatic API except that we would need to connect to a Uri that is has a Certificate Thumbprint enabled when we create the WSMAN connection in this syntax.

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
              "E75C847ADF7B355DAAC2C6D1A4EDD8284A0C0FDC",
              new Uri(certconnectionUri),
              "http://schemas.microsoft.com/powershell/Microsoft.Exchange");

Remote Request using a local run space (Scripting the remote class)

This is the best to create scripts and run your cmdlets using remote PowerShell. For this case, we have to script in the code a call to create a New-PSSession using our credential, the connection Uri and method of authentication. This is basically using the cmdlet:

New-PSSession -ConnectionUri Microsoft.Exchange -ConnectionUri $Uri -Credential $LiveCred -Authentication Basic

To do this, we have to create the Run space in which we will run the cmdlet and then create a PowerShell instance to add the cmdlet.

Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();
        command.AddCommand("New-PSSession");
        command.AddParameter("ConfigurationName", "Microsoft.Exchange");
        command.AddParameter("ConnectionUri", new Uri(liveIdconnectionUri));
        command.AddParameter("Credential", cred);
        command.AddParameter("Authentication", "Basic");
        powershell.Commands = command;

Now invoke this cmdlet and set it as a variable on the local Run Space that will be used to do the remote calls.

try
     {
         // open the remote runspace
         runspace.Open();
         // associate the runspace with powershell
          powershell.Runspace = runspace;
         // invoke the powershell to obtain the results
         Collection<RemoteRunspaceInfo> result = powershell.Invoke<RemoteRunspaceInfo>();
         foreach (ErrorRecord current in powershell.Streams.Error)
         Console.WriteLine("The following Error happen when opening the remote Runspace: " + current.Exception.ToString() + " | InnerException: " + current.Exception.InnerException);
         if (result.Count != 1)
             throw new Exception("Unexpected number of Remote Runspace connections returned.");
         // Set the runspace as a local variable on the runspace
     powershell = PowerShell.Create();
     command = new PSCommand();
     command.AddCommand("Set-Variable");
     command.AddParameter("Name", "ra");
     command.AddParameter("Value", result[0]);
     powershell.Commands = command;
         powershell.Runspace = runspace;
         powershell.Invoke();

After we have done this, we can now do the remote calls that will be executed on the server side as script blocks.

         powershell = PowerShell.Create();
     command = new PSCommand();
     command.AddScript("Invoke-Command -ScriptBlock { Get-Mailbox -Identity:" + mailboxName + " } -     Session $ra");
     powershell.Commands = command;
     powershell.Runspace = runspace;
         return powershell.Invoke();
     }
     finally
     {
         // dispose the runspace and enable garbage collection
         runspace.Dispose();
         runspace = null;
         // Finally dispose the powershell and set all variables to null to free
         // up any resources.
         powershell.Dispose();
         powershell = null;
     }

- Mario Trigueros Solorio

7 Comments
Not applicable
Hi, I am in the process of creating a project around remoting via Powershell from 2008 C#/VB using powershell 2.0 & WinRM 2.0 from a Windows Server 2008 SP2 machine. I have added the system.management.automation reference, but I am unable expose the RemoteRunspaceInfo class and my code therefore fails.  I cannot seem to expose in the fashion listed in the example above. Can you provide more information around which this class.
Not applicable
The sticking point for me seems to be the liveIdConnectionUri... what should be there? I'm using an Exchange 2010 installation that is not federated... should I point to

https://login.live.com?


Not applicable
when executed a powershell 2.0 command using vb.net to add mailbox to MS Exchange 2010 having WinRM client and server machines as same machine, i encountered the following error.



"The WinRM client cannot process the request. The authentication mechanism requested by the client is not supported by the server or unencrypted traffic is disabled in the service configuration. Verify the unencrypted traffic setting in the service configuration or specify one of the authentication mechanisms supported by the server. To use Kerberos, specify the computer name as the remote destination. Also verify that the client computer and the destination computer are joined to a domain. To use Basic, specify the computer name as the remote destination, specify Basic authentication and provide user name and password. Possible authentication mechanisms reported by server: For more information, see the about_Remote_Troubleshooting Help topic."



Environment i am using is

Windows Server 2008 R2 64-bit

MS Exchange 2010

PowerShell V2.0
Not applicable
Hi ,

i executed following code to get MailBoxDatabase  

Dim Pass As New System.Security.SecureString
       Dim MyPassword As String = Password
       For Each c As Char In MyPassword
           Pass.AppendChar(c)
       Next

       Dim credential As PSCredential = New PSCredential(UserName, Pass)


       Dim connectionInfo As New WSManConnectionInfo(New Uri("http://" & FQDN & "/powershell?serializationLevel=Full"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential)

       connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos

       Dim RSpace As Runspace
       Try
           RSpace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo)
           
       Catch ex As Exception
           Throw New Exception("RSPACE: " & ex.Message)
       End Try
 

 Dim Shell As PowerShell = PowerShell.Create()
       Dim PCommand As New PSCommand


       Try
           PCommand.AddCommand("Get-MailboxDatabase")
           PCommand.AddParameter("Server", New Microsoft.Exchange.Configuration.Tasks.ServerIdParameter(New Microsoft.Exchange.Data.Fqdn(ServerName)))

           ' PCommand.AddParameter("DomainController", PreferredDC)

           Shell.Commands = PCommand
       Catch ex As Exception
           Throw New Exception("Build command: " & ex.Message)
       End Try
       
       Dim results As Collection(Of PSObject)
       Dim Msg As String
       Dim Databases As String
       Try
           RSpace.Open()
           Shell.Runspace = RSpace
           results = Shell.Invoke()

           Dim Enm As System.Collections.Generic.IEnumerator(Of System.Management.Automation.PSObject) = results.GetEnumerator
           Dim index As Integer = 0
           Do While Enm.MoveNext()


               Databases = Enm.Current.Members("Identity").Value.ToString '& "" & Enm.Current.Members("AdminDisplayName").Value.ToString
           
           Loop

           RSpace.Close()



       Catch err As Exception
           Throw New Exception("Run command: " & err.Message)
       Finally
           RSpace.Dispose()
           RSpace = Nothing
           Shell.Dispose()
           Shell = Nothing
       End Try

and encountered the following error.
Cannot process argument transformation on parameter 'Server'. Cannot convert the "Clients.exmtemp.com" value of type "Deserialized.Microsoft.Exchange.Configuration.Tasks.ServerIdParameter" to type "Microsoft.Exchange.Configuration.Tasks.ServerIdParameter".

any help regarding above issue will be appreciated.
Thanks in Advance.

Best Regards,

cute devil
Not applicable
How can I write a code that recognize the exchange powershell and the basic powershell?

I mean:
get-mailbox | Format-list

Format-list is not recognized as a powershell command, when I use the constructor with the credential!!
Not applicable
What should the liveIdconnectionUri be in
   WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
     new Uri(liveIdconnectionUri),
     "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
     credential);
Not applicable
looks like liveIdConnectionUri should be

https://ps.outlook.com/powershell/



found it here:

http://help.outlook.com/en-us/140/cc713521.aspx#ScriptOptions