从VBA运行PowerShell FTP上载脚本,并发出带有结果的消息框

时间:2018-05-29 13:47:36

标签: vba powershell ftp

我有一个基本的FTP上传PowerShell代码,我通过Excel中的VBA运行(隐藏):

VBA:

Call Shell("powershell -executionpolicy bypass & """ & ThisWorkbook.Path & "\FTP\FTPUpload.ps1""", vbHide)

FTPUpload.ps1:

$File = "H:\Workbook\file.txt"
$ftp ="ftp://user:pass@ftp.site.org/incoming/file.txt"

"ftp url: $ftp"

$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)

"Uploading $File..."

$webclient.UploadFile($uri, $File)

我希望能够向用户显示通过/失败消息。这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:1)

使PowerShell脚本通过退出代码发出结果信号:

try
{
    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)

    "Uploading $File..."

    $webclient.UploadFile($uri, $File)

    exit 0
}
catch
{
    exit 1
}

修改您的VBA代码以等待退出代码并采取相应行动 见VBA Shell and Wait with Exit Code