Support Forums

String Value form NumericUpDown Powershell

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.
User avatar
downtoo2015
Posts: 4
Joined: Tue Feb 16, 2016 7:41 am

String Value form NumericUpDown Powershell

Postby downtoo2015 » Wed Feb 17, 2016 4:06 am

Hi everyone,

I'm creating a Powershell script about IIS. My purpose is to get a NumericUpDown form to select a port, then it's creating an app pool and a website with the port selected previously. It's working but it returns "OK" instead the value of the port, and I don't know to get the value selected. So the website creation fail. My problem is that I'm new in the Powershell world... but I like it. So I would be glad to get some help please.

Here is my script for now:

#Port selection
function call-form_tcp
{
[void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
 
[System.Windows.Forms.Application]::EnableVisualStyles()
$Window = New-Object 'System.Windows.Forms.Form'
$Port_TCP = New-Object 'System.Windows.Forms.NumericUpDown'
$LabelDescr = New-Object 'System.Windows.Forms.Label'
$buttonOK = New-Object 'System.Windows.Forms.Button'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
 
$Form_StateCorrection_Load=
{
$Window.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed=
{
try
{
$Window.remove_Load($Form_StateCorrection_Load)
$Window.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
 
$Window.SuspendLayout()
$Port_TCP.BeginInit()
 
$Window.Controls.Add($Port_TCP)
$Window.Controls.Add($LabelDescr)
$Window.Controls.Add($buttonOK)
$Window.AcceptButton = $buttonOK
$Window.ClientSize = '457, 184'
$Window.ControlBox = $False
$Window.FormBorderStyle = 'FixedDialog'
$Window.MaximizeBox = $False
$Window.MinimizeBox = $False
$Window.Name = 'Window'
$Window.ShowIcon = $False
$Window.StartPosition = 'CenterScreen'
$Window.Text = 'Port TCP'
$Window.TopMost = $True
 
$Port_TCP.Increment = 8000
$Port_TCP.Location = '179, 75'
$Port_TCP.Maximum = 65535
$Port_TCP.Minimum = 80
$Port_TCP.Name = 'Port_TCP'
$Port_TCP.Size = '101, 20'
$Port_TCP.TabIndex = 2
$Port_TCP.Value = 80
 
$LabelDescr.Location = '98, 26'
$LabelDescr.Name = 'LabelDescr'
$LabelDescr.Size = '287, 23'
$LabelDescr.TabIndex = 1
$LabelDescr.Text = 'Choose a port number for the website:'
 
$buttonOK.Anchor = 'Bottom, Right'
$buttonOK.DialogResult = 'OK'
$buttonOK.Location = '370, 149'
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = '75, 23'
$buttonOK.TabIndex = 0
$buttonOK.Text = '&OK'
$buttonOK.UseVisualStyleBackColor = $True
$Port_TCP.EndInit()
$Window.ResumeLayout()
 
$InitialFormWindowState = $Window.WindowState
$Window.add_Load($Form_StateCorrection_Load)
$Window.add_FormClosed($Form_Cleanup_FormClosed)
return $Window.showdialog()
 
if((call-form_tcp) –eq 'OK')
{
Return $script:NumericUpDown
}
 
}
$PortSelect = call-form_tcp
$PortSelected = ":"+$PortSelect+":"
 
 
#AppPool & IIS site configuration
Import-Module WebAdministration
$WebSiteName = "MyWebsite"
$iisAppPoolName = $WebSiteName
$iisAppPoolDotNetVersion = "v2.0"
$iisAppName = $WebSiteName
$WebSiteFolder = 'C:\inetpub\wwwroot\'+$WebSiteName
$directoryPath = $WebSiteFolder
cd IIS:\AppPools\
if (!(Test-Path $iisAppPoolName -pathType container))
{
$appPool = New-Item $iisAppPoolName
$appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
$appPool | Set-ItemProperty -Name "enable32BitAppOnWin64" -value true
}
cd IIS:\Sites\
if (Test-Path $iisAppName -pathType container)
{
return
}
$iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=$PortSelected} -physicalPath $directoryPath -ApplicationPool $WebSiteName
Set-ItemProperty IIS:\AppPools\$WebSiteName processmodel.identityType -Value 0
Pause
User avatar
jvierra
Posts: 10017
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Value form NumericUpDown Powershell

Postby jvierra » Wed Feb 17, 2016 4:33 am

Look at what you have here:

return $Window.showdialog()

if ((call-form_tcp) –eq 'OK') {
Return $script:NumericUpDown
}

You are returning the result of ShowDialog.
User avatar
jvierra
Posts: 10017
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Value form NumericUpDown Powershell

Postby jvierra » Wed Feb 17, 2016 4:33 am

Look at what you have here:

return $Window.showdialog()

if ((call-form_tcp) –eq 'OK') {
Return $script:NumericUpDown
}

You are returning the result of ShowDialog.
User avatar
downtoo2015
Posts: 4
Joined: Tue Feb 16, 2016 7:41 am

Re: String Value form NumericUpDown Powershell

Postby downtoo2015 » Thu Feb 18, 2016 3:14 am

Thanks for the help.
I correct those lines by:

if (($Window.showdialog()) –eq 'OK')
{
Return $Port_TCP.Value
}

And it works!
Now, I have to improve it by checking if the port is already used. I'll be back If I need some help.
Thanks again.
User avatar
downtoo2015
Posts: 4
Joined: Tue Feb 16, 2016 7:41 am

Re: String Value form NumericUpDown Powershell

Postby downtoo2015 » Thu Feb 18, 2016 7:42 am

I don't know if I should start another topic, but it's the same script.
OK, so I added a process to check if the port is already used.
I give the choice to stop the other website and continue the creation of the website OR the possibility to choose an other port.
It's OK for the first part, but for the second, I don't know how to go back to the function to select an other port.

If someone could help me... The part of the script where it's supposed to go back to the function is called # Go to function call-form_tcp.

#Port selection
 
function call-form_tcp
{
[void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
 
[System.Windows.Forms.Application]::EnableVisualStyles()
$Window = New-Object 'System.Windows.Forms.Form'
$Port_TCP = New-Object 'System.Windows.Forms.NumericUpDown'
$LabelDescr = New-Object 'System.Windows.Forms.Label'
$buttonOK = New-Object 'System.Windows.Forms.Button'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
 
$Form_StateCorrection_Load=
{
$Window.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed=
{
try
{
$Window.remove_Load($Form_StateCorrection_Load)
$Window.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
 
$Window.SuspendLayout()
$Port_TCP.BeginInit()
 
$Window.Controls.Add($Port_TCP)
$Window.Controls.Add($LabelDescr)
$Window.Controls.Add($buttonOK)
$Window.AcceptButton = $buttonOK
$Window.ClientSize = '457, 184'
$Window.ControlBox = $False
$Window.FormBorderStyle = 'FixedDialog'
$Window.MaximizeBox = $False
$Window.MinimizeBox = $False
$Window.Name = 'Window'
$Window.ShowIcon = $False
$Window.StartPosition = 'CenterScreen'
$Window.Text = 'Port TCP'
$Window.TopMost = $True
 
$Port_TCP.Increment = 8000
$Port_TCP.Location = '179, 75'
$Port_TCP.Maximum = 65535
$Port_TCP.Minimum = 80
$Port_TCP.Name = 'Port_TCP'
$Port_TCP.Size = '101, 20'
$Port_TCP.TabIndex = 2
$Port_TCP.Value = 80
 
$LabelDescr.Location = '98, 26'
$LabelDescr.Name = 'LabelDescr'
$LabelDescr.Size = '287, 23'
$LabelDescr.TabIndex = 1
$LabelDescr.Text = 'Choose a port number for the website:'
 
$buttonOK.Anchor = 'Bottom, Right'
$buttonOK.DialogResult = 'OK'
$buttonOK.Location = '370, 149'
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = '75, 23'
$buttonOK.TabIndex = 0
$buttonOK.Text = '&OK'
$buttonOK.UseVisualStyleBackColor = $True
$Port_TCP.EndInit()
$Window.ResumeLayout()
 
$InitialFormWindowState = $Window.WindowState
$Window.add_Load($Form_StateCorrection_Load)
$Window.add_FormClosed($Form_Cleanup_FormClosed)
 
if (($Window.showdialog()) –eq 'OK') {
Return $Port_TCP.Value
}
}
$PortSelect = call-form_tcp
 
# Port Checking
 
Import-Module WebAdministration
$Websites = Get-ChildItem IIS:\Sites | where { $_.State -eq 'Started' }
foreach ($Site in $Websites)
{ $Binding = $Site.bindings
[string]$BindingInfo = $Binding.Collection
[string]$IP = $BindingInfo.SubString($BindingInfo.IndexOf(" "),$BindingInfo.IndexOf(":")-$BindingInfo.IndexOf(" "))
[string]$Port = $BindingInfo.SubString($BindingInfo.IndexOf(":")+1,$BindingInfo.LastIndexOf(":")-$BindingInfo.IndexOf(":")-1)
if ($Port -eq "$PortSelect")
{ $Websitenameup = $Site.name
$PortAction = new-object -comobject wscript.shell
$intAnswer = $PortAction.popup("The Website `"$Websitenameup`" already use the port $Port. `rDo you want to stop this Website? If not, select an other port.", `
0,"Port already used",4)
If ($intAnswer -eq 6)
{
Stop-Website $Websitenameup
}
else
{
 
# Go to function call-form_tcp
 
}
}
}
 
#AppPool & IIS site configuration
 
$PortSelected = ":"+$PortSelect+":"
$WebSiteName = "MyWebsite"
$iisAppPoolName = $WebSiteName
$iisAppPoolDotNetVersion = "v2.0"
$iisAppName = $WebSiteName
$WebSiteFolder = 'C:\inetpub\wwwroot\'+$WebSiteName
$directoryPath = $WebSiteFolder
cd IIS:\AppPools\
if (!(Test-Path $iisAppPoolName -pathType container))
{
$appPool = New-Item $iisAppPoolName
$appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
$appPool | Set-ItemProperty -Name "enable32BitAppOnWin64" -value true
}
cd IIS:\Sites\
if (!(Test-Path $iisAppName -pathType container))
{
return
}
$iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=$PortSelected} -physicalPath $directoryPath -ApplicationPool $WebSiteName -force
Set-ItemProperty IIS:\AppPools\$WebSiteName processmodel.identityType -Value 0
Write-Host "Job done!"
Pause
User avatar
jvierra
Posts: 10017
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Value form NumericUpDown Powershell

Postby jvierra » Thu Feb 18, 2016 12:20 pm

It would be better if you attached the PSF file. Posting large blocks of code does nto work well for copying.

It would also be better if you posted to a new topic since it is a new question. This is more helpful to others looking for assistance by topic.
User avatar
jvierra
Posts: 10017
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Value form NumericUpDown Powershell

Postby jvierra » Thu Feb 18, 2016 4:46 pm

Here is an example of how to use validation to get you data. This is not a complete solution but it does show how to get the form to do the work of selecting the port.
Attachments
Demo-ValidatingForm.ps1
(3.11 KiB) Downloaded 85 times
User avatar
downtoo2015
Posts: 4
Joined: Tue Feb 16, 2016 7:41 am

Re: String Value form NumericUpDown Powershell

Postby downtoo2015 » Fri Feb 19, 2016 12:57 am

Thanks a lot, I will look at that.

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: Mitzen and 2 guests