簡化 powershell cmdlet

簡化 powershell cmdlet

我有一個 powershell cmdlet (move-IMAPMailboxToExchange),需要大量重複輸入。我想用另一個批次/powershell 腳本來呼叫它,該腳本只從命令列取得兩個參數。我已經嘗試了我能想到的所有呼叫約定,但無法使其工作。

我要這個:

Move-IMAPMailboxToExchange -SourcePassword P@ssW0rd! -allowunsecureconnection -sourceLoginId username -sourceserver source.ser.ver -sourceidentity [email protected] targetclientaccessserver "client.access.ser.ver" -targetidentity [email protected] -verbose

成為這樣:

migrate-user username P@ssW0rd!

我嘗試過 $args,但似乎有所擴展。我已經嘗試過$args[0] ,它可以在裸字密碼和來源登入中工作,但在@mail 旁邊不起作用......我已經嘗試過%1 等,從舊的DOS 時代開始,但那不起作用不工作。

我是一個 Unix 呆子,而且還不太熟悉 powershell。

答案1

據我所知,單引號是 PowerShell 將某些內容指定為「不解析」的方式。所以...

你可能想做這樣的事情

$username=$args[0]
$passwd=$args[1]

Move-IMAPMailboxToExchange [all that jazz]

作為在腳本上下文中牢固聲明變數的一種方法。如果您在建構 -sourceidentity 和 -targetidentity 變數時遇到問題,您可能需要在將它們放入 move-imaptoexchange 命令之前預先建構它們...

$sourceident="$username"+'@srcmail.dom.ain'
$targeditent="$username"+'@tgtmail.dom.ain'

答案2

您應該能夠圍繞 cmdlet 定義函數或腳本文件,如下所示:

    function migrate-user
{
 param($username,$password)

Move-IMAPMailboxToExchange -SourcePassword $password -allowunsecureconnection -sourceLoginId $username -sourceserver 'source.ser.ver' -sourceidentity "$username`@mail.dom.ain" targetclientaccessserver 'client.access.ser.ver' -targetidentity "$username`@mail.dom.ain" -verbose

}

相關內容