2014-09-23 89 views
2

我正在研究一個腳本,對某個Programm進行訪問控制,在這個例子中計算器如何在PowerShell中關閉Windows窗體?

當您啓動程序(腳本)時,腳本將生成一個帶有用戶名和時間戳的文本文件。如果第二個人試圖訪問,腳本會檢查這個文本文件是否存在,並提供用戶名和時間戳,以便稍後嘗試。如果文件不存在,您可以訪問該程序並生成文件。當你關閉程序時,文件將被刪除。

我正在使用一些Windows窗體來包含一個ingnore函數並退出腳本。

問題是,當你點擊兩個窗體上的忽略按鈕後,窗體不會關閉。

我不知道如何解決它。

代碼:

##### further functions ##### 

Function Warnung { 
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") 

$Form1.text = "Warning!" 
$Form1.Width = 300 
$Form1.Height = 200 

$Text = New-Object Windows.Forms.Label 
$Text.Location = New-Object Drawing.Point 75,30 
$Text.Size = New-Object Drawing.Point 200,30 
$Text.text = "Am $timestamp hat sich" 

$Text2 = New-Object Windows.Forms.Label 
$Text2.Location = New-Object Drawing.Point 75,60 
$Text2.Size = New-Object Drawing.Point 200,30 
$Text2.text = "$user" 

$Text3 = New-Object Windows.Forms.Label 
$Text3.Location = New-Object Drawing.Point 75,90 
$Text3.Size = New-Object Drawing.Point 200,30 
$Text3.text = "im calculator angemeldet!" 

$Close = New-Object Windows.Forms.Button 
$Close.text = "Close" 
$Close.Location = New-Object Drawing.Point 20,120 
$Close.add_click({$Form1.Close()}) 

$Taskmgr = New-Object Windows.Forms.Button 
$Taskmgr.text = "Taskmgr" 
$Taskmgr.Location = New-Object Drawing.Point 105,120 
$Taskmgr.add_click({Taskmgr}) 

$Ignore = New-Object Windows.Forms.Button 
$Ignore.text = "Ignore" 
$Ignore.Location = New-Object Drawing.Point 190,120 
$Ignore.add_click({Warning2}) 

$Form1.controls.add($Close) 
$Form1.controls.add($Taskmgr) 
$Form1.controls.add($Ignore) 
$Form1.controls.add($Text) 
$Form1.controls.add($Text2) 
$Form1.controls.add($Text3) 
$Form1.ShowDialog() 
} 

Function Taskmgr{ 
$Form1.Close() 
Start-process taskmgr.exe 
} 

Function Ign2{ 
$Form2.Close() 
$Form1.Close() 
Remove-Item C:\users\heisem\desktop\test.txt 
$date = Get-Date 
$file = "C:\users\heisem\desktop\test.txt" 
$env:username | set-content $file 
$date | add-content $file 
} 

Function Warning2 { 

$Form2.text = "Warnung!" 
$Form2.Width = 300 
$Form2.Height = 200 

$Text4 = New-Object Windows.Forms.Label 
$Text4.Location = New-Object Drawing.Point 40,30 
$Text4.Size = New-Object Drawing.Point 200,70 
$Text4.text = "Sind Sie sich sicher, dass Sie $user übergehen möchten?" 
       "Haben Sie $user angesprochen 
       ob diese(r) den Passwortsafe verlassen kann/hat?" 

$Close2 = New-Object Windows.Forms.Button 
$Close2.text = "Close" 
$Close2.Location = New-Object Drawing.Point 20,120 
$Close2.add_click({$Form2.Close()}) 

$Ignore2 = New-Object Windows.Forms.Button 
$Ignore2.text = "Ignore" 
$Ignore2.Location = New-Object Drawing.Point 190,120 
$Ignore2.add_click({Ign2}) 

$Form2.controls.add($Close2) 
$Form2.controls.add($Ignore2) 
$Form2.controls.add($Text4) 
$Form2.ShowDialog() 
}    

##### Main function ##### 

[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") 
$Form1 = New-Object Windows.Forms.Form 
$Form2 = New-Object Windows.Forms.Form  

if (Test-Path C:\users\heisem\desktop\test.txt) { 

$user = Get-content "C:\users\heisem\desktop\test.txt" -totalcount 1 
$timestamp = Get-Content "C:\users\heisem\desktop\test.txt" | Select-Object -Skip 1 
Warnung 
Start-Process calc.exe -Wait     
Remove-Item C:\users\heisem\desktop\test.txt 

} else { 

$datum = Get-Date 
$datei = "C:\users\heisem\desktop\test.txt" 
$env:username | set-content $datei 
$datum | add-content $datei 
Start-Process calc.exe -Wait 
Remove-Item C:\users\heisem\desktop\test.txt  
} 
+0

即時猜測你有一個範圍的問題是'$ Form1'和'$ Form2'在一個函數創建的,但你是在試圖訪問他們在另一個。這些表格不可用於'Ign2' – Matt 2014-09-23 11:26:10

+0

這也是我的想法,但沒有使用函數沒有好處。我編輯了上面的代碼。 – freakies 2014-09-23 12:53:24

回答

0

它是與你打電話calc.exe時使用-Wait參數。您不會遇到因爲您不使用-Wait而致電taskmgr.exe的功能中的問題。

我也將$Form1$Form2定義從本地函數作用域,所以每個可以有全局訪問它們。

相當瘋狂地猜測,我想-Wait參數不允許上述代碼完成執行,直到函數退出,即框架在您調用的進程存在時仍然需要存在某些引用。

##### further functions ##### 

Function Warnung { 
    [reflection.assembly]::LoadWithPartialName("System.Windows.Forms") 

    $Form1.text = "Warning!" 
    $Form1.Width = 300 
    $Form1.Height = 200 

    $Text = New-Object Windows.Forms.Label 
    $Text.Location = New-Object Drawing.Point 75,30 
    $Text.Size = New-Object Drawing.Point 200,30 
    $Text.text = "Am $timestamp hat sich" 

    $Text2 = New-Object Windows.Forms.Label 
    $Text2.Location = New-Object Drawing.Point 75,60 
    $Text2.Size = New-Object Drawing.Point 200,30 
    $Text2.text = "$user" 

    $Text3 = New-Object Windows.Forms.Label 
    $Text3.Location = New-Object Drawing.Point 75,90 
    $Text3.Size = New-Object Drawing.Point 200,30 
    $Text3.text = "im calculator angemeldet!" 

    $Close = New-Object Windows.Forms.Button 
    $Close.text = "Close" 
    $Close.Location = New-Object Drawing.Point 20,120 
    $Close.add_click({$Form1.Close()}) 

    $Taskmgr = New-Object Windows.Forms.Button 
    $Taskmgr.text = "Taskmgr" 
    $Taskmgr.Location = New-Object Drawing.Point 105,120 
    $Taskmgr.add_click({Taskmgr}) 

    $Ignore = New-Object Windows.Forms.Button 
    $Ignore.text = "Ignore" 
    $Ignore.Location = New-Object Drawing.Point 190,120 
    $Ignore.add_click({Warning2}) 

    $Form1.controls.add($Close) 
    $Form1.controls.add($Taskmgr) 
    $Form1.controls.add($Ignore) 
    $Form1.controls.add($Text) 
    $Form1.controls.add($Text2) 
    $Form1.controls.add($Text3) 
    $Form1.ShowDialog() 
} 

Function Taskmgr{ 
    $Form1.Close() ### IT WORKS HERE 
    Start-process taskmgr.exe 
} 

Function Ign2{ 
    $Form2.Close() ### HERE IS THE PROBLEM 
    $Form1.Close() ### THE FORMS DO NOT CLOSE 
    Remove-Item C:\users\heisem\desktop\test.txt 
    $date = Get-Date 
    $file = "C:\users\heisem\desktop\test.txt" 
    $env:username | set-content $file 
    $date | add-content $file 
    Start-Process calc.exe 
    Remove-Item C:\users\heisem\desktop\test.txt 
} 

Function Warning2 { 

    $Form2.text = "Warnung!" 
    $Form2.Width = 300 
    $Form2.Height = 200 

    $Text4 = New-Object Windows.Forms.Label 
    $Text4.Location = New-Object Drawing.Point 40,30 
    $Text4.Size = New-Object Drawing.Point 200,70 
    $Text4.text = "Sind Sie sich sicher, dass Sie $user übergehen möchten?" 
        "Haben Sie $user angesprochen 
        ob diese(r) den Passwortsafe verlassen kann/hat?" 

    $Close2 = New-Object Windows.Forms.Button 
    $Close2.text = "Close" 
    $Close2.Location = New-Object Drawing.Point 20,120 
    $Close2.add_click({$Form2.Close()}) 

    $Ignore2 = New-Object Windows.Forms.Button 
    $Ignore2.text = "Ignore" 
    $Ignore2.Location = New-Object Drawing.Point 190,120 
    $Ignore2.add_click({Ign2}) 

    $Form2.controls.add($Close2) 
    $Form2.controls.add($Ignore2) 
    $Form2.controls.add($Text4) 
    $Form2.ShowDialog() 
}    

##### Main function ##### 

[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") 
$Form1 = New-Object Windows.Forms.Form 
$Form2 = New-Object Windows.Forms.Form  

if (Test-Path C:\users\heisem\desktop\test.txt) { 

    $user = Get-content "C:\users\heisem\desktop\test.txt" -totalcount 1 
    $timestamp = Get-Content "C:\users\heisem\desktop\test.txt" | Select-Object -Skip 1 
    Warnung 

} else { 

    $datum = Get-Date 
    $datei = "C:\users\heisem\desktop\test.txt" 
    $env:username | set-content $datei 
    $datum | add-content $datei 
    Start-Process calc.exe -Wait 
    Remove-Item C:\users\heisem\desktop\test.txt  
} 
+0

我測試了你的代碼,但沒有解決問題。 – freakies 2014-09-23 12:55:18

+0

您是否需要'Form1'和'Form2' @ arco444 @freakies – Matt 2014-09-23 13:44:44

+0

matt前面需要'$' - 好了,謝謝。代表我複製錯誤。 @freakies,我在測試過程中包含了整個腳本,我也編寫了代碼。看看是否有幫助。一定要在全新的PowerShell窗口中進行測試。 – arco444 2014-09-23 13:47:23