【发布时间】:2023-03-27 16:48:01
【问题描述】:
在过去的几天里,我一直在整理一个 Powershell 脚本,以在 mcpcmag's articule 的帮助下监视我的 C 盘上的特定文件夹中的新文件。当最终用户请求电子邮件时,将创建新文件。新文件是一个 .sh 文件,其中包含一个 cURL 命令,该命令向SendGrid API 发送一个发布请求。
这是下面的 powershell 脚本:
# Create a listener for our emailOut directory
$watcher = New-Object System.IO.FileSystemWatcher
# Set the path and turn on the listener
$watcher.Path = 'C:\emailOut'
$watcher.EnableRaisingEvents = $true
$action =
{
# set the shellScript to the full path of the added file
$shellScript = $event.SourceEventArgs.fullPath
# get the name of the file
$name = (Get-Item $shellScript).Name
# set the path which the file will be moved to on completion
$oldFolderDir = "C:\emailOut\old\" + $name
# run the script
& $shellScript
# set the log file path
$logFile = "C:\emailOut\emailLog_" + $(get-date -format FileDate) + ".txt"
# set the log message
$logInfo = $name + " ---> " + $(get-date)
# add entry to log file
$logInfo | Out-File $logFile -Append -encoding utf8
# Move the script file into the historic folder
Move-Item -Path $shellScript -Destination $oldFolderDir
}
# Start the watcher
Register-ObjectEvent $watcher 'Created' -Action $action
cmets 使该过程相当不言自明,但为了简要描述,以下是发生(或应该发生)的情况:
- 文件进入被监视的文件夹
- Shell 脚本正在运行
- 条目已添加到日志文件中
- 文件已移至旧文件夹
- 完成
不幸的是,我运行 shell 脚本的方式似乎有问题。运行该过程时,很明显脚本正在运行,黑框出现了几分之一秒,但没有发送电子邮件。我检查了原始脚本,没有任何问题,因为当我双击 .sh 文件时,电子邮件已完美发送。
有什么想法吗?
提前感谢您的任何帮助或建议。
【问题讨论】:
标签: powershell curl sendgrid