PSSession导入后找不到Powershell命令

时间:2014-02-18 14:41:21

标签: powershell

我正在尝试使用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”命令?也许我需要导入别的东西,或者还有其他方法可以添加联系人?

1 个答案:

答案 0 :(得分:0)

您错过了创建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"