运行 通过自动 Powershell 文件夹侦听器的 shell 脚本
Running a shell script through an automated Powershell folder listener
在过去的几天里,我一直在编写一个 Powershell 脚本,在 mcpcmag's articule. The new files will be created when an email is requested by the end user. The new file is a .sh file containing a cURL command which sends a post request to the 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
这些评论使该过程相当不言自明,但为了简要描述,这是发生(或应该发生)的情况:
- 文件进入正在观看的文件夹
- Shell 脚本是 运行
- 条目已添加到日志文件
- 文件被移动到旧文件夹
- 完成
不幸的是,我 运行 编写 shell 脚本的方式似乎有问题。当 运行ning 这个过程时,很明显脚本是 运行ning 作为一个黑框出现了几分之一秒但没有发送电子邮件。我已经检查了原始脚本,没有任何问题,因为当我双击 .sh 文件时,电子邮件已完美发送。
有什么想法吗?
在此先感谢您的帮助或建议。
我认为你的逻辑有问题:
File goes into folder that is being watched
[PowerShell]Shell script is run
问题是在您调用 Register-ObjectEvent
后,您的脚本结束,powershell 进程终止,这会隐式删除 FileSystemWatcher。
您应该一直保留脚本 运行ning 'catch' 事件。
你可以运行它通过计划任务,选项Do not start a new instance
如果有实例运行ning并设置为每N次重复分钟 (在出现故障或有人终止进程等情况下恢复脚本)。此 PowerShell 实例必须 运行 不断获取事件。
$eventSI = 'CustomSI_FSWCreated'
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = 'S:\SCRIPTS\FileTest'
$watcher.EnableRaisingEvents = $true
$action = { [System.Console]::WriteLine($event.SourceEventArgs.fullPath) }
# Start the watcher
Register-ObjectEvent -InputObject $watcher -EventName 'Created' -Action $action -SourceIdentifier $eventSI | Out-Null
try
{
$l = 1;
while($true)
{
# Actually this timeout only sets the frequence of dots on your screen. Set it bigger if you want or remove Write-Host-s
Wait-Event -Timeout 1
Write-Host "." -NoNewline
$l++;
if ($l -ge 40)
{
Write-Host ""
$l = 1;
}
}
}
finally
{
# This executes when you press Ctrl+C
Unregister-Event -SourceIdentifier $eventSI
$watcher.EnableRaisingEvents = $false
$watcher.Dispose()
Write-Host "Unregistered FSWE"
}
只是想分享我最终的工作解决方案。
我确实在 运行 脚本之前尝试了 Start-Sleep -seconds 5
,但遗憾的是没有达到预期的结果。
经过反复试验,我得出了一个可行的解决方案:
# 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
#### SOLUTION START ###
#go to where bash.exe is stored
cd "C:\Program Files\Git\bin"
# run the script
& .\bash.exe $shellScript
### SOLUTION END ###
# 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
基本上,我通过将 cURL 脚本传递给 git bash.exe 运行 我的脚本。
如有任何进一步的疑问或问题,请随时与我联系。
帕特里克
在过去的几天里,我一直在编写一个 Powershell 脚本,在 mcpcmag's articule. The new files will be created when an email is requested by the end user. The new file is a .sh file containing a cURL command which sends a post request to the 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
这些评论使该过程相当不言自明,但为了简要描述,这是发生(或应该发生)的情况:
- 文件进入正在观看的文件夹
- Shell 脚本是 运行
- 条目已添加到日志文件
- 文件被移动到旧文件夹
- 完成
不幸的是,我 运行 编写 shell 脚本的方式似乎有问题。当 运行ning 这个过程时,很明显脚本是 运行ning 作为一个黑框出现了几分之一秒但没有发送电子邮件。我已经检查了原始脚本,没有任何问题,因为当我双击 .sh 文件时,电子邮件已完美发送。
有什么想法吗?
在此先感谢您的帮助或建议。
我认为你的逻辑有问题:
File goes into folder that is being watched
[PowerShell]Shell script is run
问题是在您调用 Register-ObjectEvent
后,您的脚本结束,powershell 进程终止,这会隐式删除 FileSystemWatcher。
您应该一直保留脚本 运行ning 'catch' 事件。
你可以运行它通过计划任务,选项Do not start a new instance
如果有实例运行ning并设置为每N次重复分钟 (在出现故障或有人终止进程等情况下恢复脚本)。此 PowerShell 实例必须 运行 不断获取事件。
$eventSI = 'CustomSI_FSWCreated'
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = 'S:\SCRIPTS\FileTest'
$watcher.EnableRaisingEvents = $true
$action = { [System.Console]::WriteLine($event.SourceEventArgs.fullPath) }
# Start the watcher
Register-ObjectEvent -InputObject $watcher -EventName 'Created' -Action $action -SourceIdentifier $eventSI | Out-Null
try
{
$l = 1;
while($true)
{
# Actually this timeout only sets the frequence of dots on your screen. Set it bigger if you want or remove Write-Host-s
Wait-Event -Timeout 1
Write-Host "." -NoNewline
$l++;
if ($l -ge 40)
{
Write-Host ""
$l = 1;
}
}
}
finally
{
# This executes when you press Ctrl+C
Unregister-Event -SourceIdentifier $eventSI
$watcher.EnableRaisingEvents = $false
$watcher.Dispose()
Write-Host "Unregistered FSWE"
}
只是想分享我最终的工作解决方案。
我确实在 运行 脚本之前尝试了 Start-Sleep -seconds 5
,但遗憾的是没有达到预期的结果。
经过反复试验,我得出了一个可行的解决方案:
# 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
#### SOLUTION START ###
#go to where bash.exe is stored
cd "C:\Program Files\Git\bin"
# run the script
& .\bash.exe $shellScript
### SOLUTION END ###
# 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
基本上,我通过将 cURL 脚本传递给 git bash.exe 运行 我的脚本。 如有任何进一步的疑问或问题,请随时与我联系。
帕特里克