美好的一天,
我正在尝试创建一个使用PSSEssion登录远程计算机的功能。我们的想法是,这些会话将在后台运行,脚本会执行其他操作。
问题是当我使用Get-Job
时,我可以看到工作已经完成:
Id Name PSJobTypeName State HasMoreData Location -- ---- ------------- ----- ----------- -------- 2 Job2 BackgroundJob Completed True localhost 4 Job4 BackgroundJob Completed True localhost
当我使用Receive-Job -Id 2 -Keep
时,我也可以看到结果:
ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Script 1.0 tmp_jeenmyes.e5c {Search-AdminAuditLog,
问题是当我使用Get-PSSession
时它是空的。它不会显示新会话。我也不能使用新命令。我不确定我做错了什么。
感谢您的时间。
function Sessions {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
$AdminAccount
)
begin {
# Delete 'Broken' Sessions
$PSSession = Get-PSSession
foreach ($Session in $PSSession) {
IF ($Session.State -eq 'Broken') {
Remove-PSSession -id $Session.id
}
}
# SeesionOption for a longer session
$SeesionOptions = New-PSSessionOption -IdleTimeout $(60000 * 60) `
-OpenTimeout $(60000 * 60) -OperationTimeout $(60000 * 60)
}
process {
Start-Job -ScriptBlock {
$CloudSession = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri "https://pod51043psh.outlook.com/powershell-liveid?PSVersion=5.0.9814.0" `
-Credential $args[0] -Authentication Basic -SessionOption $args[1]
Import-PSSession $CloudSession
} -ArgumentList $LocalCred, $SeesionOptions
Start-Job -ScriptBlock {
$OnPremisesSession = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri http://wki-exch01.domain.com/powershell/ `
-Credential $args[0] -Authentication Kerberos -SessionOption $args[1]
Import-PSSession $OnPremisesSession
} -ArgumentList $LocalCred, $SeesionOptions
}
end {
}
}
Sessions -AdminAccount $Localcred
你的整体目标是什么?您是否尝试使用Powershell Implicit远程处理,以便在本地使用某些远程功能?
如果是这样,脚本的一般语法将是:
#start of script
Param(
$AdminAccount
)
$DCSession = New-PSSession -Computername DC1 -Credential $AdminAccount
Invoke-Command -Session $DCSession -ScriptBlock {Import-Module ActiveDirectory}
Import-PSSession -Session $DCSession -Module ActiveDirectory
#continue with script commands here using the imported functions/module as if they were local