运行多个脚本实例



我在操作中心工作,每台计算机上有10个屏幕。主计算机通过浏览器窗口中的chrome显示了许多遥测数据和仪表板,图形,动画以及许多其他有用的信息。每次重启动计算机时,这都是很乏味的,所以我很高兴找到这个原始项目:Chrome-kiosk来自https://alextomin.wordpress.com/2015/04/04/04/10/kiosk-mode-ing-mode-in-----mode-in---------Windows-Chrome-Multiple-Displays/

它在我们的运营中心中的自动起动器非常有效,因此我对其功能的这一方面感到满意。但是后来我开始思考:如果我能以某种方式暂时在板上显示一个网页,并且在设定的时间段后将其关闭,以便整个办公室可以监视重要的事件,这是否有用。

到目前为止,我想出的是很酷的,工作人员认为它非常有用,但是,它需要更多功能。这些很简单,但是我被困了,我一直在试图弄清楚这片刻。以下是我正在使用的代码。

我无法工作的事情:杀死计时器:我可以通过在我创建的杀戮功能前添加启动等待来使它起作用,但这使得looper.ps1脚本坐在启动器时等待。ps1完成。我如何在不制作looper脚本停止的情况下运行脚本直到其他脚本完成?

如何跟踪打开的每个新的Chrome实例?使用系统ID似乎不起作用,我能做的最好的就是获取chrome和最新实例,这意味着我可以杀死比计时器价值年轻的Chrome实例,但是我正在努力获得这可以工作。

这里的任何建议将不胜感激。

服务器脚本

我将它们放入一个称为C: Scripts iCvt

的文件夹中

looper.ps1-这将不断运行

Set-Location -Path C:scriptsICVT
while ($true) {
    .file_checker.ps1;
}

file_checker.ps1-这是Looper运行的脚本。File_Checker扫描Web.txt和mon.txt的文件夹。两者都必须出现才能执行脚本。

#Checks folder for web.txt and mon.txt . Both must be present for the rest of the script to execute
Set-Location -Path C:scriptsICVT
$a = Test-Path web.txt
$b = Test-Path mon.txt
if (($a -and $b -eq $True)) {
    .launcher.ps1
} else {
    Write-Host "Scanning For Files"
} 
Start-Sleep -Seconds 5

launcher.ps1-这只是原始脚本的修改版本

$chromePath = 'C:Program Files (x86)GoogleChromeApplicationchrome.exe'
$chromeArguments = '--new-window'
$web = (Get-Content -Path web.txt)
$mon = (Get-Content -Path mon.txt)
$timer = (Get-Content -Path $timer.txt)
# if Window not moved (especially on machine start) - try increasing the 
# delay. 
$ChromeStartDelay = 5
Set-Location $PSScriptRoot
. .HelperFunctions.ps1
Chrome-Kiosk $web -MonitorNum $mon
#Delete parameters after use
Start-Sleep -Seconds 5
del web.txt
del mon.txt
del timer.txt

这是通过网络创建Web.txt,timer.txt和mon.txt文件的GUI

Set-Location -Path \networkpcfolder
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# Hide PowerShell Console
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)
function button ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) {
    $Form = New-Object "system.Windows.Forms.Form";
    $Form.ClientSize = '653,436'
    $Form.text = "Display Board URL Tool"
    $Form.TopMost = $false
    $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
    $iconBytes       = [Convert]::FromBase64String($iconBase64)
    $stream          = New-Object IO.MemoryStream($iconBytes, 0, $iconBytes.Length)
    $stream.Write($iconBytes, 0, $iconBytes.Length);
    $iconImage       = [System.Drawing.Image]::FromStream($stream, $true)
    $Form.Icon       = [System.Drawing.Icon]::FromHandle((New-Object System.Drawing.Bitmap -Argument $stream).GetHIcon())
    $Button1 = New-Object "system.Windows.Forms.Button";
    $Button1.text = "Screen 1"
    $Button1.width = 132
    $Button1.height = 77
    $Button1.location = New-Object "System.Drawing.Point"(8,14);
    $Button1.Font = 'Microsoft Sans Serif,10'
    $Button2 = New-Object "system.Windows.Forms.Button";
    $Button2.text = "Screen 2"
    $Button2.width = 132
    $Button2.height = 77
    $Button2.location = New-Object "System.Drawing.Point"(165,14);
    $Button2.Font = 'Microsoft Sans Serif,10'
    $Button3 = New-Object "system.Windows.Forms.Button";
    $Button3.text = "Screen 3"
    $Button3.width = 132
    $Button3.height = 77
    $Button3.location = New-Object "System.Drawing.Point"(326,14);
    $Button3.Font = 'Microsoft Sans Serif,10'
    $Button4 = New-Object "system.Windows.Forms.Button";
    $Button4.text = "Screen 4"
    $Button4.width = 132
    $Button4.height = 77
    $Button4.location = New-Object "System.Drawing.Point"(483,15);
    $Button4.Font = 'Microsoft Sans Serif,10'
    $WinForm1 = New-Object "system.Windows.Forms.Form";
    $WinForm1.ClientSize = '653,400'
    $WinForm1.text = "Form"
    $WinForm1.TopMost = $false
    $Button5 = New-Object "system.Windows.Forms.Button";
    $Button5.text = "Screen 5"
    $Button5.width = 132
    $Button5.height = 77
    $Button5.location = New-Object "System.Drawing.Point"(8,117);
    $Button5.Font = 'Microsoft Sans Serif,10'
    $Button6 = New-Object "system.Windows.Forms.Button";
    $Button6.text = "Screen 6"
    $Button6.width = 132
    $Button6.height = 77
    $Button6.location = New-Object "System.Drawing.Point"(165,119);
    $Button6.Font = 'Microsoft Sans Serif,10'
    $Button7 = New-Object "system.Windows.Forms.Button";
    $Button7.text = "Screen 7"
    $Button7.width = 132
    $Button7.height = 77
    $Button7.location = New-Object "System.Drawing.Point"(326,119);
    $Button7.Font = 'Microsoft Sans Serif,10'
    $Button8 = New-Object "system.Windows.Forms.Button";
    $Button8.text = "Screen 8"
    $Button8.width = 132
    $Button8.height = 77
    $Button8.location = New-Object "System.Drawing.Point"(483,119);
    $Button8.Font = 'Microsoft Sans Serif,10'
    $Button9 = New-Object "system.Windows.Forms.Button";
    $Button9.text = "Screen 9"
    $Button9.width = 132
    $Button9.height = 77
    $Button9.location = New-Object "System.Drawing.Point"(9,220);
    $Button9.Font = 'Microsoft Sans Serif,10'
    $Button10 = New-Object "system.Windows.Forms.Button";
    $Button10.text = "Screen 10"
    $Button10.width = 132
    $Button10.height = 77
    $Button10.location = New-Object "System.Drawing.Point"(165,220);
    $Button10.Font = 'Microsoft Sans Serif,10'
    $Button11 = New-Object "system.Windows.Forms.Button";
    $Button11.text = "Screen 11"
    $Button11.width = 132
    $Button11.height = 77
    $Button11.location = New-Object "System.Drawing.Point"(326,220);
    $Button11.Font = 'Microsoft Sans Serif,10'
    $TextBox1 = New-Object "system.Windows.Forms.TextBox";
    $TextBox1.multiline = $false
    $TextBox1.width = 400
    $TextBox1.height = 20
    $TextBox1.location = New-Object "System.Drawing.Point"(220,314);
    $TextBox1.Font = 'Microsoft Sans Serif,10'
    $TextBox2 = New-Object "system.Windows.Forms.TextBox";
    $TextBox2.multiline = $false
    $TextBox2.width = 50
    $TextBox2.height = 20
    $TextBox2.location = New-Object "System.Drawing.Point"(220,348);
    $TextBox2.Font = 'Microsoft Sans Serif,10'
    $Label1 = New-Object "system.Windows.Forms.Label";
    $Label1.text = "Display Time (Minutes)"
    $Label1.AutoSize = $true
    $Label1.width = 25
    $Label1.height = 10
    $Label1.location = New-Object "System.Drawing.Point"(15,348);
    $Label1.Font = 'Microsoft Sans Serif,10'
    $Label2 = New-Object "system.Windows.Forms.Label";
    $Label2.text = "URL for Timed Monitoring"
    $Label2.AutoSize = $true
    $Label2.width = 25
    $Label2.height = 10
    $Label2.location = New-Object "System.Drawing.Point"(15,314);
    $Label2.Font = 'Microsoft Sans Serif,10'
    $Button12 = New-Object "system.Windows.Forms.Button";
    $Button12.text = "Kill Current Screen"
    $Button12.width = 132
    $Button12.height = 77
    $Button12.location = New-Object "System.Drawing.Point"(483,220);
    $Button12.Font = 'Microsoft Sans Serif,10'
    $Button12.ForeColor = "#d0021b"
    $Form.controls.AddRange(@($Button1,$Button2,$Button3,$Button4,$Button5,$Button6,$Button7,$Button8,$Button9,$Button10,$Button11,$TextBox1,$TextBox2,$Label1,$Label2,$Button12))
    ############# This is when you have to close the form after getting values
    $eventHandler1 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '1'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $eventHandler2 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '2'
    $text | Set-Content 'mon.txt'
    $form.Close();};

    $eventHandler3 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '3'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $eventHandler4 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '4'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $eventHandler5 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '5'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $eventHandler6 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '6'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $eventHandler7 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '7'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $eventHandler8 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '8'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $eventHandler9 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '9'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $eventHandler10 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '10'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $eventHandler11 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Create mon.txt per button
    $text = '11'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $eventHandler12 = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    ############# Kill the last Screen that was launched
    $text = '1'
    $text | Set-Content 'mon.txt'
    $form.Close();};
    $button1.Add_Click($eventHandler1) ;
    $button2.Add_Click($eventHandler2) ;
    $button3.Add_Click($eventHandler3) ;
    $button4.Add_Click($eventHandler4) ;
    $button5.Add_Click($eventHandler5) ;
    $button6.Add_Click($eventHandler6) ;
    $button7.Add_Click($eventHandler7) ;
    $button8.Add_Click($eventHandler8) ;
    $button9.Add_Click($eventHandler9) ;
    $button10.Add_Click($eventHandler10) ;
    $button11.Add_Click($eventHandler11) ;
    $button12.Add_Click($eventHandler12) ;
    #############Add controls to all the above objects defined
    $form.Controls.Add($button1);
    $form.Controls.Add($button2);
    $form.Controls.Add($button3);
    $form.Controls.Add($button4);
    $form.Controls.Add($button5);
    $form.Controls.Add($button6);
    $form.Controls.Add($button7);
    $form.Controls.Add($button8);
    $form.Controls.Add($button9);
    $form.Controls.Add($button10);
    $form.Controls.Add($button11);
    $form.Controls.Add($textLabel1);
    $form.Controls.Add($textLabel2);
    $form.Controls.Add($textBox1);
    $form.Controls.Add($textBox2);
    $ret = $form.ShowDialog();
    #################return values
    return $textBox1.Text, $textBox2.Text
}
#Creates the 3 txt files for web, mon and timer
$return = button "Monitoring Screen Selector" "Enter URL" "Enter Screen # from 1 to 11" 
if ($return[0] -ne "") {
    $return[0] > web.txt
}
if ($return[0] -eq "") {
    exit
}
if ($return[1] -ne "") {
    $return[1] > timer.txt
}
if ($return[1] -eq "") {
    exit
}
#multiply by 60 to get minutes
if (Test-Path timer.txt) {
    if ((Get-Item timer.txt).Length -gt 0kb) {
        $result = Get-Content timer.txt
        60 * $result > timer.txt
    }
}

为了执行许多功能,您首先需要为Chrome窗口获得一个流程ID。为此,我们需要对Chrome-Kiosk功能进行一些更改以返回该信息。首先,我将Start-Process更改为包括-PassThru参数。这样,我得到一个可以从中获得pid的过程对象。对Get-Process的另一项编辑用于使用ID代替" Chrome",最后我们Write-Output $Process对象。

function Chrome-Kiosk($Url, $MonitorNum)
{
    Write-Output "starting chrome $Url , monitor: $MonitorNum"
    $Process = Start-Process $chromePath "$chromeArguments $Url" -PassThru
    Start-Sleep -Seconds $ChromeStartDelay
    $window = (Get-Process -Id $Process.Id | where MainWindowHandle -ne ([IntPtr]::Zero) | select -First 1).MainWindowHandle
    $WinAPI::ShowWindow($window, [Tomin.Tools.KioskMode.Enums.ShowWindowCommands]::Restore)
    $Helpers::MoveToMonitor($window, $MonitorNum)
    $Helpers::SendKey($window, '{F11}')
    Start-Sleep -Seconds $ChromeStartDelay
    Write-Output $Process
}

其余的工作是修改启动器脚本以使用杀死计时器。本质上,我们将.txt删除部分拉回您的 file_checker.PS1脚本,以便我们可以启动launcher.ps1作为新的自我包含的PowerShell进程。

file_checker.ps1

#Checks folder for web.txt and mon.txt . Both must be present for the rest of the script to execute
Set-Location -Path C:scriptsICVT
$a = Test-Path web.txt
$b = Test-Path mon.txt
IF (($a -and $b -eq $True)) {
    . .launcher.ps1
    #Delete parameters after use
    del web.txt
    del mon.txt
    del timer.txt
} 
else {
    Write-Host "Scanning For Files"
} 
Start-Sleep -Seconds 5

通过使用修改的Chrome-Kiosk函数,它返回Chrome窗口的过程对象,我们现在可以使用启动器脚本启动,睡眠,然后仅杀死我们启动的Chrome窗口:

启动器.ps1

$chromePath = 'C:Program Files (x86)GoogleChromeApplicationchrome.exe'
$chromeArguments = '--new-window'
$web = (Get-Content -Path web.txt)
$mon = (Get-Content -Path mon.txt)
$timer = (Get-Content -Path $timer.txt)
# if Window not moved (especially on machine start) - try increasing the 
# delay. 
$ChromeStartDelay = 5
Set-Location $PSScriptRoot
. .HelperFunctions.ps1
$Process = Chrome-Kiosk $web -MonitorNum $mon
#Wait some period of time
Start-Sleep -Seconds 60
#Kill the process
Stop-Process $Process

最新更新