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.
Locked
User avatar
downtoo2015
Posts: 4
Joined: Tue Feb 16, 2016 7:41 am

String Value form NumericUpDown Powershell

Post by 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:
  1. #Port selection
  2.  
  3. function call-form_tcp
  4.  
  5. {
  6.  
  7.     [void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  8.  
  9.     [void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  10.  
  11.     [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  12.  
  13.     [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  14.  
  15.     [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  16.  
  17.     [void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  18.  
  19.     [void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  20.  
  21.     [void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  22.  
  23.     [void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  24.  
  25.  
  26.  
  27.     [System.Windows.Forms.Application]::EnableVisualStyles()
  28.  
  29.     $Window = New-Object 'System.Windows.Forms.Form'
  30.  
  31.     $Port_TCP = New-Object 'System.Windows.Forms.NumericUpDown'
  32.  
  33.     $LabelDescr = New-Object 'System.Windows.Forms.Label'
  34.  
  35.     $buttonOK = New-Object 'System.Windows.Forms.Button'
  36.  
  37.     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  38.  
  39.  
  40.  
  41.     $Form_StateCorrection_Load=
  42.  
  43.     {
  44.  
  45.         $Window.WindowState = $InitialFormWindowState
  46.  
  47.     }
  48.  
  49.         $Form_Cleanup_FormClosed=
  50.  
  51.     {
  52.  
  53.         try
  54.  
  55.         {
  56.  
  57.             $Window.remove_Load($Form_StateCorrection_Load)
  58.  
  59.             $Window.remove_FormClosed($Form_Cleanup_FormClosed)
  60.  
  61.         }
  62.  
  63.         catch [Exception]
  64.  
  65.         { }
  66.  
  67.     }
  68.  
  69.  
  70.  
  71.     $Window.SuspendLayout()
  72.  
  73.     $Port_TCP.BeginInit()
  74.  
  75.  
  76.  
  77.     $Window.Controls.Add($Port_TCP)
  78.  
  79.     $Window.Controls.Add($LabelDescr)
  80.  
  81.     $Window.Controls.Add($buttonOK)
  82.  
  83.     $Window.AcceptButton = $buttonOK
  84.  
  85.     $Window.ClientSize = '457, 184'
  86.  
  87.     $Window.ControlBox = $False
  88.  
  89.     $Window.FormBorderStyle = 'FixedDialog'
  90.  
  91.     $Window.MaximizeBox = $False
  92.  
  93.     $Window.MinimizeBox = $False
  94.  
  95.     $Window.Name = 'Window'
  96.  
  97.     $Window.ShowIcon = $False
  98.  
  99.     $Window.StartPosition = 'CenterScreen'
  100.  
  101.     $Window.Text = 'Port TCP'
  102.  
  103.     $Window.TopMost = $True
  104.  
  105.  
  106.  
  107.     $Port_TCP.Increment = 8000
  108.  
  109.     $Port_TCP.Location = '179, 75'
  110.  
  111.     $Port_TCP.Maximum = 65535
  112.  
  113.     $Port_TCP.Minimum = 80
  114.  
  115.     $Port_TCP.Name = 'Port_TCP'
  116.  
  117.     $Port_TCP.Size = '101, 20'
  118.  
  119.     $Port_TCP.TabIndex = 2
  120.  
  121.     $Port_TCP.Value = 80
  122.  
  123.  
  124.  
  125.     $LabelDescr.Location = '98, 26'
  126.  
  127.     $LabelDescr.Name = 'LabelDescr'
  128.  
  129.     $LabelDescr.Size = '287, 23'
  130.  
  131.     $LabelDescr.TabIndex = 1
  132.  
  133.     $LabelDescr.Text = 'Choose a port number for the website:'
  134.  
  135.  
  136.  
  137.     $buttonOK.Anchor = 'Bottom, Right'
  138.  
  139.     $buttonOK.DialogResult = 'OK'
  140.  
  141.     $buttonOK.Location = '370, 149'
  142.  
  143.     $buttonOK.Name = 'buttonOK'
  144.  
  145.     $buttonOK.Size = '75, 23'
  146.  
  147.     $buttonOK.TabIndex = 0
  148.  
  149.     $buttonOK.Text = '&OK'
  150.  
  151.     $buttonOK.UseVisualStyleBackColor = $True
  152.  
  153.     $Port_TCP.EndInit()
  154.  
  155.     $Window.ResumeLayout()
  156.  
  157.  
  158.  
  159.     $InitialFormWindowState = $Window.WindowState
  160.  
  161.     $Window.add_Load($Form_StateCorrection_Load)
  162.  
  163.     $Window.add_FormClosed($Form_Cleanup_FormClosed)
  164.  
  165.     return $Window.showdialog()
  166.  
  167.    
  168.  
  169.     if((call-form_tcp) –eq 'OK')
  170.  
  171.     {
  172.  
  173.         Return $script:NumericUpDown
  174.  
  175.     }
  176.  
  177.    
  178.  
  179. }
  180.  
  181. $PortSelect = call-form_tcp
  182.  
  183. $PortSelected = ":"+$PortSelect+":"
  184.  
  185.  
  186.  
  187.  
  188.  
  189. #AppPool & IIS site configuration
  190.  
  191. Import-Module WebAdministration
  192.  
  193. $WebSiteName = "MyWebsite"
  194.  
  195. $iisAppPoolName = $WebSiteName
  196.  
  197. $iisAppPoolDotNetVersion = "v2.0"
  198.  
  199. $iisAppName = $WebSiteName
  200.  
  201. $WebSiteFolder = 'C:\inetpub\wwwroot\'+$WebSiteName
  202.  
  203. $directoryPath = $WebSiteFolder
  204.  
  205. cd IIS:\AppPools\
  206.  
  207. if (!(Test-Path $iisAppPoolName -pathType container))
  208.  
  209. {
  210.  
  211.     $appPool = New-Item $iisAppPoolName
  212.  
  213.     $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
  214.  
  215.     $appPool | Set-ItemProperty -Name "enable32BitAppOnWin64" -value true
  216.  
  217. }
  218.  
  219. cd IIS:\Sites\
  220.  
  221. if (Test-Path $iisAppName -pathType container)
  222.  
  223. {
  224.  
  225.     return
  226.  
  227. }
  228.  
  229. $iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=$PortSelected} -physicalPath $directoryPath -ApplicationPool $WebSiteName
  230.  
  231. Set-ItemProperty IIS:\AppPools\$WebSiteName processmodel.identityType -Value 0
  232.  
  233. Pause

User avatar
jvierra
Posts: 11550
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Value form NumericUpDown Powershell

Post by 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: 11550
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Value form NumericUpDown Powershell

Post by 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

Post by 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

Post by 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.
  1. #Port selection
  2.  
  3.  
  4.  
  5. function call-form_tcp
  6.  
  7. {
  8.  
  9.     [void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  10.  
  11.     [void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  12.  
  13.     [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  14.  
  15.     [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  16.  
  17.     [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  18.  
  19.     [void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  20.  
  21.     [void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  22.  
  23.     [void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  24.  
  25.     [void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  26.  
  27.  
  28.  
  29.     [System.Windows.Forms.Application]::EnableVisualStyles()
  30.  
  31.     $Window = New-Object 'System.Windows.Forms.Form'
  32.  
  33.     $Port_TCP = New-Object 'System.Windows.Forms.NumericUpDown'
  34.  
  35.     $LabelDescr = New-Object 'System.Windows.Forms.Label'
  36.  
  37.     $buttonOK = New-Object 'System.Windows.Forms.Button'
  38.  
  39.     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  40.  
  41.  
  42.  
  43.     $Form_StateCorrection_Load=
  44.  
  45.     {
  46.  
  47.         $Window.WindowState = $InitialFormWindowState
  48.  
  49.     }
  50.  
  51.         $Form_Cleanup_FormClosed=
  52.  
  53.     {
  54.  
  55.         try
  56.  
  57.         {
  58.  
  59.             $Window.remove_Load($Form_StateCorrection_Load)
  60.  
  61.             $Window.remove_FormClosed($Form_Cleanup_FormClosed)
  62.  
  63.         }
  64.  
  65.         catch [Exception]
  66.  
  67.         { }
  68.  
  69.     }
  70.  
  71.  
  72.  
  73.     $Window.SuspendLayout()
  74.  
  75.     $Port_TCP.BeginInit()
  76.  
  77.  
  78.  
  79.     $Window.Controls.Add($Port_TCP)
  80.  
  81.     $Window.Controls.Add($LabelDescr)
  82.  
  83.     $Window.Controls.Add($buttonOK)
  84.  
  85.     $Window.AcceptButton = $buttonOK
  86.  
  87.     $Window.ClientSize = '457, 184'
  88.  
  89.     $Window.ControlBox = $False
  90.  
  91.     $Window.FormBorderStyle = 'FixedDialog'
  92.  
  93.     $Window.MaximizeBox = $False
  94.  
  95.     $Window.MinimizeBox = $False
  96.  
  97.     $Window.Name = 'Window'
  98.  
  99.     $Window.ShowIcon = $False
  100.  
  101.     $Window.StartPosition = 'CenterScreen'
  102.  
  103.     $Window.Text = 'Port TCP'
  104.  
  105.     $Window.TopMost = $True
  106.  
  107.  
  108.  
  109.     $Port_TCP.Increment = 8000
  110.  
  111.     $Port_TCP.Location = '179, 75'
  112.  
  113.     $Port_TCP.Maximum = 65535
  114.  
  115.     $Port_TCP.Minimum = 80
  116.  
  117.     $Port_TCP.Name = 'Port_TCP'
  118.  
  119.     $Port_TCP.Size = '101, 20'
  120.  
  121.     $Port_TCP.TabIndex = 2
  122.  
  123.     $Port_TCP.Value = 80
  124.  
  125.  
  126.  
  127.     $LabelDescr.Location = '98, 26'
  128.  
  129.     $LabelDescr.Name = 'LabelDescr'
  130.  
  131.     $LabelDescr.Size = '287, 23'
  132.  
  133.     $LabelDescr.TabIndex = 1
  134.  
  135.     $LabelDescr.Text = 'Choose a port number for the website:'
  136.  
  137.  
  138.  
  139.     $buttonOK.Anchor = 'Bottom, Right'
  140.  
  141.     $buttonOK.DialogResult = 'OK'
  142.  
  143.     $buttonOK.Location = '370, 149'
  144.  
  145.     $buttonOK.Name = 'buttonOK'
  146.  
  147.     $buttonOK.Size = '75, 23'
  148.  
  149.     $buttonOK.TabIndex = 0
  150.  
  151.     $buttonOK.Text = '&OK'
  152.  
  153.     $buttonOK.UseVisualStyleBackColor = $True
  154.  
  155.     $Port_TCP.EndInit()
  156.  
  157.     $Window.ResumeLayout()
  158.  
  159.  
  160.  
  161.     $InitialFormWindowState = $Window.WindowState
  162.  
  163.     $Window.add_Load($Form_StateCorrection_Load)
  164.  
  165.     $Window.add_FormClosed($Form_Cleanup_FormClosed)
  166.  
  167.  
  168.  
  169.     if (($Window.showdialog()) –eq 'OK') {
  170.  
  171.     Return $Port_TCP.Value
  172.  
  173.     }
  174.  
  175. }
  176.  
  177. $PortSelect = call-form_tcp
  178.  
  179.  
  180.  
  181. # Port Checking
  182.  
  183.  
  184.  
  185. Import-Module WebAdministration
  186.  
  187. $Websites = Get-ChildItem IIS:\Sites | where {  $_.State -eq 'Started'  }
  188.  
  189. foreach ($Site in $Websites)
  190.  
  191.     {   $Binding = $Site.bindings
  192.  
  193.         [string]$BindingInfo = $Binding.Collection
  194.  
  195.         [string]$IP = $BindingInfo.SubString($BindingInfo.IndexOf(" "),$BindingInfo.IndexOf(":")-$BindingInfo.IndexOf(" "))        
  196.  
  197.         [string]$Port = $BindingInfo.SubString($BindingInfo.IndexOf(":")+1,$BindingInfo.LastIndexOf(":")-$BindingInfo.IndexOf(":")-1)
  198.  
  199.             if ($Port -eq "$PortSelect")
  200.  
  201.             {       $Websitenameup = $Site.name
  202.  
  203.                     $PortAction = new-object -comobject wscript.shell
  204.  
  205.                     $intAnswer = $PortAction.popup("The Website `"$Websitenameup`" already use the port $Port. `rDo you want to stop this Website? If not, select an other port.", `
  206.  
  207.                     0,"Port already used",4)
  208.  
  209.                         If ($intAnswer -eq 6)
  210.  
  211.                         {
  212.  
  213.                         Stop-Website $Websitenameup
  214.  
  215.                         }
  216.  
  217.                         else
  218.  
  219.                         {
  220.  
  221.                        
  222.  
  223.                         # Go to function call-form_tcp     
  224.  
  225.                        
  226.  
  227.                         }
  228.  
  229.     }
  230.  
  231. }
  232.  
  233.  
  234.  
  235. #AppPool & IIS site configuration
  236.  
  237.  
  238.  
  239. $PortSelected = ":"+$PortSelect+":"
  240.  
  241. $WebSiteName = "MyWebsite"
  242.  
  243. $iisAppPoolName = $WebSiteName
  244.  
  245. $iisAppPoolDotNetVersion = "v2.0"
  246.  
  247. $iisAppName = $WebSiteName
  248.  
  249. $WebSiteFolder = 'C:\inetpub\wwwroot\'+$WebSiteName
  250.  
  251. $directoryPath = $WebSiteFolder
  252.  
  253. cd IIS:\AppPools\
  254.  
  255. if (!(Test-Path $iisAppPoolName -pathType container))
  256.  
  257. {
  258.  
  259.     $appPool = New-Item $iisAppPoolName
  260.  
  261.     $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
  262.  
  263.     $appPool | Set-ItemProperty -Name "enable32BitAppOnWin64" -value true
  264.  
  265. }
  266.  
  267. cd IIS:\Sites\
  268.  
  269. if (!(Test-Path $iisAppName -pathType container))
  270.  
  271. {
  272.  
  273.     return
  274.  
  275. }
  276.  
  277. $iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=$PortSelected} -physicalPath $directoryPath -ApplicationPool $WebSiteName -force
  278.  
  279. Set-ItemProperty IIS:\AppPools\$WebSiteName processmodel.identityType -Value 0
  280.  
  281. Write-Host "Job done!"
  282.  
  283. Pause

User avatar
jvierra
Posts: 11550
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Value form NumericUpDown Powershell

Post by 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: 11550
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Value form NumericUpDown Powershell

Post by 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 90 times

User avatar
downtoo2015
Posts: 4
Joined: Tue Feb 16, 2016 7:41 am

Re: String Value form NumericUpDown Powershell

Post by downtoo2015 » Fri Feb 19, 2016 12:57 am

Thanks a lot, I will look at that.

Locked