I am creating a process where an AWS instance can be started if currently stopped, then backed up on-premises via an rsync-based script, and then stopped when done. I also want the script to be executable via a web interface. Given that the backup process potentially takes several hours, the web interface must start a process that quickly takes the user to a landing page and doesn't stop the backup process if the browser tab or window is closed.
I have written the substantive code to accomplish this, but am running into problems when executing the code via PHP. I am on a Windows Server 2012 R2 running PHP 7.3.29, using PowerShell 7 and AWS Tools for PowerShell 4.1.18.
I have been trying to route around issues with spaces, quotes, and passing parameters, so the current implementation involves a few steps of indirection.
Here is the PHP code that kicks off the process:
exec('C:\scripts\Active_Unscheduled_Scripts\Instance_Backups\start_backup_and_stop_MSSC_Test_2.bat');
Here is the Windows batch file it executes:
Start "" "C:\scripts\Active_Unscheduled_Scripts\Instance_Backups\backup_and_stop_MSSC_Test_2.ps1"
Here is the PowerShell script it executes:
Try
{
Invoke-Expression "C:\'Program Files'\PowerShell\7\pwsh.exe C:\scripts\Active_Unscheduled_Scripts\Instance_Backups\backup_and_stop_master.ps1 `'[instance ID redacted]`' `'MSSC Test 2`'"
'Step 0 completed successfully' | Out-File C:\scripts\errors.txt -Append
}
Catch
{
'Step 0 Error ' + $error[0] | Out-File C:\scripts\errors.txt -Append
Exit
}
Here is the PowerShell script it executes in the Try:
$instanceId = $args[0]
$instanceName = $args[1]
[functioning email notification code, aka Step 1, removed for brevity]
Set-Variable -Scope global -Name status -Value ""
Try
{
$status = Get-EC2InstanceStatus -InstanceId $instanceId -IncludeAllInstances $true -Region us-east-2
$status.Status.Status.Value | Out-File C:\scripts\status.txt -Append
$status.InstanceState.Name.Value | Out-File C:\scripts\status.txt -Append
'Step 2 completed successfully' | Out-File C:\scripts\errors.txt -Append
}
Catch
{
('Step 2 Error ' + $error[0]) | Out-File C:\scripts\errors.txt -Append
Exit
}
When I open up a Command Prompt and run start_backup_and_stop_MSSC_Test_2.bat
, everything works as I expect. I receive the kickoff email (removed above) that confirms the parameter values, errors.txt
is filled with Step 1 completed successfully Step 2 completed successfully Step 0 completed successfully
, and status.txt
is filled with not-applicable
and stopped
.
However, when I execute the process via PHP, I get the kickoff email, but errors.txt
is filled with Step 1 completed successfully Step 2 Error The type initializer for 'Amazon.Runtime.Internal.Settings.PersistenceManager' threw an exception. Step 0 completed successfully
There are practically no Google results for this error. The one substantive post on the error concerns the Region parameter on a different commandlet, which I'm explicitly passing in even though it's not required.