从脚本调用函数并使其在原始脚本中运行

时间:2014-06-11 15:42:05

标签: function powershell

我有两个单独的PowerShell脚本需要同时运行。一个是信息亭,另一个是检测空闲时间并在信息亭脚本中调用函数的脚本。该功能旨在关闭自助服务终端并重新打开它以清除任何表单数据。唯一的问题是它没有关闭原始信息亭而是打开一个新实例。

我已经尝试将这些脚本合并为一个,但却没有运气让它发挥作用。

这是空闲时间脚本:

Add-Type @'
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace PInvoke.Win32 {

public static class UserInput {

    [DllImport("user32.dll", SetLastError=false)]
    private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

    [StructLayout(LayoutKind.Sequential)]
    private struct LASTINPUTINFO {
        public uint cbSize;
        public int dwTime;
    }

    public static DateTime LastInput {
        get {
            DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);
            DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks);
            return lastInput;
        }
    }

    public static TimeSpan IdleTime {
        get {
            return DateTime.UtcNow.Subtract(LastInput);
        }
    }

    public static int LastInputTicks {
        get {
            LASTINPUTINFO lii = new LASTINPUTINFO();
            lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
            GetLastInputInfo(ref lii);
            return lii.dwTime;
        }
    }
}
}
'@

    for ( $i = 1)
    {Write-Verbose ("Idle for " + [PInvoke.Win32.UserInput]::IdleTime)
    Start-Sleep -Seconds 1

    if ([PInvoke.Win32.UserInput]::IdleTime -gt "00:00:10.0000000")
    {Stop-Process -processname kiosk*
    C:\kiosk.exe MakeNewForm
}

用于测试的空闲时间设置为10秒。在生产中当然会更长。

这是自助服务终端脚本:

Set-ExecutionPolicy Unrestricted
$URL = "www.website.com"
$event = {$Browser.Print()}

Add-Type -AssemblyName System.Windows.Forms

#Browser Refresh
Function MakeNewForm {
    $mainForm.close()
    $mainForm.dispose() 
    Start-Sleep -Milliseconds 100

    # WinForm
    $mainForm = New-Object System.Windows.Forms.Form
    $mainForm.Text = " Internet Explorer"
    $mainform.WindowState = "maximized"
    $mainForm.ControlBox = $false
    $mainForm.FormBorderStyle = "none"
    $mainForm.MinimumSize =  "1024, 768"
    $mainForm.Top = $true


    # Back
    $back = New-Object System.Windows.Forms.Button
    $back.Size = "55, 55"
    $back.Location = "875, 0"
    $back.BackgroundImage = [System.Drawing.Image]::FromFile('C:\back_button.jpg')
    $back.Add_Click({$Browser.GoBack()})

    $mainForm.Controls.Add($back)

    # Forward
    $forward = New-Object System.Windows.Forms.Button
    $forward.Size = "55, 55"
    $forward.location = "953, 0"
    $forward.BackgroundImage = [System.Drawing.Image]::FromFile('C:\forward_button.jpg')
    $forward.Add_Click({$browser.GoForward()})

    $mainForm.Controls.Add($forward)

    # Go Home
    $gohome = New-Object System.Windows.Forms.Button
    $gohome.Size = "55, 55"
    $gohome.location = "75, 0"
    $gohome.BackgroundImage = [System.Drawing.Image]::FromFile('C:\home_button.jpg')
    $gohome.Add_Click({$browser.URL= "$URL"})

    $mainForm.Controls.Add($gohome)

    # Print
    $button = New-Object System.Windows.Forms.Button 
    $button.Size = "54, 55"
    $button.Location = "0, 0"
    $button.BackgroundImage = [System.Drawing.Image]::FromFile('C:\print.jpg')
    $button.Add_Click($event)

    $mainForm.Controls.Add($button)


    # Browser
    $Browser = New-Object System.Windows.Forms.WebBrowser
    $Browser.IsWebBrowserContextMenuEnabled = $false
    $Browser.URL = $URL
    $Browser.Location = "0, 0"
    $Browser.Dock = "fill"


    $mainForm.Controls.Add($Browser)
    }



# WinForm
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Text = " Internet Explorer"
$mainform.WindowState = "maximized"
$mainForm.ControlBox = $false
$mainForm.FormBorderStyle = "none"
$mainForm.MinimumSize =  "1024, 768"
$mainForm.Top = $true


# Back
$back = New-Object System.Windows.Forms.Button
$back.Size = "55, 55"
$back.Location = "875, 0"
$back.BackgroundImage = [System.Drawing.Image]::FromFile('C:\back_button.jpg')
$back.Add_Click({$Browser.GoBack()})

$mainForm.Controls.Add($back)

# Forward
$forward = New-Object System.Windows.Forms.Button
$forward.Size = "55, 55"
$forward.location = "953, 0"
$forward.BackgroundImage = [System.Drawing.Image]::FromFile('C:\forward_button.jpg')
$forward.Add_Click({$browser.GoForward()})

$mainForm.Controls.Add($forward)

# Go Home
$gohome = New-Object System.Windows.Forms.Button
$gohome.Size = "55, 55"
$gohome.location = "75, 0"
$gohome.BackgroundImage = [System.Drawing.Image]::FromFile('C:\home_button.jpg')
$gohome.Add_Click({$browser.URL= "$URL"})

$mainForm.Controls.Add($gohome)

# Print
$button = New-Object System.Windows.Forms.Button 
$button.Size = "54, 55"
$button.Location = "0, 0"
$button.BackgroundImage = [System.Drawing.Image]::FromFile('C:\print.jpg')
$button.Add_Click($event)

$mainForm.Controls.Add($button)


# Browser
$Browser = New-Object System.Windows.Forms.WebBrowser
$Browser.IsWebBrowserContextMenuEnabled = $false
$Browser.URL = $URL
$Browser.Location = "0, 0"
$Browser.Dock = "fill"


$mainForm.Controls.Add($Browser)

$mainForm.ShowDialog() | Out-Null

我试过了。 。\ idle.ps1,do {} while(),将空闲脚本组合到kiosk脚本的末尾,但在组合它们时没有任何效果。

是否有办法将这些组合或让空闲实际调用该函数并让它在kiosk脚本中运行?

提前感谢您的回复或建议!

2 个答案:

答案 0 :(得分:0)

你的方法很健全。你需要的是将kiosk脚本的进程id写入持久性的东西,即文本文件。然后,如果需要,可以将其用于空闲脚本以引用并终止原始进程。可以通过内置变量$ pid来访问Powershell会话的当前进程ID。

保存当前会话的进程ID

$pid | Out-File -FilePath c:\kiosk-pid.txt

稍后访问它并使用它来终止进程

Get-Process -PID ([int32](Get-Content -Path C:\kiosk-pid.txt)) | Stop-Process

答案 1 :(得分:0)

我完全忘记提到这些将作为.exe运行。我加入的是 stop-process -processname kiosk * ,它现在正在完美运行。再次感谢MFT指出我正确的方向。我将更新上面的脚本以显示更改。

if ([PInvoke.Win32.UserInput]::IdleTime -gt "00:00:10.0000000")
{Stop-Process -processname kiosk*
C:\kiosk.exe MakeNewForm