2
我需要將一些參數從VBA7傳遞到Powershell腳本(4.0版)。我能夠通過在Powershell Editor中手動輸入所有參數來運行PS腳本,並運行它來驗證腳本是否正常工作。下面是PS腳本:如何將參數從VBA傳遞到Powershell腳本
param(
$pw,
$from ="",
$to ="",
$subject = "",
$body = "",
$file = "",
$server = "imap.myserver.com",
$port = 111
)
$pw = ConvertTo-SecureString $pw -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($from, $pw)
try{
Send-MailMessage -From $from -to $to -SmtpServer $server -Body $body -Subject $subject -Attachments $file -Port $port -Credential $cred -ErrorAction Stop
Exit 0;
}
catch
{
Write-Host $error[0]
Exit 1;
}
出於某種原因,當我打電話從VBA這個腳本這是行不通的,我沒有任何錯誤信息,我可以看到PowerShell是正在從宏調用,但什麼都沒發生。也許synthax不正確?我試過在這裏和那裏添加引號,但沒有成功。下面是代碼的簡單求版本:
Sub CallPowerShellWithArguments()
Call Shell("powershell.exe -ExecutionPolicy Bypass C:\Program Files (x86)\mailsend\send_reports.ps1 -pw Mypassword -from [email protected] -to [email protected] -subject MyEmail -body This is my test -file C:\mysql\test.tx")
End Sub
難道你不需要你的參數引號?例如。路徑?像這樣嘗試:「」C:\ Program Files文件(x86)\ mailsend \ send_reports.ps1「」 – vacip
另外,如果您只是將代碼複製粘貼到正常的cmd窗口,它是否正常運行? – vacip
@vacip我嘗試使用雙引號,我得到一個語法錯誤。代碼工作在一個cmd窗口中。這個問題真的好像在傳遞 – Seva