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.