vba - 从 VBA 运行 PowerShell FTP 上传脚本并发出带有结果的消息框
问题描述
我有一个基本的 FTP 上传 PowerShell 代码,我通过 Excel 中的 VBA 运行(隐藏):
VBA:
Call Shell("powershell -executionpolicy bypass & """ & ThisWorkbook.Path & "\FTP\FTPUpload.ps1""", vbHide)
FTP上传.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)
我希望能够向用户显示通过/失败消息。做这个的最好方式是什么?
解决方案
使 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 并等待退出代码
推荐阅读
- android - 当值从 onActivityResult() 更改时,观察者没有触发
- r - 我可以在后续函数调用中评估参数吗?
- selenium-webdriver - Pyinstaller - 当代码包含 Selenium webdriver 时无法执行脚本
- azure-devops - 从 Azure DevOps 导出讨论笔记
- python-3.x - 如何将响应发送回发起者
- directx - DirectX 计算着色器 (HLSL):如何访问字符串中的单个字符?
- python-3.x - Pandas 无法正确解析逗号分隔的文件
- json - ruby JSON.parse ParseError 意外令牌
- python - Gensim Doc2Vec:生成的向量少于预期
- javascript - 将 Javascript 库与 parcel 一起使用会导致“ReferenceError:XXX 未定义”