문제

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?

도움이 되었습니까?

해결책

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)
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top