0

I have a need to execute a batch file when I drop it into a specific folder of one of my Windows PCs.

We have 20 PCs spread out thruout the US. I can remote into each PC and execute the batch file on the local PC but that takes a long time.

I can also do a file transfer into each of these PCs very quickly. If I can get the batch file to execute once it is placed in a specific folder that would solve my problem.

I've heard of listeners in the past. Where "something" on the PC is just looking at the contents of a folder. If a file happens to exist in that folder the listener executes "something".

Here's some pseudocode to help get a better understanding:

\\Program watches the contents of C:\Folder1
If 
   file batchfile.bat exists in C:\Folder1
     then execute batchfile.bat and then delete batchfile.bat

Else
   continue watching C:\Folder1 for batchfile.bat

I'm digging around SO and looks like PowerShell is going to be the path I need to take. Do you guys agree or is there a more efficient solution?

4

2 に答える 2

0

powershell はあなたが探しているものではないと確信しています。ペイロードのバッチの代わりに powershell をお勧めしますが、この種のスケジュールされたタスクには理想的ではありません。

代わりに、1 つの 2 つのパスをお勧めします。

  • 遅延に耐えることができる場合は、バッチまたは PowerShell スクリプトを頻繁に実行して、特定のフォルダーをチェックし、見つかったものを実行することができます。
  • すぐに満足したい場合は、常に実行され、この方法でリスナーを簡単に処理できる Windows サービスが最適です。
于 2012-07-11T17:19:45.477 に答える
0

これが私がそれを行う方法です:

param([switch]$install)

$batchfile = "C:\Folder1\batchfile.bat"

if($install)
{
    invoke-expression ("schtasks -create -tn `"Job1`" -tr `"powershell -file '" + $myinvocation.mycommand.path + "'`" -sc DAILY -st 05:00 -rl HIGHEST -ru SYSTEM")
}
else
{
    if(test-path $batchfile)
    {
        cmd /c batchfile.bat
        remove-item $batchfile | out-null
    }
}

「-install」スイッチを指定して上記のスクリプトを実行すると、1 日 1 回、午前 5:00 に実行されるようにスケジュールされたタスクが作成されます。より頻繁に実行したい場合は、schtasksの構文を確認して変更してください。

于 2012-07-11T17:21:13.460 に答える