Exchange/Office365 自動處理腳本:常用函數(一)

創建自定義函數腳本
1,在C:\Users\當前登錄用戶名\Documents\WindowsPowershell下創建名稱爲Microsoft.PowerShell_profile.ps1 的文件
2,打開Microsoft.PowerShell_profile.ps1,使用Powershell ISE添加以下函數

函數1:憑據

function exchangeaccountname
{
$account="[email protected]" 
$password = ConvertTo-SecureString "Account Password" -asplaintext -force
$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist $account,$password
return $cred
}
function skypeaccountname
{
$account="[email protected]" 
$password = ConvertTo-SecureString "Account Password" -asplaintext -force
$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist $account,$password
return $cred
}
function o365accountname
{
$account="[email protected]" 
$password = ConvertTo-SecureString "Account Password" -asplaintext -force
$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist $account,$password
return $cred
}

函數2:連接到Exchange 2010/2013 Server

function connectoExchange
{
$ConnectionUri = "http://cas01.domain.cn/PowerShell/","http://cas02.domain.cn/PowerShell/" #此處將鏈接修改爲Exchange Cas
$thisConnectionUri = $ConnectionUri | Get-Random
$cred = exchangeaccountname    #此處使用憑據函數
echo $thisConnectionUri
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $thisConnectionUri -Authentication Kerberos -Credential $cred
Import-PSSession $Session -AllowClobber  
}
}

函數2:連接到Skype for Business 2015

function localskype
{
<#
.Synopsis
連接到skype for business 2015
#>
$cred= skypeaccountname    #此處使用憑據函數
$so = New-PSSessionOption -SkipCACheck:$true -SkipCNCheck:$true -SkipRevocationCheck:$true
$Session = New-PSSession -ConnectionUri "https://sfbfe01.domain.cn/ocspowershell" -Credential $cred -SessionOption $so
Import-PSSession $Session  #此處將鏈接修改爲skype前端
}

函數3:連接到Office365

function O365
{
<#
.Synopsis
連接到Office365
#>
import-module MSonline
#輸入Office365管理員賬號和密碼
$credential = o365accountname #此處使用憑據函數
Connect-MsolService -Credential $credential
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection
Import-PSSession $exchangeSession -AllowClobber
}

示例爲連接國際版Office365。
如連接21Vianet,使用_ConnectionUri_值:https://partner.outlook.cn/PowerShell
如連接德國Office 365,使用_ConnectionUri_值:https://outlook.office.de/powershell-liveid/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章