markus2016
Goto Top

Script zur Installation von Programmen

Hallo zusammen,

seid langem habe ich nun endlich mal wieder Zeit gefunden mich mit Powershell zu beschäftigen.
Mein Script funktioniert auch soweit....
Ich finde nur keine Lösung den objForm.ShowDialog automatisch zu schließen, bzw. mit objForm.Show das eingebaute animierte *.gif bewegt darzustellen.
Gibt es für die eine, oder die andere Variante eine Möglichkeit?

Mein Ansatz funktioniert leider nicht.
Fehlermeldung: Für "ShowDialog" und die folgende Argumentezahl kann keine Überladung gefunden werden: "1"

$objForm.ShowDialog({if (Get-Job -state Completed){$objForm.Close()}}) | Out-Null

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")   
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  

$Bild =
@(
"$PSScriptRoot\Bilder\Bild_01.ico"  
"$PSScriptRoot\Bilder\Bild_02.png"  
"$PSScriptRoot\Bilder\Bild_03.png"  
"$PSScriptRoot\Bilder\loader.gif"  
"$PSScriptRoot\Bilder\ok.png"  
"$PSScriptRoot\Bilder\nok.png"  
)

$Name =
@(
"Visual C++ 2010 x64 Redistributable"  
"Visual C++ 2015 x64 Redistributable"  
)

$Process=
@(
{Start-Process "C:\Software\Microsoft\Visual C++ 2010 x64 Redistributable\VisualC++Redistributable.vbs" -Wait }  
{Start-Process "C:\Software\Microsoft\Visual C++ 2015 x64 Redistributable\VisualC++Redistributable.vbs" -Wait }  
)

$Text_X=
@(
345
345
)

$Text_Y=
@(
80
100
)

$Image_X=
@(
585
585
)

$Image_Y=
@(
80
100
)

# Fenster
$objForm = New-Object System.Windows.Forms.Form
$objForm.StartPosition = "CenterScreen"  
$objForm.Size = New-Object System.Drawing.Size(1000,600)
$objForm.Text = "Windows Setup"  
$objForm.BackColor = "white"  
$objForm.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($Bild)
$objForm.Opacity = 0.9                                                  
$objForm.Topmost = $true
$objForm.ShowInTaskbar=$false
$objForm.MaximizeBox = $false
$objForm.MinimizeBox = $false
$objForm.ControlBox = $true
$objForm.ShowIcon = $true
$objForm.Visible = $false
$objForm.WindowState = "Normal"                                           
$objForm.SizeGripStyle = "Show"                                           

# Linien
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(0,0) 
$objLabel.Size = New-Object System.Drawing.Size(1000,20)
$objLabel.BackColor = "lightblue"  
$objForm.Controls.Add($objLabel)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(0,540) 
$objLabel.Size = New-Object System.Drawing.Size(1000,20)
$objLabel.BackColor = "lightblue"  
$objForm.Controls.Add($objLabel)

# Bilder
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(25,40) 
$objLabel.Size = New-Object System.Drawing.Size(256,256)
$objLabel.BackColor = "white"  
$objLabel.Image = [System.Drawing.Image]::FromFile($Bild[1])
$objForm.Controls.Add($objLabel)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(700,260) 
$objLabel.Size = New-Object System.Drawing.Size(256,256)
$objLabel.BackColor = "white"  
$objLabel.Image = [System.Drawing.Image]::FromFile($Bild[2])
$objForm.Controls.Add($objLabel)

# Textfeld
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(340,40) 
$objLabel.Size = New-Object System.Drawing.Size(500,30)
$objLabel.Font = "Arial Unicode MS, 14"                                     
$objLabel.ForeColor = "black"                                               
$objLabel.Text = "Programme werden installiert..."  
$objLabel.Name = "Programme werden installiert..."  
$objForm.Controls.Add($objLabel)

# Programme starten...

$i=0
Do{
if (-not $Name[$i]) { Exit }
else
{
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size($Text_X[$i],$Text_Y[$i]) 
$objLabel.Size = New-Object System.Drawing.Size(240,20)
$objLabel.BackColor = "white"   
$objLabel.Font = "Arial Unicode MS, 10"  
$objLabel.ForeColor = "black"  
$objLabel.Text = $Name[$i]
$objForm.Controls.Add($objLabel)

$objForm.ShowDialog() | Out-Null

if ($objLabel.Text = $Name[$i])
{
$Name[$i] = Start-Job $Process[$i] |Get-Job

if (Get-Job -state Running)
{
$objImage_Run = New-Object System.Windows.Forms.Label
$objImage_Run.Location = New-Object System.Drawing.Size($Image_X[$i],$Image_Y[$i]) 
$objImage_Run.Size = New-Object System.Drawing.Size(16,16)
$objImage_Run.Image = [System.Drawing.Image]::FromFile($Bild[3])
$objForm.Controls.Add($objImage_Run)

$objLabel.Font = New-Object System.Drawing.Font($objLabel.Font,[System.Drawing.FontStyle]::Bold)
$objForm.Controls.Add($objLabel)
}
$objForm.ShowDialog() | Out-Null

Wait-Job -Job $Name[$i]
Receive-Job -Job $Name[$i]

if (Get-Job -state Completed)
{
$objImage_Ok = New-Object System.Windows.Forms.Label
$objImage_Ok.Location = New-Object System.Drawing.Size($Image_X[$i],$Image_Y[$i]) 
$objImage_Ok.Size = New-Object System.Drawing.Size(16,16)
$objImage_Ok.Image = [System.Drawing.Image]::FromFile($Bild[4])
$objForm.Controls.Remove($objImage_Run)
$objForm.Controls.Add($objImage_Ok)

$objLabel.Font = New-Object System.Drawing.Font($objLabel.Font,[System.Drawing.FontStyle]::Regular)
$objForm.Controls.Add($objLabel)
}
else
{
$objImage_nOk = New-Object System.Windows.Forms.Label
$objImage_nOk.Location = New-Object System.Drawing.Size($Image_X[$i],$Image_Y[$i]) 
$objImage_nOk.Size = New-Object System.Drawing.Size(16,16)
$objImage_nOk.Image = [System.Drawing.Image]::FromFile($Bild[5])
$objForm.Controls.Remove($objImage_Run)
$objForm.Controls.Add($objImage_nOk)

$objLabel.Font = New-Object System.Drawing.Font($objLabel.Font,[System.Drawing.FontStyle]::Regular)
$objForm.Controls.Add($objLabel)
}}}

$objForm.ShowDialog() | Out-Null

Remove-Job -Job $Name[$i]

$i++
}
while ($i -lt $Name.Length) &$Process.Length; &$Text_X.Length; &$Text_Y.Length; &$Image_X.Length; &$Image_Y.Length

Exit

Ich habe mein Script auch mal mit einer Hash-Tabelle ausprobiert.
Leider kann bei der Hash-Tabelle mittels Wait-Job, Receive-Job und Remove-Job der Job nicht abgefragt werden.

[array]$Prg =@()
$Prg+=($Programm=""| Select Name,Process,Text_X,Text_Y,Image_X,Image_Y); $Programm.Name="Visual C++ 2010 x64 Redistributable"; $Programm.Process={Start-Process "C:\Software\Microsoft\Visual C++ 2010 x64 Redistributable\VisualC++Redistributable.vbs" -Wait };$Programm.Text_X=345;$Programm.Text_Y=80;$Programm.Image_X=585;$Programm.Image_Y=82;$Programm  
$Prg+=($Programm=""| Select Name,Process,Text_X,Text_Y,Image_X,Image_Y); $Programm.Name="Visual C++ 2015 x64 Redistributable"; $Programm.Process={Start-Process "C:\Software\Microsoft\Visual C++ 2015 x64 Redistributable\VisualC++Redistributable.vbs" -Wait };$Programm.Text_X=345;$Programm.Text_Y=100;$Programm.Image_X=585;$Programm.Image_Y=102;$Programm  

Hierzu finde ich leider auch bei Google nichts um dies zu verstehen.
Gibt es für Neulinge eine Erklärung zu diesem Thema?

Vielen Dank im Voraus für Eure Unterstützung.

Schöne Grüße

Markus2016

Content-Key: 313204

Url: https://administrator.de/contentid/313204

Printed on: April 23, 2024 at 16:04 o'clock

Mitglied: 129813
129813 Aug 22, 2016 updated at 14:05:11 (UTC)
Goto Top
Subscribe the completed event of a job (Register-ObjectEvent) and close your form from it.

https://learn-powershell.net/2013/02/08/powershell-and-events-object-eve ...

Regards
Member: Markus2016
Markus2016 Aug 23, 2016 at 20:33:04 (UTC)
Goto Top
Hello highload,

thank you for your support.
I subscribe the object event of the job and close the form from it.
But unfortunately it does not work.
Please, can you support me again.

Thanks a lot.

Regards

Markus2016


konsole

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")   
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  

$Bild =
@(
"$PSScriptRoot\Bilder\Bild_01.ico"  
"$PSScriptRoot\Bilder\Bild_02.png"  
"$PSScriptRoot\Bilder\Bild_03.png"  
"$PSScriptRoot\Bilder\loader.gif"  
"$PSScriptRoot\Bilder\ok.png"  
"$PSScriptRoot\Bilder\nok.png"  
)

$Name =
@(
"Visual C++ 2010 x64 Redistributable"  
"Visual C++ 2015 x64 Redistributable"  
)

$Process=
@(
{Start-Process "C:\Software\Microsoft\Visual C++ 2010 x64 Redistributable\VisualC++Redistributable.vbs" -Wait }  
{Start-Process "C:\Software\Microsoft\Visual C++ 2015 x64 Redistributable\VisualC++Redistributable.vbs" -Wait }  
)

$Text_X=
@(
345
345
)

$Text_Y=
@(
80
100
)

$Image_X=
@(
585
585
)

$Image_Y=
@(
82
102
)

# Fenster
$objForm = New-Object System.Windows.Forms.Form
$objForm.StartPosition = "CenterScreen"  
$objForm.Size = New-Object System.Drawing.Size(1000,600)
$objForm.Text = "Windows Setup"  
$objForm.BackColor = "white"  
$objForm.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($Bild)
$objForm.Opacity = 0.9                                                   
$objForm.Topmost = $true
$objForm.ShowInTaskbar=$false
$objForm.MaximizeBox = $false
$objForm.MinimizeBox = $false
$objForm.ControlBox = $true
$objForm.ShowIcon = $true
$objForm.Visible = $false
$objForm.WindowState = "Normal"                                            
$objForm.SizeGripStyle = "Show"                                            

# Linien
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(0,0) 
$objLabel.Size = New-Object System.Drawing.Size(1000,20)
$objLabel.BackColor = "lightblue"  
$objForm.Controls.Add($objLabel)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(0,540) 
$objLabel.Size = New-Object System.Drawing.Size(1000,20)
$objLabel.BackColor = "lightblue"  
$objForm.Controls.Add($objLabel)

# Bilder
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(25,40) 
$objLabel.Size = New-Object System.Drawing.Size(256,256)
$objLabel.BackColor = "white"  
$objLabel.Image = [System.Drawing.Image]::FromFile($Bild[1])
$objForm.Controls.Add($objLabel)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(700,260) 
$objLabel.Size = New-Object System.Drawing.Size(256,256)
$objLabel.BackColor = "white"  
$objLabel.Image = [System.Drawing.Image]::FromFile($Bild[2])
$objForm.Controls.Add($objLabel)

# Textfeld
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(340,40) 
$objLabel.Size = New-Object System.Drawing.Size(500,30)
$objLabel.Font = "Arial Unicode MS, 14"                                    
$objLabel.ForeColor = "black"                                               
$objLabel.Text = "Programme werden installiert..."  
$objLabel.Name = "Programme werden installiert..."  
$objForm.Controls.Add($objLabel)

# Programme starten...

$i=0
Do{
if (-not $Name[$i]) { Exit }
else
{
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size($Text_X[$i],$Text_Y[$i]) 
$objLabel.Size = New-Object System.Drawing.Size(240,20)
$objLabel.BackColor = "white"   
$objLabel.Font = "Arial Unicode MS, 10"  
$objLabel.ForeColor = "black"  
$objLabel.Text = $Name[$i]
$objForm.Controls.Add($objLabel)

$objForm.ShowDialog() | Out-Null

if ($objLabel.Text = $Name[$i])
{
$Name[$i] = Start-Job $Process[$i] 
Get-Job
Register-ObjectEvent -InputObject $Name[$i] -EventName StateChanged -SourceIdentifier JobEnd -Action {if (Get-Job -State Completed){$objForm.Close($objForm)}}

$Name[$i] | Get-Member -MemberType Event

if (Get-Job -State Running)
{
$objImage_Run = New-Object System.Windows.Forms.Label
$objImage_Run.Location = New-Object System.Drawing.Size($Image_X[$i],$Image_Y[$i]) 
$objImage_Run.Size = New-Object System.Drawing.Size(16,16)
$objImage_Run.Image = [System.Drawing.Image]::FromFile($Bild[3])
$objForm.Controls.Add($objImage_Run)

$objLabel.Font = New-Object System.Drawing.Font($objLabel.Font,[System.Drawing.FontStyle]::Bold)

$objForm.Controls.Add($objLabel)
}

$objForm.ShowDialog()

Wait-Job -Job $Name[$i]
Receive-Job -Job $Name[$i]

if (Get-Job -State Completed)
{
$objImage_Ok = New-Object System.Windows.Forms.Label
$objImage_Ok.Location = New-Object System.Drawing.Size($Image_X[$i],$Image_Y[$i]) 
$objImage_Ok.Size = New-Object System.Drawing.Size(16,16)
$objImage_Ok.Image = [System.Drawing.Image]::FromFile($Bild[4])
$objForm.Controls.Remove($objImage_Run)
$objForm.Controls.Add($objImage_Ok)

$objLabel.Font = New-Object System.Drawing.Font($objLabel.Font,[System.Drawing.FontStyle]::Regular)
$objForm.Controls.Add($objLabel)
}
else
{
$objImage_nOk = New-Object System.Windows.Forms.Label
$objImage_nOk.Location = New-Object System.Drawing.Size($Image_X[$i],$Image_Y[$i]) 
$objImage_nOk.Size = New-Object System.Drawing.Size(16,16)
$objImage_nOk.Image = [System.Drawing.Image]::FromFile($Bild[5])
$objForm.Controls.Remove($objImage_Run)
$objForm.Controls.Add($objImage_nOk)

$objLabel.Font = New-Object System.Drawing.Font($objLabel.Font,[System.Drawing.FontStyle]::Regular)
$objForm.Controls.Add($objLabel)
}}}

$objForm.ShowDialog() | Out-Null

Unregister-Event -SourceIdentifier JobEnd
Remove-Job -Name JobEnd
Remove-Job -Job $Name[$i]

$i++
}
while ($i -lt $Name.Length) &$Process.Length; &$Text_X.Length; &$Text_Y.Length; &$Image_X.Length; &$Image_Y.Length

Exit
Mitglied: 129813
Solution 129813 Aug 24, 2016 at 11:20:44 (UTC)
Goto Top
Simple Example
function GenerateForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  
#endregion

$global:form1 = New-Object System.Windows.Forms.Form
$pb = New-Object System.Windows.Forms.ProgressBar
$btnStart = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 500

$global:job = $null

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------

$timer.add_Tick({
    if ($global:job.JobStateInfo.State -eq "Completed"){  
        $form1.Close()
        $timer.Stop()
    }
})
$handler_btnStart_Click= 
{
    $pb.Visible = $true
    $timer.Start()
    $global:job = Start-Job -ScriptBlock {
        # simply wait 3 seconds
        sleep 3
    }
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$global:form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 98
$System_Drawing_Size.Width = 367
$global:form1.ClientSize = $System_Drawing_Size
$global:form1.DataBindings.DefaultDataSourceUpdateMode = 0
$global:form1.MaximizeBox = $False
$global:form1.MinimizeBox = $False
$global:form1.Name = "form1"  
$global:form1.Text = "Test close from job"  

$pb.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 54
$pb.Location = $System_Drawing_Point
$pb.Name = "pb"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 300
$pb.Size = $System_Drawing_Size
$pb.TabIndex = 1
$pb.Style = 2
$pb.Visible = $false

$global:form1.Controls.Add($pb)


$btnStart.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$btnStart.Location = $System_Drawing_Point
$btnStart.Name = "btnStart"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnStart.Size = $System_Drawing_Size
$btnStart.TabIndex = 0
$btnStart.Text = "Start job"  
$btnStart.UseVisualStyleBackColor = $True
$btnStart.add_Click($handler_btnStart_Click)

$global:form1.Controls.Add($btnStart)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $global:form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$global:form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$global:form1.ShowDialog()| Out-Null
$timer.Stop()
$timer.Dispose()
} #End Function
GenerateForm
Member: Markus2016
Markus2016 Aug 27, 2016 at 16:53:23 (UTC)
Goto Top
Hello highload,

thank you for your example.

I solved it with Runspace.
Example Runspace
It's works fine.

Thank you for your support.

Regards

Markus2016