pixel0815
Goto Top

GUI in Powershell für diesen Code

Hallo liebes Forum,

ist es möglich für diesen Code eine einfache Gui zu entwerfen?
Es sollen Edit Felder ein die man befüllen kann, einzig für drivername sollen die Treiber aus dem System ausgelesen werden.

function CreatePrinter {
$server = $args
$print = ([WMICLASS]“\\.\ROOT\cimv2:Win32_Printer”).createInstance()
$print.drivername = $args[1]
$print.PortName = $args[2]
$print.Shared = $true
$print.Published = $true
$print.Sharename = $args[3]
$print.Location = $args[4]
$print.Comment = $args[5]
$print.DeviceID = $args[6]
$print.Put()
}

function CreatePrinterPort {
$server = $args
$port = ([WMICLASS]“\\.\ROOT\cimv2:Win32_TCPIPPrinterPort”).createInstance()
$port.Name= $args[1]
$port.SNMPEnabled=$false
$port.Protocol=1
$port.HostAddress= $args[2]
$port.Put()
}

$printers = Import-Csv “Drucker.csv”

foreach ($printer in $printers) {
CreatePrinterPort $printer.Printserver $printer.Portname $printer.IPAddress
CreatePrinter $printer.Printserver $printer.Driver $printer.Portname $printer.Sharename $printer.Location $printer.Comment $printer.Printername
}

Hat da jemand was parat?

Gruß
Heiko

Content-Key: 234871

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

Printed on: April 27, 2024 at 01:04 o'clock

Member: colinardo
colinardo Apr 08, 2014 updated at 11:59:31 (UTC)
Goto Top
Hallo Heiko,
Zitat von @pixel0815:
Hat da jemand was parat?
parat nicht, aber schnell was zusammen gezimmert. Das ganze sähe dann so aus:

2330e637a760aa7d843affb2665f668e

(Deinen Code kannst du ja selber im Button-Click-Event hinterlegen und an die Felder koppeln, im Moment werden nur die Werte der Felder in einer MessagBox angezeigt - ein bißchen Arbeit habe ich dir also noch gelassen face-wink)

Zur Info: Einen kostenlosen GUI-Builder bekommst du hier: Primal Forms Community Edition
#Generated Form Function
function GenerateForm {

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

$formMain = New-Object System.Windows.Forms.Form
$btnCreate = New-Object System.Windows.Forms.Button
$cbShared = New-Object System.Windows.Forms.CheckBox
$cbPublished = New-Object System.Windows.Forms.CheckBox
$label6 = New-Object System.Windows.Forms.Label
$comboDriver = New-Object System.Windows.Forms.ComboBox
$txtDeviceID = New-Object System.Windows.Forms.TextBox
$label5 = New-Object System.Windows.Forms.Label
$txtComment = New-Object System.Windows.Forms.TextBox
$label4 = New-Object System.Windows.Forms.Label
$label3 = New-Object System.Windows.Forms.Label
$txtLocation = New-Object System.Windows.Forms.TextBox
$label2 = New-Object System.Windows.Forms.Label
$lblPortname = New-Object System.Windows.Forms.Label
$txtShareName = New-Object System.Windows.Forms.TextBox
$txtPortName = New-Object System.Windows.Forms.TextBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState


#----------------------------------------------
# Event Script Blocks
#----------------------------------------------
$handler_btnCreate_Click= 
{
    #TODO: hier dein Code wenn auf den Button geklickt wird
    $value = "Drivername: $($comboDriver.SelectedItem)`r`nPortname: $($txtPortName.Text)`r`nSharename: $($txtShareName.Text)`r`nLocation: $($txtLocation.Text)`r`nComment: $($txtComment.Text)`r`nDevice ID: $($txtDeviceID.Text)`r`nPublished: $($cbPublished.Checked)`r`nShared: $($cbShared.Checked)"  
    [System.Windows.Forms.MessageBox]::Show($value)
}

$handler_formMain_Load= 
{
    $drivers = gwmi Win32_Printerdriver | select Name
    $comboDriver.Items.AddRange($drivers.Name)
}

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

#----------------------------------------------
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 256
$System_Drawing_Size.Width = 265
$formMain.ClientSize = $System_Drawing_Size
$formMain.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 284
$System_Drawing_Size.Width = 273
$formMain.MinimumSize = $System_Drawing_Size
$formMain.Name = "formMain"  
$formMain.Text = "Create Printer"  
$formMain.add_Load($handler_formMain_Load)

$btnCreate.Anchor = 14

$btnCreate.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 22
$System_Drawing_Point.Y = 209
$btnCreate.Location = $System_Drawing_Point
$btnCreate.Name = "btnCreate"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 35
$System_Drawing_Size.Width = 221
$btnCreate.Size = $System_Drawing_Size
$btnCreate.TabIndex = 16
$btnCreate.Text = "Create Printer"  
$btnCreate.UseVisualStyleBackColor = $True
$btnCreate.add_Click($handler_btnCreate_Click)

$formMain.Controls.Add($btnCreate)


$cbShared.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 179
$System_Drawing_Point.Y = 179
$cbShared.Location = $System_Drawing_Point
$cbShared.Name = "cbShared"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 64
$cbShared.Size = $System_Drawing_Size
$cbShared.TabIndex = 15
$cbShared.Text = "shared"  
$cbShared.UseVisualStyleBackColor = $True

$formMain.Controls.Add($cbShared)


$cbPublished.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 100
$System_Drawing_Point.Y = 179
$cbPublished.Location = $System_Drawing_Point
$cbPublished.Name = "cbPublished"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 95
$cbPublished.Size = $System_Drawing_Size
$cbPublished.TabIndex = 14
$cbPublished.Text = "published"  
$cbPublished.UseVisualStyleBackColor = $True

$formMain.Controls.Add($cbPublished)

$label6.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 22
$System_Drawing_Point.Y = 25
$label6.Location = $System_Drawing_Point
$label6.Name = "label6"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 16
$System_Drawing_Size.Width = 72
$label6.Size = $System_Drawing_Size
$label6.TabIndex = 11
$label6.Text = "Drivername:"  

$formMain.Controls.Add($label6)

$comboDriver.Anchor = 13
$comboDriver.DataBindings.DefaultDataSourceUpdateMode = 0
$comboDriver.DropDownStyle = 2
$comboDriver.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 100
$System_Drawing_Point.Y = 22
$comboDriver.Location = $System_Drawing_Point
$comboDriver.Name = "comboDriver"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 143
$comboDriver.Size = $System_Drawing_Size
$comboDriver.TabIndex = 10

$formMain.Controls.Add($comboDriver)

$txtDeviceID.Anchor = 13
$txtDeviceID.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 100
$System_Drawing_Point.Y = 153
$txtDeviceID.Location = $System_Drawing_Point
$txtDeviceID.Name = "txtDeviceID"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 143
$txtDeviceID.Size = $System_Drawing_Size
$txtDeviceID.TabIndex = 9

$formMain.Controls.Add($txtDeviceID)

$label5.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 22
$System_Drawing_Point.Y = 156
$label5.Location = $System_Drawing_Point
$label5.Name = "label5"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 68
$label5.Size = $System_Drawing_Size
$label5.TabIndex = 8
$label5.Text = "Device ID:"  

$formMain.Controls.Add($label5)

$txtComment.Anchor = 13
$txtComment.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 100
$System_Drawing_Point.Y = 127
$txtComment.Location = $System_Drawing_Point
$txtComment.Name = "txtComment"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 143
$txtComment.Size = $System_Drawing_Size
$txtComment.TabIndex = 7

$formMain.Controls.Add($txtComment)

$label4.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 22
$System_Drawing_Point.Y = 130
$label4.Location = $System_Drawing_Point
$label4.Name = "label4"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 17
$System_Drawing_Size.Width = 62
$label4.Size = $System_Drawing_Size
$label4.TabIndex = 6
$label4.Text = "Comment:"  

$formMain.Controls.Add($label4)

$label3.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 22
$System_Drawing_Point.Y = 104
$label3.Location = $System_Drawing_Point
$label3.Name = "label3"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 67
$label3.Size = $System_Drawing_Size
$label3.TabIndex = 5
$label3.Text = "Location:"  

$formMain.Controls.Add($label3)

$txtLocation.Anchor = 13
$txtLocation.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 100
$System_Drawing_Point.Y = 101
$txtLocation.Location = $System_Drawing_Point
$txtLocation.Name = "txtLocation"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 143
$txtLocation.Size = $System_Drawing_Size
$txtLocation.TabIndex = 4

$formMain.Controls.Add($txtLocation)

$label2.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 22
$System_Drawing_Point.Y = 78
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 72
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 3
$label2.Text = "Sharename:"  

$formMain.Controls.Add($label2)

$lblPortname.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 22
$System_Drawing_Point.Y = 52
$lblPortname.Location = $System_Drawing_Point
$lblPortname.Name = "lblPortname"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 17
$System_Drawing_Size.Width = 72
$lblPortname.Size = $System_Drawing_Size
$lblPortname.TabIndex = 2
$lblPortname.Text = "Portname:"  

$formMain.Controls.Add($lblPortname)

$txtShareName.Anchor = 13
$txtShareName.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 100
$System_Drawing_Point.Y = 75
$txtShareName.Location = $System_Drawing_Point
$txtShareName.Name = "txtShareName"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 143
$txtShareName.Size = $System_Drawing_Size
$txtShareName.TabIndex = 1

$formMain.Controls.Add($txtShareName)

$txtPortName.Anchor = 13
$txtPortName.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 100
$System_Drawing_Point.Y = 49
$txtPortName.Location = $System_Drawing_Point
$txtPortName.Name = "txtPortName"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 143
$txtPortName.Size = $System_Drawing_Size
$txtPortName.TabIndex = 0

$formMain.Controls.Add($txtPortName)

#Save the initial state of the form
$InitialFormWindowState = $formMain.WindowState
#Init the OnLoad event to correct the initial state of the form
$formMain.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$formMain.ShowDialog()| Out-Null

} #End Function

GenerateForm
Grüße Uwe
Member: mayho33
mayho33 Apr 14, 2014 at 14:31:13 (UTC)
Goto Top
Hi!

Von SAPIEN gibt's die ComunityEdition (CE) des PrimalForms. Damit zeichnest du deine Form ala VisualStudio und legst die Ereignisse fest, danach exportierst du dies in eine PS1 und hast eine fertige lauffähige GUI.

Anschließend baust du deine Funktionalitäten ein, fertig!

Link: http://www.sapien.com/software/communitytools

Du musst dich registrieren, damit du das laden kannst, ist aber sehr empfehlenswert.

lg

mayho
Member: colinardo
colinardo Apr 17, 2014 updated at 10:57:39 (UTC)
Goto Top
Wenns das dann war, den Beitrag bitte noch auf gelöst setzen, Merci.

Grüße Uwe