I am trying to create a GUI that can create new AD Users.
My Problem: When I press the Button "Speichern" it does not use the Data in the textboxs to create the AD User. Instead this text comes in the command console:
New ADUser cmdlet at command pipeline location 1
Specify values for the following parameters:
Surname:
If I enter the Surname it will create the User with this Surname.
This is my Powershell script:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Import-Module ActiveDirectory
#GUI Oberfläche
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(500,500)
$Form.Text = "Benutzer hinzufügen"
#-------Labels-----------
#Label Benutzer hinzufügen
$labeladduser = New-Object System.Windows.Forms.Label
$labeladduser.Location = New-Object System.Drawing.Size(200,10)
$labeladduser.Size = New-Object System.Drawing.Size(200,30)
$labeladduser.Text = "Benutzer hinzufügen"
$labeladduser.Name = "Benutzer hinzufügen"
$Form.Controls.Add($labeladduser)
#Label Vorname
$Labelvorname = New-Object System.Windows.Forms.Label
$Labelvorname.Location = New-Object System.Drawing.Size(10,50)
$Labelvorname.Size = New-Object System.Drawing.Size(100,20)
$Labelvorname.Text = "Vorname"
$Labelvorname.Name = "Vorname"
$Form.Controls.Add($Labelvorname)
#Label Nachname
$Labelnachname = New-Object System.Windows.Forms.Label
$Labelnachname.Location = New-Object System.Drawing.Size(10,80)
$Labelnachname.Size = New-Object System.Drawing.Size(100,20)
$Labelnachname.Text = "Nachname"
$Labelnachname.Name = "Nachname"
$Form.Controls.Add($Labelnachname)
#Label Vollständigername
$LabelVn = New-Object System.Windows.Forms.Label
$LabelVn.Location = New-Object System.Drawing.Size(10,110)
$LabelVn.Size = New-Object System.Drawing.Size(100,20)
$LabelVn.Text = "Vollständigername"
$LabelVn.Name = "Vollständigername"
$Form.Controls.Add($LabelVn)
#Label Benutzeranmeldename
$LabelBa = New-Object System.Windows.Forms.Label
$LabelBa.Location = New-Object System.Drawing.Size(10,140)
$LabelBa.Size = New-Object System.Drawing.Size(150,20)
$LabelBa.Text = "Benutzeranmeldename"
$LabelBa.Name = "Benutzeranmeldename"
$Form.Controls.Add($LabelBa)
#-------Buttons-------
#Close GUI Button
$BTcancel = New-Object System.Windows.Forms.Button
$BTcancel.Location = New-Object System.Drawing.Size(400,180)
$BTcancel.Size = New-Object System.Drawing.Size(75,23)
$BTcancel.Text = "Cancel"
$BTcancel.Name = "Cancel"
$BTcancel.Add_Click({$Form.Close()})
$Form.Controls.Add($BTcancel)
#Speichern Button
$BTsave = New-Object System.Windows.Forms.Button
$BTsave.Location = New-Object System.Drawing.Size(320,180)
$BTsave.Size = New-Object System.Drawing.Size(75,23)
$BTsave.Text = "Speichern"
$BTsave.Name = "Speichern"
$Form.Controls.Add($BTsave)
#------Textfields------
#Textfeld Vorname
$Textboxvorname = New-Object System.Windows.Forms.TextBox
$Textboxvorname.Location = New-Object System.Drawing.Size(200,50)
$Textboxvorname.Size = New-Object System.Drawing.Size(200,20)
$addv = $Textboxvorname.Text
$Form.Controls.Add($Textboxvorname)
#Textfeld Nachname
$Textboxnachname = New-Object System.Windows.Forms.TextBox
$Textboxnachname.Location = New-Object System.Drawing.Size(200,80)
$Textboxnachname.Size = New-Object System.Drawing.Size(200,20)
$addn = $Textboxnachname.Text
$Form.Controls.Add($Textboxnachname)
#Textfeld Vollständigername
$TextboxVa = New-Object System.Windows.Forms.TextBox
$TextboxVa.Location = New-Object System.Drawing.Size(200,110)
$TextboxVa.Size = New-Object System.Drawing.Size(200,20)
$addVa = $TextboxVa.Text
$Form.Controls.Add($TextboxVa)
#Textfeld Benutzeranmeldename
$TextboxBa= New-Object System.Windows.Forms.TextBox
$TextboxBa.Location = New-Object System.Drawing.Size(200,140)
$TextboxBa.Size = New-Object System.Drawing.Size(200,20)
$addBa = $TextboxBa.Text
$Form.Controls.Add($TextboxBa)
#------Funktionen--------
#Add User
$BTsave.Add_Click({
New-ADuser
-Name $addVa
-Path "Ou=Users, DC=domain, DC=com"
-Givenname = $addv
-Surname =$addn
-SamAccountName $addBa
-AccountPassword (ConvertT0-SecureString "<redacted>" -AsPlainText -Force)
})
[void] $Form.ShowDialog()
Does someone have a solution for my problem?
I believe the problem is when you are assigning values.
taking this as an example:
#Textfeld Benutzeranmeldename
$TextboxBa= New-Object System.Windows.Forms.TextBox
$TextboxBa.Location = New-Object System.Drawing.Size(200,140)
$TextboxBa.Size = New-Object System.Drawing.Size(200,20)
$addBa = $TextboxBa.Text
$Form.Controls.Add($TextboxBa)
when you're assigning a value to $addBa the value of $TextboxBa.Text is most likely empty!
So you should probably change your $BTsave.Add_Click event to directly access the textbox value(s) rather than the empty variable.
Also worth noting is that PowerShell doesn't take kindly to line breaks. If you want to avoid long lines you have to add a back-tick character.
#Add User
$BTsave.Add_Click({
New-ADuser `
-Name $TextboxVa.Text `
-Path "Ou=Users, DC=domain, DC=com" `
-Givenname $Textboxvorname.Text `
-Surname $Textboxnachname.Text `
-SamAccountName $TextboxBa.Text `
-AccountPassword (ConvertTo-SecureString "<redacted>" -AsPlainText -Force)
})
Below has a full form.
Create User and add to groups.
NewUserForm PowershellGUI
https://github.com/jcvnstdn/jcvnstdn/blob/main/UserCreationForm.PS1
Related
We have all of our Autopilot deployed devices in 1 OU and the techs have to move them to their own site's OU I have written a GUI to do this. they enter the device name and the location name. The location name is tha final ou the device will reside in. My GUI gets the OUs for the site and lists them in a Out-Gridview you click on the ou you want and click ok. it sends that to a textbox. then you click move. thats where I ger the error that the device cannot be found. I am sure I have some silly syntax wrong. Thanks in advance.
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);'
[Console.Window]::ShowWindow([Console.Window]::GetConsoleWindow(), 0)
<#
.NAME
AP Device Move
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,400)
$Form.text = "Form"
$Form.TopMost = $false
$LBL_APDEVICE = New-Object system.Windows.Forms.Label
$LBL_APDEVICE.text = "Computer Name"
$LBL_APDEVICE.AutoSize = $true
$LBL_APDEVICE.width = 25
$LBL_APDEVICE.height = 10
$LBL_APDEVICE.location = New-Object System.Drawing.Point(0,2)
$LBL_APDEVICE.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TBX_APDEVICE = New-Object system.Windows.Forms.TextBox
$TBX_APDEVICE.Text = ""
$TBX_APDEVICE.multiline = $false
$TBX_APDEVICE.width = 100
$TBX_APDEVICE.height = 20
$TBX_APDEVICE.location = New-Object System.Drawing.Point(6,37)
$TBX_APDEVICE.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$LBL_SITE = New-Object system.Windows.Forms.Label
$LBL_SITE.text = "Site Name"
$LBL_SITE.AutoSize = $true
$LBL_SITE.width = 25
$LBL_SITE.height = 10
$LBL_SITE.location = New-Object System.Drawing.Point(4,71)
$LBL_SITE.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$LOCATION = New-Object system.Windows.Forms.TextBox
$LOCATION.multiline = $false
$LOCATION.width = 100
$LOCATION.height = 20
$LOCATION.location = New-Object System.Drawing.Point(3,101)
$LOCATION.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$DESTINATION_OU = New-Object system.Windows.Forms.TextBox
$DESTINATION_OU.text = ""
$DESTINATION_OU.multiline = $false
$DESTINATION_OU.width = 100
$DESTINATION_OU.height = 20
$DESTINATION_OU.location = New-Object System.Drawing.Point(14,194)
$DESTINATION_OU.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TARGET_OU = New-Object system.Windows.Forms.Label
$TARGET_OU.text = "Target OU"
$TARGET_OU.AutoSize = $true
$TARGET_OU.width = 25
$TARGET_OU.height = 10
$TARGET_OU.location = New-Object System.Drawing.Point(12,139)
$TARGET_OU.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Get_OU = New-Object system.Windows.Forms.Button
$Get_OU.text = "Get OUs for Site"
$Get_OU.width = 104
$Get_OU.height = 30
$Get_OU.location = New-Object System.Drawing.Point(133,54)
$Get_OU.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$BTN_MOVE = New-Object system.Windows.Forms.Button
$BTN_MOVE.text = "Move Device"
$BTN_MOVE.width = 91
$BTN_MOVE.height = 30
$BTN_MOVE.location = New-Object System.Drawing.Point(34,246)
$BTN_MOVE.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Form.controls.AddRange(#($LBL_APDEVICE,$TBX_APDEVICE,$LBL_SITE,$LOCATION,$DESTINATION_OU,$TARGET_OU,$Get_OU,$BTN_MOVE))
$Get_OU.Add_Click({ GetSiteOUs })
$BTN_MOVE.Add_Click({ doit })
#region Logic
function GetSiteOUs {
$DESTINATION_OU.Text = Get-ADOrganizationalUnit -Filter "Name -Like '*$($LOCATION.Text.Trim())*'" |
Select-Object -ExpandProperty 'Distinguishedname' |
Out-GridView -PassThru -Title "Select the OU"
}
function doit{
$DEVICE = $TBX_APDEVICE.TEXT
$DestOU = "OU=$DESTINATION_OU.text,OU=Computers,OU=World,OU=Disney,OU=Goofy,OU=Duck,OU=Donald,DC=Mickey,DC=Mouse,"
Move-ADObject –Identity "CN=$Device,OU=Autopilot,OU=Lucy,OU=linus,OU=Brown,OU=charlie,DC=Mickey,DC=Mouse," -TargetPath $DestOU
}
#endregion
[void]$Form.ShowDialog()
The answer was to adjust the doit function
function doit{
$DEVICE = $TBX_APDEVICE.TEXT
$DestOU = $DESTINATION_OU.text
Move-ADObject –Identity "CN=$Device,OU=Autopilot,OU=Lucy,OU=linus,OU=Brown,OU=charlie,DC=Mickey,DC=Mouse," -TargetPath $DestOU-TargetPath $DestOU
}
i have built a little powershell gui for creating local computer accounts. i have a problem with my code creating accounts, where i am not asked for changing the password after login. maybe someone can help. i want a further checkbock i can mark, where i am not been asked for changing my password after windows login
$ErrorActionPreference = "Stop"
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# restart elevated if needed
if(!(new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole(544)){
start powershell -Verb runas -ArgumentList '-File',$MyInvocation.MyCommand.Definition
exit
}
#####################################################################################################################################################
#create form
$form = New-Object System.Windows.Forms.Form
$form.Width = 500
$form.Height = 700
$form.MaximizeBox = $false
$form.TopMost = $true
#####################################################################################################################################################
$objLabel = New-Object System.Windows.Forms.label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(130,15)
$objLabel.BackColor = "Transparent"
$objLabel.ForeColor = "Black"
$objLabel.Text = "Username"
$Form.Controls.Add($objLabel)
#textbox with choosen user name
$txtBox = New-Object System.Windows.Forms.TextBox
$txtBox.Location = New-Object System.Drawing.Point (180, 20)
$txtBox.Size = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox)
#####################################################################################################################################################
$objLabel2 = New-Object System.Windows.Forms.label
$objLabel2.Location = New-Object System.Drawing.Size(10,50)
$objLabel2.Size = New-Object System.Drawing.Size(130,15)
$objLabel2.BackColor = "Transparent"
$objLabel2.ForeColor = "Black"
$objLabel2.Text = "Password"
$Form.Controls.Add($objLabel2)
#textbox with choosen password
$txtBox2 = New-Object Windows.Forms.MaskedTextBox
$txtBox2.PasswordChar = '*'
$txtBox2.Location = New-Object System.Drawing.Point (180, 50)
$txtBox2.Size = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox2)
#####################################################################################################################################################
#create checkbox1
$checkBox = New-Object System.Windows.Forms.CheckBox
$checkBox.Location = New-Object System.Drawing.Point (10, 100)
$checkBox.Size = New-Object System.Drawing.Size(350,30)
$checkBox.Text = "PasswordNeverExpires"
$form.Controls.Add($checkBox)
#create checkbox2
$checkBox2 = New-Object System.Windows.Forms.CheckBox
$checkBox2.Location = New-Object System.Drawing.Point (10, 150)
$checkBox2.Size = New-Object System.Drawing.Size(350,30)
$checkBox2.Text = "UserMayChangePassword"
$form.Controls.Add($checkBox2)
#create checkbox3
$checkBox3 = New-Object System.Windows.Forms.CheckBox
$checkBox3.Location = New-Object System.Drawing.Point (10, 200)
$checkBox3.Size = New-Object System.Drawing.Size(350,30)
$checkBox3.Text = "AccountNeverExpires"
$form.Controls.Add($checkBox3)
#create checkbox4
$checkBox4 = New-Object System.Windows.Forms.CheckBox
$checkBox4.Location = New-Object System.Drawing.Point (10, 250)
$checkBox4.Size = New-Object System.Drawing.Size(350,30)
$checkBox4.Text = "AdminAccount"
$form.Controls.Add($checkBox4)
#create checkbox5
$checkBox5 = New-Object System.Windows.Forms.CheckBox
$checkBox5.Location = New-Object System.Drawing.Point (10, 300)
$checkBox5.Size = New-Object System.Drawing.Size(350,30)
$checkBox5.Text = "noPassword"
$checkbox5.Add_Click({
# disable/enable other controls depending on state of current checkbox
$checkBox.Enabled = !$checkBox5.Checked
$txtBox2.Enabled = !$checkBox5.Checked
$checkbox4.Enabled = !$checkBox5.Checked
})
$form.Controls.Add($checkBox5)
#create checkbox6
$checkBox6 = New-Object System.Windows.Forms.CheckBox
$checkBox6.Location = New-Object System.Drawing.Point (10, 350)
$checkBox6.Size = New-Object System.Drawing.Size(350,30)
$checkBox6.Text = "ChangePasswordAtLogon"
$form.Controls.Add($checkBox6)
#create user button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(10,450)
$Button.Size = New-Object System.Drawing.Size(150,50)
$Button.Text = "create user"
$Button.Add_Click({
# Admin or Users Group
$group = #{$true='S-1-5-32-544';$false='S-1-5-32-545'}[$checkbox4.checked]
try{
# define options to create user
$useroptions = #{
Name = $txtbox.Text
Description = $txtbox.Text
Fullname = $txtbox.Text
AccountNeverExpires = $checkbox3.Checked
UserMayNotChangePassword = !$checkbox2.Checked
ChangePasswordAtLogon = $checkbox6.Checked
}
# if the "noPassword" checkbox is not checked
if (!$checkbox5.Checked){
$useroptions.Password = ConvertTo-SecureString $txtbox2.Text -AsPlainText -Force
$useroptions.PasswordNeverExpires = $checkbox.Checked
}else{
# "noPassword" checkbox is checked
$useroptions.NoPassword = $true
$group = 'S-1-5-32-545'
}
# create user and assign to administrators group
New-LocalUser #useroptions | Add-LocalGroupMember -Group (Get-Localgroup | ? Sid -eq $group)
[System.Windows.Forms.MessageBox]::Show("User has been created successfully.","User created",0,64)
}catch{
[System.Windows.Forms.MessageBox]::Show("Error creating new user account:`n $($_.Exception.Message)","Exception",0,48)
}
})
$form.Controls.Add($Button)
#end
[void]$form.ShowDialog()
Hi i make some changes from middle to end part of your code, i think you need this:
$group = #{$true='Administrators';$false='Users'}[$checkbox4.checked]
try{
# define options to create user
$useroptions = #{
Name = $txtbox.Text
Description = $txtbox.Text
Fullname = $txtbox.Text
AccountNeverExpires = $checkbox3.Checked
UserMayNotChangePassword = !$checkbox2.Checked
#ChangePasswordAtLogon = $checkbox6.Checked
}
# if the "noPassword" checkbox is not checked
if (!$checkbox5.Checked){
$useroptions.Password = ConvertTo-SecureString $txtbox2.Text -AsPlainText -Force
$useroptions.PasswordNeverExpires = $checkbox.Checked
}else{
# "noPassword" checkbox is checked
$useroptions.NoPassword = $true
}
# create user and assign to administrators group
New-LocalUser #useroptions | Set-LocalUser -PasswordNeverExpires $checkbox.Checked
Add-LocalGroupMember -Group $group -Member $useroptions.Name
[System.Windows.Forms.MessageBox]::Show("User has been created successfully.","User created",0,64)
}catch{
[System.Windows.Forms.MessageBox]::Show("Error creating new user account:`n $($_.Exception.Message)","Exception",0,48)
}
})
$form.Controls.Add($Button)
this will add new user in Adminsitrators or in Users group (depends if admin is flagged) and if "PasswordNeverExpires" is flagged, will not ask for prompt new password on the first login.
this is not recognized as parameter for New-LocalUser, so i comment it, you decide what to do.
#ChangePasswordAtLogon = $checkbox6.Checked
CODE TESTED AND WORKING
hope this help you
Active Directory looks at the pwdLastSet attribute to see if the account needs to change a password or not. Open AD Users and Computer and look at a perfectly good user account for the "User must change password at next login" box on the Accounts tab. Check the box, and this attribute will be cleared. Uncheck the box again, and it is set to the current timestamp, regardless of what was there originally.
I haven't done this in PowerShell, but I have similar C# code with a UserPrincipal object that uses userPrincipalInstance.LastPasswordSet.HasValue to see is this box would be checked or not, and set (or clear) the userPrincipalInstance.LastPasswordSet to change it's status.
Of course, this is for Active Directory's UserPrincipal, but it's possible WindowsPrincipal for local accounts is similar.
Trying to create a simple GUI Script using powershell to ping a TP ip address and display it with a simple Green/Red Image depending whether its on/off.
But for some reason, when hitting the Close button, I cant seem to remove the green/red dots.
The File contains:
Arial,172.0.0.0
Bodoni,172.0.0.0
Caslon,172.0.0.1
Script:
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.text = "Hardware Checks"
$Form.Size = New-Object System.Drawing.Size(600,600)
$arrayTPs = Get-Content -path "C:\Activity monitor\TPs.txt"
#Image Locations
$filered = (get-item 'C:\Activity monitor\red1.png')
$filegreen = (get-item 'C:\Activity monitor\green1.png')
$imgred = [System.Drawing.Image]::Fromfile($filered)
$imggreen = [System.Drawing.Image]::Fromfile($filegreen)
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,200)
$outputBox.Size = New-Object System.Drawing.Size(565,200)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(450,30)
$Button.Size = New-Object System.Drawing.Size(75,75)
$Button.Text = "Check"
$Button.Add_Click({
$Form.controls.Remove($outputBox)
$Form.controls.refresh()
foreach($room in $arrayTPs){
#Get Name of room from array
$roomAlone = $room -split ","
$roomAlone[0]
$pictureBoxRed = new-object Windows.Forms.PictureBox
$pictureBoxGreen = new-object Windows.Forms.PictureBox
#Add room name to textbox
if($roomAlone[1] -eq "172.0.0.0"){
$pictureBoxGreen.Width = 10
$pictureBoxGreen.Height = 10
$pictureBoxGreen.Image = $imggreen
$pictureBoxGreen.Location = New-Object System.Drawing.Point(90,$counterpic)
$outputBox.controls.add($pictureBoxGreen)
$Form.Controls.Add($outputBox)}
else{
$pictureBoxRed.Width = 10
$pictureBoxRed.Height = 10
$pictureBoxRed.Image = $imgred
$pictureBoxRed.Location = New-Object System.Drawing.Point(90,$counterpic)
$outputBox.controls.add($pictureBoxRed)
$Form.Controls.Add($outputBox)
}
$counterpic = $Counterpic + 20
$counter = $Counter + 20
}})
$Form.Controls.Add($Button)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(450,520)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Close"
$cancelButton.Add_Click({
$outputBox.controls.Remove($pictureBoxRed)
$outputBox.controls.Remove($pictureBoxGreen)
$outputBox.Controls.Equals($null)
$outputBox.controls.update()
$Form.Refresh()
foreach($room in $arrayTPs){
#Get Name of room from array
$roomAlone = $room -split ","
$roomAlone[0]
$pictureBoxRed = new-object Windows.Forms.PictureBox
$pictureBoxGreen = new-object Windows.Forms.PictureBox
}})
$Form.Controls.Add($cancelButton)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
I have some ps script which is runs OK via PowerShell ISE env.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(600,400)
$objForm.StartPosition = "CenterScreen"
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(50,50)
$objTextBox.Size = New-Object System.Drawing.Size(150,20)
$findLabel = New-Object System.Windows.Forms.Label
$findLabel.Location = New-Object System.Drawing.Size(50,100)
$findLabel.Size = New-Object System.Drawing.Size(300,20)
$findLabel.Text = "Nađi:"
$findBox = New-Object System.Windows.Forms.TextBox
$findBox.Location = New-Object System.Drawing.Size(50,120)
$findBox.Size = New-Object System.Drawing.Size(260,20)
$replaceLabel = New-Object System.Windows.Forms.Label
$replaceLabel.Location = New-Object System.Drawing.Size(50,180)
$replaceLabel.Size = New-Object System.Drawing.Size(300,20)
$replaceLabel.Text = "Zameni sa:"
$replaceBox = New-Object System.Windows.Forms.TextBox
$replaceBox.Location = New-Object System.Drawing.Size(50,200)
$replaceBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($startButton)
$objForm.Controls.Add($findBox)
$objForm.Controls.Add($replaceBox)
$objForm.Controls.Add($objTextBox)
$objForm.Controls.Add($beginScriptButton)
$objForm.Controls.Add($findLabel)
$objForm.Controls.Add($replaceLabel)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$startButton = New-Object System.Windows.Forms.Button
$startButton.Location = New-Object System.Drawing.Size(220,50)
$startButton.Size = New-Object System.Drawing.Size(75,23)
$startButton.Text = "Browse!"
$startButton.Add_Click({
$browser = New-Object System.Windows.Forms.FolderBrowserDialog
$browser.Location = New-Object System.Drawing.Size(60,60)
$null = $browser.ShowDialog()
$path = $browser.SelectedPath
$objTextBox.Text = $path
})
$beginScriptButton = New-Object System.Windows.Forms.Button
$beginScriptButton.Location = New-Object System.Drawing.Size(350,130)
$beginScriptButton.Size = New-Object System.Drawing.Size(350,180)
$beginScriptButton.Text = "Begin"
$beginScriptButton.Add_Click({
$a = $objTextBox.Text
if(($a) -and ($findBox.Text) -and ($replaceBox.Text)){
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$list = Get-ChildItem "c:\users\stefan\test\*.*" -Include *.doc*
foreach($item in $list){
$objDoc = $objWord.Documents.Open($item.FullName,$true)
$objSelection = $objWord.Selection
$wdFindContinue = 1
$FindText = $findBox.Text
$MatchCase = $false
$MatchWholeWord = $true
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $wdFindContinue
$Format = $False
$wdReplaceNone = 0
$ReplaceWith = $replaceBox.Text
$wdFindContinue = 1
$ReplaceAll = 2
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith,$ReplaceAll)
$objDoc.Save()
$objDoc.Close()
}
$objWord.Quit()
[System.Windows.Forms.MessageBox]::Show("Uspešno ste izvršili izmenu!")
}
else{
[System.Windows.Forms.MessageBox]::Show("Please fill in all fields.")
}
})
Then, I compile it via PowerGUI as .exe
And I run it, first, it opens a cmd and after a second it disappear, I can't even see my powershell form that I've created through the code.
I've tried different options through PowerGui (I want to not be displayed a cmd window, just my form). Do you know some way how to compile it to .exe and to see only form and logic behind it?
Thanks!
Compiling the script as an exe in Powershell runs it in a new instance, where you'll need to load the form components. Try adding this to the start of your script:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Evening everyone,
I've spent a while having a play on google learning how to create a basic form with powershell. Currently I'm having a problem coming to grips with how to get this basic form to output to a html file at the click of a button I have managed to get it to work with the services ie -
Get-Service | ConvertTo-Html | Out-File "Location"
However i'm wanting to input data on my form and then once the submit button is pressed, it puts the data into a table and then outputs it to the html file.
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "BasicForm"
$Form.Width = 800
$Form.AutoScroll = $True
#VRN label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,75)
$objLabel.Text = "Num:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#VRN Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(100,75)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Title by Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(250,75)
$objLabel.Text = "Title:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Title by Dropdownbox
$objtitlelist = New-Object System.Windows.Forms.ComboBox
$objtitlelist.Location = New-Object System.Drawing.Size(350,75)
$objtitlelist.Size = New-Object System.Drawing.Size(125,50)
$objtitlelist.Items.Add("Mr")
$objtitlelist.Items.Add("Mrs")
$objtitlelist.Items.Add("Ms")
$Form.Controls.Add($objtitlelist)
#Title by Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(500,75)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Title by Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(250,125)
$objLabel.Text = "Title:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Title by Dropdownbox
$objtitlelist = New-Object System.Windows.Forms.ComboBox
$objtitlelist.Location = New-Object System.Drawing.Size(350,125)
$objtitlelist.Size = New-Object System.Drawing.Size(125,50)
$objtitlelist.Items.Add("Mr")
$objtitlelist.Items.Add("Mrs")
$objtitlelist.Items.Add("Ms")
$Form.Controls.Add($objtitlelist)
#Title by Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(500,125)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Date label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,125)
$objLabel.Text = "Date:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Date Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(100,125)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Add Button docs
$Buttondocs = New-Object System.Windows.Forms.Button
$Buttondocs.Location = New-Object System.Drawing.Size(175,35)
$Buttondocs.Size = New-Object System.Drawing.Size(120,23)
$Buttondocs.Text = "Documentation"
$Form.Controls.Add($Buttondocs)
#Add Button docs event
$Buttondocs.Add_Click({Button_Click})
Function Button_Click()
{
Invoke-Item "C:\Users\Michael\Documents\PowershellProjects\EIMI\*.txt"
}
#Add Button html
$Buttonhtml = New-Object System.Windows.Forms.Button
$Buttonhtml.Location = New-Object System.Drawing.Size(35,35)
$Buttonhtml.Size = New-Object System.Drawing.Size(120,23)
$Buttonhtml.Text = "Submit Form"
$Form.Controls.Add($Buttonhtml)
#Add Button html event
$Buttonhtml.Add_Click({Button_Click})
Function Button_Click()
{
ConvertTo-Html | Out-File "C:\Users\Michael\Documents\PowershellProjects\EIMI\index.html"
}
#Add Question 1
$Question1 = New-Object System.Windows.Forms.Label
$Question1.Location = New-Object System.Drawing.Size(50,175)
$Question1.Size = New-Object System.Drawing.Size(500,50)
$Question1.Text = "Random Form Question."
$Form.Controls.Add($Question1)
$Form.ShowDialog()
The documentation invoke-item section is another problem altogether but I beleive I have found a solution to that one. Unless someone has a simple method. Apologise if this is just trivial nonsense but I'm learning from playing with things. Any suggestions on good authors would be greatly appreciated, or even tips on how to improve this in general.
Thanks Mike.