Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Session PowerShell关闭开放会话_Session_Powershell - Fatal编程技术网

Session PowerShell关闭开放会话

Session PowerShell关闭开放会话,session,powershell,Session,Powershell,我正在编写一个大型脚本,其中运行一个Foreach循环,在该循环中定义变量,然后检查$Server变量是否可ping以及是否可远程访问 为此,我使用来自PowerShell帮助的以下函数: # Function to check if $Server is online Function CanPing ($Server) { $error.clear() $tmp = Test-Connection $Server -ErrorAction SilentlyContinue

我正在编写一个大型脚本,其中运行一个
Foreach
循环,在该循环中定义变量,然后检查
$Server
变量是否可ping以及是否可远程访问

为此,我使用来自PowerShell帮助的以下函数:

# Function to check if $Server is online
Function CanPing ($Server) {
   $error.clear()
   $tmp = Test-Connection $Server -ErrorAction SilentlyContinue

   if ($?) {
       Write-Host "Ping succeeded: $Server"; Return $true
   }
   else {
        Write-Host "Ping failed: $Server."; Return $false
   }
}

# Function to check if $Server is remotely accessible
Function CanRemote ($Server) {
    $s = New-PSSession $Server -Authentication Credssp -Credential $Credentials -Name "Test" -ErrorAction SilentlyContinue

    if ($s -is [System.Management.Automation.Runspaces.PSSession]) {
        Enter-PSSession -Session $s
        Exit-PSSession
        Write-Host "Remote test succeeded: $Server."; Return $true
    }
    else {
        Write-Host "Remote test failed: $Server."; Return $false
    }
}

# Execute functions to check $Server
if ($Server -ne "UNC") {

    if (CanPing $Server) {
        if (-Not (CanRemote $Server)) {
        Write-Host "Exit loop REMOTE" -ForegroundColor Yellow
        continue
        }
    }
    else {
         Write-Host "Exit loop PING" -ForegroundColor Yellow
         continue # 'continue' to the next object and don't execute the rest of the code, 'break' exits the foreach loop completely
    }
} 
每次运行此代码时,都会在远程服务器上创建一个名为
wsmprovhost.exe
的进程。如果我在web上找到的信息正确,则此过程表示PowerShell会话。但是,在执行
Get PSSession$Server
操作时,PowerShell ISE中不会显示任何打开的会话,即使进程在远程服务器上可见,并且只能通过任务管理器终止

当我运行此代码时,通常会达到打开会话的限制,因为每次将新进程
wsmprovhost.exe
添加到
$Server
时,命令都会出错。我试图通过在代码中添加
Exit PSSession
来解决这个问题,但它不会关闭会话


欢迎您提供任何帮助或想法。

问题在于您必须进入PSSession。Enter PSSession只能以交互方式使用,不能在脚本中使用。我建议采取类似的措施:

# Function to check if $Server is remotely accessible
Function CanRemote ($Server) {

  Try {
   $s = New-PSSession $Server -Authentication Credssp -Credential $Credentials -Name "Test" -ErrorAction Stop
   Write-Host "Remote test succeeded: $Server."
   $true
   Remove-PSSession $s
   }

  Catch {
          "Remote test failed: $Server."
          $false
        }
 }

如果我理解正确,您的远程
ps会话将不会关闭

据我所知,
Get-PSSession
将在本地会话之前显示会话 处于活动状态(我是指您创建的远程
ps会话
)但一旦您的本地会话 ends
Get-PSSession
将不显示它们,因为它们在您的计算机上不再有效 而是在远程系统(或)上,它们不再在本地会话范围内

您可以使用命令获取会话

Get-PSSession -ComputerName server_name
如果你想移除它们,你可以这样做

Get-PSSession -ComputerName server_name | Remove-PSSession

即使在执行下面的命令之后,如果您不能创建会话

Get-PSSession -ComputerName server_name | Remove-PSSession

请在目标计算机上重新启动Windows远程管理(WS-Management)服务。

命令
Remove PSSession
起作用,谢谢您的建议。在脚本中无法从内部进入远程会话仍然很奇怪,但是这个解决方法对我来说很好。谢谢你的帮助Rahul。我尝试了
getpssession-ComputerName server\u name
,但没有给出任何结果,但进程在远程服务器上仍然可见。这可能是因为,正如您所说,当脚本结束时,本地会话已经关闭。我没有想到这一点,因为我希望在执行命令
getpssession-ComputerName server\u name
时总是看到所有打开的会话,但情况显然不是这样。再次感谢您的帮助。@DarkLite1,没错
Get PSSession
将仅在会话处于活动状态时显示会话。欢迎使用。由于请求过程错误而拒绝连接时,重新启动Windows远程管理会有所帮助。谢谢