美好的一天,
我正在尝试创建一个使用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
答案 0 :(得分:0)
您的总体目标是什么?您是否要使用Powershell隐式远程处理,以便可以在本地使用一些远程功能?
如果是这样,则脚本的一般语法为:
#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