How do you authenticate to Live@EDU (outlook.com) while using an imported PowerShell session?
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 cor开发者_C百科rectly, 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?
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-PSImplicitRemotingSession
function.
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)
}
精彩评论