我需要将一些参数从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正在从宏调用,但没有任何反应。也许synthax是不正确的?我已经尝试在这里和那里添加引号,但没有成功。以下是代码的简化版本:
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发布于 2016-03-02 06:15:34
我不确定这是否是正确的答案,但这绝对是你可以尝试的东西。我建议提供PowerShell.exe的完整路径。例如,
C:\Windows\System32\WindowsPowershell\v1.0\powershell.exehttps://stackoverflow.com/questions/35733964
复制相似问题