我的 PHP 腳本
exec('powershell.exe -ExecutionPolicy Unrestricted -NoProfile -InputFormat none -file "..\..\scripts\addcustomer.ps1"', $output);
腳本中的AZ函式:
$file = “$PSScriptRoot\Azure\pass.txt”
$azureuser = “[email protected]”
$Password = Get-Content $file | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($azureuser, $Password)
Login-AzAccount -Credential $credential
New-AzDnsRecordSet -Name "$name" -RecordType A -ZoneName "contoso.co" -ResourceGroupName "RG" -Ttl 3600 -DnsRecords (New-AzDnsRecordConfig -IPv4Address "$ipaddress")
$output 不顯示此函式的任何輸出。我確認如果我手動運行腳本,一切正常。
非常感謝。
uj5u.com熱心網友回復:
使用exec($your_command, $output, $error_code)并查看$error_code包含的內容。這可能只是因為powershell.exe不在 PHP 的 PATH 環境變數中。
嘗試將完整路徑放入您的 PowerShell 可執行檔案,通常是這樣的:
<?php
// A default powershell path in case "where powershell" doesn't work.
define('POWERSHELL_EXE', 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe');
// Find the path to powershell with the "where" command.
$powershell_exe = exec('where powershell');
if ($powershell_exe === false) {
$powershell_exe = POWERSHELL_EXE;
}
// Also check that the path to your script can be found.
// If it doesn't then you can use realpath() to get the
// full path to your script.
$cmd = $powershell_exe .
' -ExecutionPolicy Unrestricted -NoProfile -InputFormat none' .
' -file "..\..\scripts\addcustomer.ps1"';
$last_line = exec($cmd, $full_output, $error_code);
// Then check if $last_line !== false and check $error_code to see
// what happened.
var_export([
'$cmd' => $cmd,
'$last_line' => $last_line,
'$full_output' => $full_output,
'$error_code' => $error_code,
]);
另一個重點:運行您的 PHP 代碼的用戶是否有足夠的權限來執行您在 PS1 腳本中所做的事情?
您可能需要使用提升權限或其他用戶運行 PowerShell 腳本。我從來沒有這樣做過,但也許這個主題可以幫助: PHP command to execute powershell script as admin
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/421049.html
標籤:
