Php 重用/共享powershell远程会话访问脚本
我有一个php服务器,可以生成ps1(poweshell)脚本并管理远程计算机。例如,我的powershell脚本如下所示:Php 重用/共享powershell远程会话访问脚本,php,powershell,scripting,Php,Powershell,Scripting,我有一个php服务器,可以生成ps1(poweshell)脚本并管理远程计算机。例如,我的powershell脚本如下所示: $ip=192.168.137.25; $pw = convertto-securestring -AsPlainText -Force -String a $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "$ip\admin",$pw $sessi
$ip=192.168.137.25;
$pw = convertto-securestring -AsPlainText -Force -String a
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "$ip\admin",$pw
$session = new-pssession $ip -credential $cred
invoke-command -session $session -scriptblock {ls}
我通过以下方式从php运行此脚本:
shell_exec("powershell.exe -ExecutionPolicy RemoteSigned -File script.ps1")
然后我需要调用第二个脚本,并希望使用由第一个脚本创建的会话。
问题是如何在第一个脚本结束后使远程会话保持活动状态。
也许还有其他解决方案,比如使用其他语言而不是php
谢谢如果托管计算机上有Powershell V3,则可以使用断开连接的会话,在第一个脚本中创建会话,然后使用Disconnect PSSession显式断开与会话的连接。然后,您可以在第二个脚本中使用Connect-PSSession重新连接到同一会话。您可以使用全局变量在多个脚本之间重用/共享远程powershell会话
$Global:session = New-PSSession -ComputerName $remoteServer -credential $credential
在第一个脚本中,创建一个远程会话并将其存储在全局变量中
$Global:session = New-PSSession -ComputerName $remoteServer -credential $credential
在所有其他脚本中,您可以使用调用命令
,如下所示:
Invoke-Command -Session $session -ScriptBlock {
/* Remote code */
}
$scripts = "create_session.ps1; script_1.ps1; script_2.ps1";
shell_exec("powershell.exe -Command @(\"$scripts\" )");
然后可以按如下方式运行脚本:
Invoke-Command -Session $session -ScriptBlock {
/* Remote code */
}
$scripts = "create_session.ps1; script_1.ps1; script_2.ps1";
shell_exec("powershell.exe -Command @(\"$scripts\" )");