此解決方案使用PowerShell遠程,並且要求機器你遠程入具有ActiveDirectory的模塊,並且它要求機器使得遠程連接(客戶端)是PowerShell的3或更高的版本。
在此示例中,機器的遙控器到自身。
這將是您的script.ps1文件:
#requires -Version 3.0
$ExistingSession = Get-PSSession -ComputerName . | Select-Object -First 1
if ($ExistingSession) {
Write-Verbose "Using existing session" -Verbose
$ExistingSession | Connect-PSSession | Out-Null
} else {
Write-Verbose "Creating new session." -Verbose
$ExistingSession = New-PSSession -ComputerName . -ErrorAction Stop
Invoke-Command -Session $ExistingSession -ScriptBlock { Import-Module ActiveDirectory }
}
Invoke-Command -Session $ExistingSession -ScriptBlock {
# do all your stuff here
}
$ExistingSession | Disconnect-PSSession | Out-Null
它需要的PowerShell的爲斷開的會話支持的優勢。每當您掏出PowerShell.exe時,最終都會連接到已加載ActiveDirectory模塊的現有會話。
一旦你所有的調用來完成的,你應該銷燬會話:
Get-PSSession -ComputerName . | Remove-PSSession
這與在每次運行一個獨立的powershell.exe調用測試。
我不知道是否延遲的原因實際上是因爲加載了ActiveDirectory模塊,或者至少有一部分延遲是由於必須加載PowerShell.exe本身而引起的。
什麼版本的PowerShell的? – briantist
你可以做的「主機配置文件」加載它時,PowerShell是加載 – Luke
@Luke不會保存任何時間,雖然,這是該規定的目的。您只需更改每次輸入模塊的位置。 – briantist