连接到现有的 PSSession

连接到现有的 PSSession

目前我正在使用网页JEA内部授予我们的服务台一些 o365 MFA 的管理权限。在这种情况下,重置 MFA 联系方式并查看启用了 MFA 的当前用户。

他们点击的主页运行一个 onload 脚本,该脚本连接到所需的 powershell 会话。该脚本是

$adminCredential = "xxxxxxx"
$Pass = ConvertTo-SecureString "xxxxx" -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential - 
ArgumentList $adminCredential, $Pass

Write-Output "Connecting to Exchange Online Remote Powershell Service"
$ExoSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
if ($null -ne $ExoSession) { 
    Import-PSSession $ExoSession -AllowClobber
} else {
    Write-Output "  No EXO service set up for this account"
}

Write-Output "Connecting to EOP Powershell Service"
$EopSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
if ($null -ne $EopSession) { 
    Import-PSSession $EopSession -AllowClobber
} else {
    Write-Output "  No EOP service set up for this account"
}


#This connects to Azure Active Directory
Connect-MsolService -Credential $cred

每次加载“主页”时都会运行此程序,这通常会导致加载时间过长,因为它会导入所需的 cmdlet 和 ETC。它还会输出一些错误消息,表示无法连接,因为已达到最大连接数等等。

如果存在任何现有的 PSsession,我该如何使上述脚本看起来正确?

如果有打开的会话,则不会运行脚本的其余部分。

任何帮助将不胜感激

答案1

放在这里:因为评论太长,超出了字符限制。

这不是 PowerShell 代码问题/限制,而是安全/操作边界限制。

每个会话都有一个 ID 和名称,甚至与启动会话的机器绑定。如果用户连接到给定主机,则该主机上的管理员实际上可以查看并看到该入站连接。如果这不是 http 会话,那么您可以通过这种方式获取并重新连接到该主机的会话...

# Get all PSRemoting sessions the target host
Get-PSSession -ComputerName MyServerName 

# Reconnect to all open sessions on the target host
Get-PSSession -ComputerName MyServerName | Connect-PSSession

由于这是 Web,因此每个用户会话都是唯一的(用户令牌、cookie 等等……等等……),尤其是在使用 SSL 的情况下,它们必须是唯一的。出于显而易见的原因,没有办法解决这个问题,而且,这不是 PowerShell 的限制,这只是 Web/SSL 会话的工作方式。

您的用户来自不同的工作站,可能使用不同的浏览器并建立唯一的用户会话。

这样做会导致跨站点脚本漏洞或 cookie/浏览器会话窃取。这是绝不应该允许的。

相关内容