PowerShell模块连接到交换前提

时间:2019-04-30 01:39:29

标签: powershell module exchange-server

使用自定义模块时,无法从powershell运行Exchange cmdlet。

大家好,

当与脚本一起使用时,波纹管功能正常工作;但是,当转换为模块时,它会加载,并且所有但都无法识别cmdlet(例如get-mailbox)。

Function ConnectToExchange ([string]$eXCHServer, $myCred){
  $Session = "" 

  if(!$Session)
  {      
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $eXCHServer -Credential $myCred -AllowRedirection

    Import-PSSession $Session -AllowClobber 


    if ($Session.State -eq "Closed")
    {
      return
    }
 }
}#end function

1 个答案:

答案 0 :(得分:0)

我一直在努力解决同样的问题。当您进行交换时,它会创建一个临时模块,用于存储命令,我不知道为什么,但是它不会加载命令。

为连接函数分配一个变量,然后使用它来查找和加载模块似乎可行。

$exc = ConnectToExchange

$excmod = Get-ChildItem -Path $env:userprofile -Filter $exc.name -recurse -Force -ErrorAction SilentlyContinue | select FullName

Import-Module $excmod.fullname