update rm deadcode
This commit is contained in:
@@ -7,6 +7,9 @@ use phpseclib3\Net\SSH2;
|
||||
use Exception;
|
||||
use App\Models\InfraManagement;
|
||||
use App\Http\Facade\SignRSA;
|
||||
use App\Support\PowerShellEscaper;
|
||||
use App\Support\PowerCliCertificatePolicy;
|
||||
use App\Support\SshHostKeyVerifier;
|
||||
|
||||
class PowerShellService
|
||||
{
|
||||
@@ -41,6 +44,8 @@ class PowerShellService
|
||||
|
||||
$connection = new SSH2($sshHost, $sshPort);
|
||||
|
||||
SshHostKeyVerifier::verify($connection, $sshHost);
|
||||
|
||||
if ($connection->login($sshUser, $sshPass)) {
|
||||
$connection->setTimeout(0); // No timeout
|
||||
return $connection;
|
||||
@@ -72,9 +77,8 @@ class PowerShellService
|
||||
return "Login_Fail";
|
||||
}
|
||||
|
||||
// Action wrapper
|
||||
$action = 'powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass '
|
||||
. '-Command "' . str_replace('"', '\"', $psCommand) . '"';
|
||||
$action = 'powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand '
|
||||
. PowerShellEscaper::encodedCommand($psCommand);
|
||||
|
||||
try {
|
||||
$out = $auth->exec($action);
|
||||
@@ -91,6 +95,11 @@ class PowerShellService
|
||||
return $this->CommandRSP($cmd);
|
||||
}
|
||||
|
||||
private function powerCliCertificateLine(): string
|
||||
{
|
||||
return PowerCliCertificatePolicy::configurationLine();
|
||||
}
|
||||
|
||||
protected function CommandRSP(string $cmd)
|
||||
{
|
||||
$auth = $this->LoginAuth();
|
||||
@@ -98,12 +107,9 @@ class PowerShellService
|
||||
return "Login_Fail";
|
||||
}
|
||||
|
||||
$esc = fn(string $v) => str_replace("'", "''", $v);
|
||||
|
||||
// Get credentials from Client
|
||||
$server = $esc($this->client->apiUri ?? '');
|
||||
$user = $esc($this->client->getUsername() ?? '');
|
||||
$pass = $esc($this->client->getPassword() ?? '');
|
||||
$server = PowerShellEscaper::singleQuote($this->client->apiUri ?? '');
|
||||
$user = PowerShellEscaper::singleQuote($this->client->getUsername() ?? '');
|
||||
$pass = PowerShellEscaper::singleQuote($this->client->getPassword() ?? '');
|
||||
|
||||
$psTemplate = <<<'PS'
|
||||
$ProgressPreference = 'SilentlyContinue'
|
||||
@@ -112,7 +118,7 @@ $WarningPreference = 'SilentlyContinue'
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
Import-Module VMware.PowerCLI -ErrorAction Stop
|
||||
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session -Confirm:$false | Out-Null
|
||||
<CERT>
|
||||
Connect-VIServer -Server '<SERVER>' -User '<USER>' -Password '<PASS>' | Out-Null
|
||||
|
||||
& {
|
||||
@@ -121,19 +127,19 @@ Connect-VIServer -Server '<SERVER>' -User '<USER>' -Password '<PASS>' | Out-Null
|
||||
PS;
|
||||
|
||||
$psScript = strtr($psTemplate, [
|
||||
'<CERT>' => $this->powerCliCertificateLine(),
|
||||
'<SERVER>' => $server,
|
||||
'<USER>' => $user,
|
||||
'<PASS>' => $pass,
|
||||
'<CMD>' => $cmd,
|
||||
]);
|
||||
|
||||
$encoded = base64_encode(mb_convert_encoding($psScript, 'UTF-16LE', 'UTF-8'));
|
||||
$action = "powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand $encoded";
|
||||
$action = 'powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand '
|
||||
. PowerShellEscaper::encodedCommand($psScript);
|
||||
|
||||
try {
|
||||
$out = $auth->exec($action);
|
||||
$errOut = $auth->getStdError();
|
||||
// PowerCLI banner is CLIXML informational records — not an error
|
||||
$realErr = $errOut && !str_starts_with(trim($errOut), '#< CLIXML');
|
||||
return $out . ($realErr ? "\n[STDERR]\n" . $errOut : '');
|
||||
} catch (\Throwable $e) {
|
||||
@@ -148,11 +154,9 @@ PS;
|
||||
return "Login_Fail";
|
||||
}
|
||||
|
||||
$esc = fn(string $v) => str_replace("'", "''", $v);
|
||||
|
||||
$server = $esc($this->client->apiUri ?? '');
|
||||
$user = $esc($this->client->getUsername() ?? '');
|
||||
$pass = $esc($this->client->getPassword() ?? '');
|
||||
$server = PowerShellEscaper::singleQuote($this->client->apiUri ?? '');
|
||||
$user = PowerShellEscaper::singleQuote($this->client->getUsername() ?? '');
|
||||
$pass = PowerShellEscaper::singleQuote($this->client->getPassword() ?? '');
|
||||
|
||||
$psTemplate = <<<'PS'
|
||||
$ProgressPreference = 'SilentlyContinue'
|
||||
@@ -162,7 +166,7 @@ $ErrorActionPreference = 'Stop'
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
|
||||
Import-Module VMware.PowerCLI -ErrorAction Stop
|
||||
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session -Confirm:$false | Out-Null
|
||||
<CERT>
|
||||
Connect-VIServer -Server '<SERVER>' -User '<USER>' -Password '<PASS>' | Out-Null
|
||||
|
||||
& {
|
||||
@@ -171,14 +175,15 @@ Connect-VIServer -Server '<SERVER>' -User '<USER>' -Password '<PASS>' | Out-Null
|
||||
PS;
|
||||
|
||||
$psScript = strtr($psTemplate, [
|
||||
'<CERT>' => $this->powerCliCertificateLine(),
|
||||
'<SERVER>' => $server,
|
||||
'<USER>' => $user,
|
||||
'<PASS>' => $pass,
|
||||
'<CMD>' => $cmd,
|
||||
]);
|
||||
|
||||
$encoded = base64_encode(mb_convert_encoding($psScript, 'UTF-16LE', 'UTF-8'));
|
||||
$action = "powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand $encoded";
|
||||
$action = 'powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand '
|
||||
. PowerShellEscaper::encodedCommand($psScript);
|
||||
|
||||
try {
|
||||
$out = $auth->exec($action);
|
||||
@@ -201,15 +206,14 @@ PS;
|
||||
public function resetGuestPasswordViaPs1(string $vmName, string $guestUser, string $guestPassword, string $newPassword, string $targetUser = 'Administrator'): array|string
|
||||
{
|
||||
$psFile = "C:\\CLISave\\AssignIP\\SetGuestPwd.ps1";
|
||||
$q = fn(string $v) => str_replace("'", "''", $v);
|
||||
|
||||
$args = " -VMName '" . $q($vmName) . "'";
|
||||
$args .= " -GuestUser '" . $q($guestUser) . "'";
|
||||
$args .= " -GuestPassword '" . $q($guestPassword) . "'";
|
||||
$args .= " -TargetUser '" . $q($targetUser) . "'";
|
||||
$args .= " -NewPassword '" . $q($newPassword) . "'";
|
||||
$args = ' -VMName ' . PowerShellEscaper::singleQuote($vmName);
|
||||
$args .= ' -GuestUser ' . PowerShellEscaper::singleQuote($guestUser);
|
||||
$args .= ' -GuestPassword ' . PowerShellEscaper::singleQuote($guestPassword);
|
||||
$args .= ' -TargetUser ' . PowerShellEscaper::singleQuote($targetUser);
|
||||
$args .= ' -NewPassword ' . PowerShellEscaper::singleQuote($newPassword);
|
||||
|
||||
$cmd = "& '$psFile'$args | Out-String";
|
||||
$cmd = '& ' . PowerShellEscaper::singleQuote($psFile) . "{$args} | Out-String";
|
||||
|
||||
$raw = $this->CommandRSP($cmd);
|
||||
if ($raw === 'Login_Fail' || $raw === false || $raw === null) {
|
||||
@@ -230,14 +234,13 @@ PS;
|
||||
public function changeRDPPortWindows(string $vmName, string $guestUser, string $guestPassword, string $newPort): array|string
|
||||
{
|
||||
$psFile = "C:\\CLISave\\AssignIP\\change-rdp-port.ps1";
|
||||
$q = fn(string $v) => str_replace("'", "''", $v);
|
||||
|
||||
$args = " -VMName '" . $q($vmName) . "'";
|
||||
$args .= " -GuestUser '" . $q($guestUser) . "'";
|
||||
$args .= " -GuestPassword '" . $q($guestPassword) . "'";
|
||||
$args .= " -NewPort " . (int) $newPort;
|
||||
$args = ' -VMName ' . PowerShellEscaper::singleQuote($vmName);
|
||||
$args .= ' -GuestUser ' . PowerShellEscaper::singleQuote($guestUser);
|
||||
$args .= ' -GuestPassword ' . PowerShellEscaper::singleQuote($guestPassword);
|
||||
$args .= ' -NewPort ' . (int) $newPort;
|
||||
|
||||
$cmd = "& '$psFile'$args";
|
||||
$cmd = '& ' . PowerShellEscaper::singleQuote($psFile) . $args;
|
||||
|
||||
$raw = $this->CommandChangePort($cmd, true);
|
||||
|
||||
@@ -286,8 +289,6 @@ PS;
|
||||
string $dns = '8.8.8.8',
|
||||
string $hostname = ''
|
||||
): string {
|
||||
$q = fn(string $v) => str_replace("'", "''", $v);
|
||||
|
||||
$isLinux = str_contains(strtolower($osType), 'linux')
|
||||
|| str_contains(strtolower($osType), 'ubuntu')
|
||||
|| str_contains(strtolower($osType), 'centos')
|
||||
@@ -295,29 +296,35 @@ PS;
|
||||
|
||||
if ($isLinux) {
|
||||
$psFile = "C:\\CLISave\\changeIPLinux.ps1";
|
||||
$Command = "& '{$psFile}'"
|
||||
. " -VMName '" . $q($vmName) . "'"
|
||||
. " -IP '" . $q($ipAddress) . "'"
|
||||
. " -Gateway '" . $q($gateway) . "'"
|
||||
. " -DNS '" . $q($dns) . "'"
|
||||
. " -GuestUser '" . $q($guestUser) . "'"
|
||||
. " -GuestPassword '" . $q($guestPassword) . "'";
|
||||
$Command = '& ' . PowerShellEscaper::singleQuote($psFile)
|
||||
. ' -VMName ' . PowerShellEscaper::singleQuote($vmName)
|
||||
. ' -IP ' . PowerShellEscaper::singleQuote($ipAddress)
|
||||
. ' -Gateway ' . PowerShellEscaper::singleQuote($gateway)
|
||||
. ' -DNS ' . PowerShellEscaper::singleQuote($dns)
|
||||
. ' -GuestUser ' . PowerShellEscaper::singleQuote($guestUser)
|
||||
. ' -GuestPassword ' . PowerShellEscaper::singleQuote($guestPassword);
|
||||
|
||||
Log::channel('Custom')->info('[changeIpVM:linux] ' . $Command);
|
||||
Log::channel('Custom')->info('[changeIpVM:linux]', [
|
||||
'script' => 'changeIPLinux.ps1',
|
||||
'vmware_id' => $vmwareId,
|
||||
]);
|
||||
|
||||
return (string) $this->CommandChangePort($Command, true);
|
||||
}
|
||||
|
||||
$VirtualMachine = "VirtualMachine-$vmwareId";
|
||||
$hn = strtolower($hostname ?: $vmName);
|
||||
$Command = "\$vm = get-vm -Id '{$VirtualMachine}';"
|
||||
. " \$hostname = '{$q($hn)}';"
|
||||
. " \$ipadd = '{$q($ipAddress)}';"
|
||||
. " \$gw = '{$q($gateway)}';"
|
||||
. " \$password = \"'{$q($guestPassword)}'\"; "
|
||||
. " cd /; C:\\CLISave\\changeWindows1line.ps1;";
|
||||
$Command = '$vm = get-vm -Id ' . PowerShellEscaper::singleQuote($VirtualMachine) . ';'
|
||||
. ' $hostname = ' . PowerShellEscaper::singleQuote($hn) . ';'
|
||||
. ' $ipadd = ' . PowerShellEscaper::singleQuote($ipAddress) . ';'
|
||||
. ' $gw = ' . PowerShellEscaper::singleQuote($gateway) . ';'
|
||||
. ' $password = ' . PowerShellEscaper::singleQuote($guestPassword) . '; '
|
||||
. ' cd /; C:\\CLISave\\changeWindows1line.ps1;';
|
||||
|
||||
Log::channel('Custom')->info('[changeIpVM:windows] ' . $Command);
|
||||
Log::channel('Custom')->info('[changeIpVM:windows]', [
|
||||
'script' => 'changeWindows1line.ps1',
|
||||
'vmware_id' => $vmwareId,
|
||||
]);
|
||||
|
||||
return (string) $this->CommandRSP($Command);
|
||||
}
|
||||
@@ -326,11 +333,11 @@ PS;
|
||||
{
|
||||
$ps = "(Get-ItemProperty 'HKLM:\\System\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp' -Name PortNumber).PortNumber";
|
||||
|
||||
$cmd = "Invoke-VMScript -VM '$vmwareId' "
|
||||
. "-ScriptText \"$ps\" "
|
||||
. "-ScriptType Powershell "
|
||||
. "-GuestUser '$guestUser' "
|
||||
. "-GuestPassword '$guestPassword'";
|
||||
$cmd = 'Invoke-VMScript -VM ' . PowerShellEscaper::singleQuote($vmwareId)
|
||||
. ' -ScriptText ' . PowerShellEscaper::singleQuote($ps)
|
||||
. ' -ScriptType Powershell'
|
||||
. ' -GuestUser ' . PowerShellEscaper::singleQuote($guestUser)
|
||||
. ' -GuestPassword ' . PowerShellEscaper::singleQuote($guestPassword);
|
||||
|
||||
$output = $this->CommandRSP($cmd);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user