2

I am connecting to Exchange 2010 for Live@edu via PowerShell. I can connect using the standard methods just fine. However, downloading and importing the session commands every time seems wasteful, especially since this isn't on a LAN. Furthermore, occasionally, these scripts are going to be returning data to a web page, and the import time seems wasteful there as well.

I have found how to export the session using the Export-PSSession cmdlet. If I import that exported module with Import-Module, everything works correctly, except for one problem. When I run a cmdlet from the module, it expects me to interactively, via a GUI, set the password. What I really want is for my scripts to run in a non-interactive manner, but yet load the module locally.

Is this possible to do?

Tim Lehner
  • 14,813
  • 4
  • 59
  • 76

1 Answers1

2

The problem you are facing is that you need to be able to set the PSSession for all of the imported functions implicitly. To do that you need to be able run the Set-PSImplicitRemotingSessionfunction.

Unfortionatly that function is not being exported so you cannot access it. What you need to do to resolve this is crack open the PSM1 file and add that function to the end of $script:ExportModuleMember. Now when you import the module that function will be abilable to set your PSSession for all of the functions.

Here is what your powershell or scripts will need to run to be able to use any of the imported modules.

Import-Module "C:\Credentials.psm1"
Import-Module "C:\ExportedPSSession.psm1"
$Cred = Import-Credential -path C:\Cred.xml
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Authentication Basic -AllowRedirection -Credential $Cred
Set-PSImplicitRemotingSession -PSSession $Session -createdByModule $True
#You can now run any of the imported functions.

Credentials.psm1 Beware! Anyone who can load the xml file can now impersonate you!

function Export-Credential($cred, $path) {    
  $cred = $cred | Select-Object *    
  $cred.password = $cred.Password | ConvertFrom-SecureString
  $obj = New-Object psobject
  $obj | Add-Member -MemberType NoteProperty -Name UserName -Value $cred.username
  $obj | Add-Member -MemberType NoteProperty -Name Password -Value $cred.password
  $obj | Export-Clixml $path
}

function Import-Credential($path) {    
  $obj = Import-Clixml $path    
  $obj.password = $obj.Password | ConvertTo-SecureString
    return New-Object system.Management.Automation.PSCredential( $obj.username, $obj.password)
}
jamason1983
  • 419
  • 1
  • 3
  • 11
  • I've got that part working. I followed his code to make BPOS work correctly. However, the BPOS cmdlets take a credential parameter. However, for the Live@edu ones, I don't see any way to specify a credential. If I follow those instructions and load a module, or load a module and follow those instructions, it still asks for a password interactively. – Richard Frovarp Mar 23 '11 at 18:43
  • PowerShell interactively request creds from you, or some other form/process is making this request outside of PS? – jamason1983 Mar 23 '11 at 19:02
  • PowerShell is interactively requesting creds. The username is pre-filled in with the user used to export the session. – Richard Frovarp Mar 23 '11 at 22:04
  • Updated my anwser... turns out you need to edit your psm1 file to export an additional function so you can set the ps session information. – jamason1983 Mar 25 '11 at 02:08
  • 2
    You don't need to export that function, you can call into private functions of a module like this: & (gmo "NameOfTheModule") Set-PSImplicitRemotingSession -PSSession $Session -createdByModule $True – JasonMArcher Mar 26 '11 at 17:42