我想运行一个包含aws命令的powershell脚本,该命令用于获取当前运行的aws实例的ip地址。通过cmd我可以通过执行executer.ps1文件来获取当前正在运行的实例ip地址。但是,通过php命令没有被执行。在executer.ps1文件中我调用python文件也正常工作。
此命令无法通过UI工作,
aws ec2 describe-instances --instance-ids $id --query Reservations[].Instances[].PublicIpAddress
如何做到这一点?
executer.ps1文件是,
Clear-Host
Clear-History
$Username = "**"
$Password = "***"
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
$ips = @()
Write-Host "----------------------------------"
Try
{
echo "Creating Instances will start now..."
python C:\AWS\Distributed-setup-3\CreateInstance.py
}
Catch
{
echo "Instance not created"
exit
}
Write-Host "----------------------------------"
echo "Creating setup...."
Start-Sleep -Seconds 60
python C:\AWS\Distributed-setup-3\statusCheck.py
Start-Sleep -Seconds 60
$content = Get-Content C:\AWS\Distributed-setup-3\InstanceId.txt
echo $content
Write-Host "----------------------------------"
ForEach ($id In $content){
echo "execute.ps1 id"
echo $id
$instances = aws ec2 describe-instances --instance-ids $id --query Reservations[].Instances[].PublicIpAddress
$instance = $instances[1]
$instance = $instance -replace '"' , ""
$instance = $instance.Trim()
$ips = $ips+$instance
}
Write-Host "----------------------------------"
Clear-Content 'C:\AWS\Distributed-setup-3\ipconfig.txt'
ForEach ($i In $ips){
#echo $i
if($i -eq ($ips[0])){
Write-Host "Master is "$i
}
else
{
Write-Host "Client "$i
$index = [array]::indexof($ips,$i)
$index = $index+1
if($ips[$index] -eq $null){
$i | Add-Content 'C:\AWS\Distributed-setup-3\ipconfig.txt'
}
else{
$i = $i+","
$i | Add-Content 'C:\AWS\Distributed-setup-3\ipconfig.txt'
}
}
}
Write-Host "----------------------------------"
aws s3 cp C://AWS/Distributed-setup-3/ipconfig.txt s3://s3merahkee
aws s3 cp C://AWS/Distributed-setup-3/Input.yaml s3://s3merahkee
Write-Host "----------------------------------"
Write-Host "Executing jmeter distributed test on remote system" $ips
Write-Host "----------------------------------"
ForEach ($ip In $ips){
if($ip -eq ($ips[0])){
#echo "Executing hub"
Write-Host "Master ip is "$ip
}
else
{
#Write-Host "Executing slave "$ip
Start-Job "C:\AWS\Distributed-setup-3\instance_2.ps1" -ArgumentList $ip
}
}
Start-Sleep -Seconds 60
Write-Host "----------------------------------"
Get-job | Receive-Job
Write-Host "----------------------------------"
$hub = $ips[0]
echo -n "Executing Master :"$hub
Start-Sleep -Seconds 10
Invoke-Command -ComputerName $hub -ScriptBlock { cmd.exe /c C:\Users\Administrator\Desktop\gitclone.bat } -credential $Cred
Write-Host "----------------------------------"
ForEach ($ip In $ips){
echo -n "Executing on remote system for reports " $ip
Try
{
Invoke-Command -ComputerName $ip -ScriptBlock { cmd.exe /c C:\Users\Administrator\Desktop\report.bat } -credential $Cred
}
Catch{
echo "Unable to connect check the remote connection permission"
}
}
Write-Host "----------------------------------"
ForEach ($id In $content){
$instances = aws ec2 describe-instances --instance-ids $id --query 'Reservations[].Instances[].PrivateIpAddress'
$instance = $instances[1]
$instance = $instance -replace '"' , ""
$i = $instance.Trim()
#echo $i
aws s3 cp s3://s3merahkee/Logs/$i C:\AWS\Distributed-setup-3\Reports --recursive
cd C:\AWS\Distributed-setup-3\Reports
Rename-Item jmeter-server.log jmeter-server_$i.log
}
Write-Host "----------------------------------"
echo "Terminating instance"
ForEach ($id In $content){
aws ec2 terminate-instances --instance-ids $id
}
Write-Host "----------------------------------"
Write-Host "Execution completed"
在trail.php中,我正在调用executer.ps1 powershell脚本
<?php
ini_set('max_execution_time', 500);
$output= shell_exec('powershell -command C:/AWS/Distributed-setup-3/executer.ps1');
echo( '<pre>' );
echo( $output );
echo( '</pre>' );
?>