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