【问题标题】:Powershell command not found after PSSession ImportPSSession 导入后找不到 Powershell 命令
【发布时间】:2014-02-19 06:40:50
【问题描述】:

我正在尝试使用 Powershell 将外部联系人添加到 MS Exchange。

$username = "username@domain.com"
$password = "password"
$secure_password = $password | ConvertTo-SecureString -AsPlainText -Force
$credencial = New-Object System.Management.Automation.PSCredential ($username, $secure_password)    
$session_name = "office365_session"

foreach($tmp in Get-PSSession){
    if ($tmp.Name -eq $session_name) {
        $opened_session = Get-PSSession -Name $session_name
    }
}

if ($opened_session -eq $null) {
    $opened_session = New-PSSession -Name $session_name -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $credencial -Authentication Basic -AllowRedirection -WarningAction SilentlyContinue -ErrorAction Stop
    Import-PSSession $opened_session -AllowClobber -WarningAction SilentlyContinue -ErrorAction Stop -DisableNameChecking | Out-Null
}

New-MailContact -Name "test" -DisplayName "test user" -ExternalEmailAddress "some.email@mail.com" -FirstName "Test" -LastName "User"

但是没有找到“New-MailContact”命令并抛出错误:

New-MailContact : The term 'New-MailContact' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

如何运行“New-MailContact”命令?也许我需要导入其他东西,或者可能有另一种添加联系人的方法?

【问题讨论】:

  • 在导入会话后添加管理单元。

标签: powershell


【解决方案1】:

您错过了创建与 Exchange 框的会话的关键部分,因此您的导入不起作用。

这是 O365 的示例

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $credencial  -Authentication Basic –AllowRedirection;  

只有这样你才能运行

Import-PSSession $session -AllowClobber -WarningAction SilentlyContinue -ErrorAction Stop -DisableNameChecking | Out-Null

New-MailContact -Name "test" -DisplayName "test user" -ExternalEmailAddress "some.email@mail.com" -FirstName "Test" -LastName "User"

【讨论】:

  • 我没有错过在“New-MailContact”命令之前创建会话,只是复制了一个错误的脚本。抱歉 :) 我已经更新了有问题的 powershell 脚本。
  • 将您的导入更改为Import-PSSession $session -AllowClobber -WarningAction SilentlyContinue -ErrorAction Stop,您将获得有意义的输出。新模块将作为文件下载(临时文件,即 tmp_yywgcdto.ni0)。然后运行Get-Command -Module _yourModuleName_Get-Command -Module tmp_yywgcdto.ni0。你应该得到一堆交换命令或错误
  • 我使用了$import = Import-PSSession $session -AllowClobber -WarningAction SilentlyContinue -ErrorAction Stop Get-Command -Module $import.Name 我得到了一堆命令,但是列表中不存在New-MailContact 命令。
  • 是否有任何 Exchange 命令? Get-Command -Module $import.Name -Name *mail* 之外的任何内容?您应该获得 100 多个命令,例如 new-mailboxget-mailboxget-mailcontact 等。
  • 是的,我用*mail*Get-MailboxSet-MailboxSet-MailUser 获得了大约40 个命令,但是任何添加邮件联系人的命令。
猜你喜欢
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 2015-02-18
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 2014-04-12
相关资源
最近更新 更多