我需要将一些参数从VBA7传递给Powershell脚本(版本4.0)。我能够通过在Powershell编辑器中手动输入所有参数来运行PS脚本,并运行它来验证脚本是否有效。这是PS脚本:
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但没有任何反应。也许合成器不正确?我试过在这里和那里添加引号但没有成功。这是代码的简化版本:
Sub CallPowerShellWithArguments()
Call Shell("powershell.exe -ExecutionPolicy Bypass C:\Program Files (x86)\mailsend\send_reports.ps1 -pw Mypassword -from myemail1@mail.com -to myemail1@mail.com -subject MyEmail -body This is my test -file C:\mysql\test.tx")
End Sub
答案 0 :(得分:0)
不确定这是否是正确答案,但肯定是您可以尝试的。我建议为PowerShell.exe提供完整路径。例如,
C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe