Page 1 of 1

String Value form NumericUpDown Powershell

Posted: Wed Feb 17, 2016 3:06 am
by downtoo2015
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. function call-form_tcp
  3. {
  4.     [void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  5.     [void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  6.     [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  7.     [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  8.     [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  9.     [void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  10.     [void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  11.     [void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  12.     [void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  13.  
  14.     [System.Windows.Forms.Application]::EnableVisualStyles()
  15.     $Window = New-Object 'System.Windows.Forms.Form'
  16.     $Port_TCP = New-Object 'System.Windows.Forms.NumericUpDown'
  17.     $LabelDescr = New-Object 'System.Windows.Forms.Label'
  18.     $buttonOK = New-Object 'System.Windows.Forms.Button'
  19.     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  20.  
  21.     $Form_StateCorrection_Load=
  22.     {
  23.         $Window.WindowState = $InitialFormWindowState
  24.     }
  25.         $Form_Cleanup_FormClosed=
  26.     {
  27.         try
  28.         {
  29.             $Window.remove_Load($Form_StateCorrection_Load)
  30.             $Window.remove_FormClosed($Form_Cleanup_FormClosed)
  31.         }
  32.         catch [Exception]
  33.         { }
  34.     }
  35.  
  36.     $Window.SuspendLayout()
  37.     $Port_TCP.BeginInit()
  38.  
  39.     $Window.Controls.Add($Port_TCP)
  40.     $Window.Controls.Add($LabelDescr)
  41.     $Window.Controls.Add($buttonOK)
  42.     $Window.AcceptButton = $buttonOK
  43.     $Window.ClientSize = '457, 184'
  44.     $Window.ControlBox = $False
  45.     $Window.FormBorderStyle = 'FixedDialog'
  46.     $Window.MaximizeBox = $False
  47.     $Window.MinimizeBox = $False
  48.     $Window.Name = 'Window'
  49.     $Window.ShowIcon = $False
  50.     $Window.StartPosition = 'CenterScreen'
  51.     $Window.Text = 'Port TCP'
  52.     $Window.TopMost = $True
  53.  
  54.     $Port_TCP.Increment = 8000
  55.     $Port_TCP.Location = '179, 75'
  56.     $Port_TCP.Maximum = 65535
  57.     $Port_TCP.Minimum = 80
  58.     $Port_TCP.Name = 'Port_TCP'
  59.     $Port_TCP.Size = '101, 20'
  60.     $Port_TCP.TabIndex = 2
  61.     $Port_TCP.Value = 80
  62.  
  63.     $LabelDescr.Location = '98, 26'
  64.     $LabelDescr.Name = 'LabelDescr'
  65.     $LabelDescr.Size = '287, 23'
  66.     $LabelDescr.TabIndex = 1
  67.     $LabelDescr.Text = 'Choose a port number for the website:'
  68.  
  69.     $buttonOK.Anchor = 'Bottom, Right'
  70.     $buttonOK.DialogResult = 'OK'
  71.     $buttonOK.Location = '370, 149'
  72.     $buttonOK.Name = 'buttonOK'
  73.     $buttonOK.Size = '75, 23'
  74.     $buttonOK.TabIndex = 0
  75.     $buttonOK.Text = '&OK'
  76.     $buttonOK.UseVisualStyleBackColor = $True
  77.     $Port_TCP.EndInit()
  78.     $Window.ResumeLayout()
  79.  
  80.     $InitialFormWindowState = $Window.WindowState
  81.     $Window.add_Load($Form_StateCorrection_Load)
  82.     $Window.add_FormClosed($Form_Cleanup_FormClosed)
  83.     return $Window.showdialog()
  84.    
  85.     if((call-form_tcp) –eq 'OK')
  86.     {
  87.         Return $script:NumericUpDown
  88.     }
  89.    
  90. }
  91. $PortSelect = call-form_tcp
  92. $PortSelected = ":"+$PortSelect+":"
  93.  
  94.  
  95. #AppPool & IIS site configuration
  96. Import-Module WebAdministration
  97. $WebSiteName = "MyWebsite"
  98. $iisAppPoolName = $WebSiteName
  99. $iisAppPoolDotNetVersion = "v2.0"
  100. $iisAppName = $WebSiteName
  101. $WebSiteFolder = 'C:\inetpub\wwwroot\'+$WebSiteName
  102. $directoryPath = $WebSiteFolder
  103. cd IIS:\AppPools\
  104. if (!(Test-Path $iisAppPoolName -pathType container))
  105. {
  106.     $appPool = New-Item $iisAppPoolName
  107.     $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
  108.     $appPool | Set-ItemProperty -Name "enable32BitAppOnWin64" -value true
  109. }
  110. cd IIS:\Sites\
  111. if (Test-Path $iisAppName -pathType container)
  112. {
  113.     return
  114. }
  115. $iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=$PortSelected} -physicalPath $directoryPath -ApplicationPool $WebSiteName
  116. Set-ItemProperty IIS:\AppPools\$WebSiteName processmodel.identityType -Value 0
  117. Pause

Re: String Value form NumericUpDown Powershell

Posted: Wed Feb 17, 2016 3:33 am
by jvierra
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.

Re: String Value form NumericUpDown Powershell

Posted: Wed Feb 17, 2016 3:33 am
by jvierra
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.

Re: String Value form NumericUpDown Powershell

Posted: Thu Feb 18, 2016 2:14 am
by downtoo2015
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.

Re: String Value form NumericUpDown Powershell

Posted: Thu Feb 18, 2016 6:42 am
by downtoo2015
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. function call-form_tcp
  4. {
  5.     [void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  6.     [void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  7.     [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  8.     [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  9.     [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  10.     [void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  11.     [void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  12.     [void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  13.     [void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  14.  
  15.     [System.Windows.Forms.Application]::EnableVisualStyles()
  16.     $Window = New-Object 'System.Windows.Forms.Form'
  17.     $Port_TCP = New-Object 'System.Windows.Forms.NumericUpDown'
  18.     $LabelDescr = New-Object 'System.Windows.Forms.Label'
  19.     $buttonOK = New-Object 'System.Windows.Forms.Button'
  20.     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  21.  
  22.     $Form_StateCorrection_Load=
  23.     {
  24.         $Window.WindowState = $InitialFormWindowState
  25.     }
  26.         $Form_Cleanup_FormClosed=
  27.     {
  28.         try
  29.         {
  30.             $Window.remove_Load($Form_StateCorrection_Load)
  31.             $Window.remove_FormClosed($Form_Cleanup_FormClosed)
  32.         }
  33.         catch [Exception]
  34.         { }
  35.     }
  36.  
  37.     $Window.SuspendLayout()
  38.     $Port_TCP.BeginInit()
  39.  
  40.     $Window.Controls.Add($Port_TCP)
  41.     $Window.Controls.Add($LabelDescr)
  42.     $Window.Controls.Add($buttonOK)
  43.     $Window.AcceptButton = $buttonOK
  44.     $Window.ClientSize = '457, 184'
  45.     $Window.ControlBox = $False
  46.     $Window.FormBorderStyle = 'FixedDialog'
  47.     $Window.MaximizeBox = $False
  48.     $Window.MinimizeBox = $False
  49.     $Window.Name = 'Window'
  50.     $Window.ShowIcon = $False
  51.     $Window.StartPosition = 'CenterScreen'
  52.     $Window.Text = 'Port TCP'
  53.     $Window.TopMost = $True
  54.  
  55.     $Port_TCP.Increment = 8000
  56.     $Port_TCP.Location = '179, 75'
  57.     $Port_TCP.Maximum = 65535
  58.     $Port_TCP.Minimum = 80
  59.     $Port_TCP.Name = 'Port_TCP'
  60.     $Port_TCP.Size = '101, 20'
  61.     $Port_TCP.TabIndex = 2
  62.     $Port_TCP.Value = 80
  63.  
  64.     $LabelDescr.Location = '98, 26'
  65.     $LabelDescr.Name = 'LabelDescr'
  66.     $LabelDescr.Size = '287, 23'
  67.     $LabelDescr.TabIndex = 1
  68.     $LabelDescr.Text = 'Choose a port number for the website:'
  69.  
  70.     $buttonOK.Anchor = 'Bottom, Right'
  71.     $buttonOK.DialogResult = 'OK'
  72.     $buttonOK.Location = '370, 149'
  73.     $buttonOK.Name = 'buttonOK'
  74.     $buttonOK.Size = '75, 23'
  75.     $buttonOK.TabIndex = 0
  76.     $buttonOK.Text = '&OK'
  77.     $buttonOK.UseVisualStyleBackColor = $True
  78.     $Port_TCP.EndInit()
  79.     $Window.ResumeLayout()
  80.  
  81.     $InitialFormWindowState = $Window.WindowState
  82.     $Window.add_Load($Form_StateCorrection_Load)
  83.     $Window.add_FormClosed($Form_Cleanup_FormClosed)
  84.  
  85.     if (($Window.showdialog()) –eq 'OK') {
  86.     Return $Port_TCP.Value
  87.     }
  88. }
  89. $PortSelect = call-form_tcp
  90.  
  91. # Port Checking
  92.  
  93. Import-Module WebAdministration
  94. $Websites = Get-ChildItem IIS:\Sites | where {  $_.State -eq 'Started'  }
  95. foreach ($Site in $Websites)
  96.     {   $Binding = $Site.bindings
  97.         [string]$BindingInfo = $Binding.Collection
  98.         [string]$IP = $BindingInfo.SubString($BindingInfo.IndexOf(" "),$BindingInfo.IndexOf(":")-$BindingInfo.IndexOf(" "))        
  99.         [string]$Port = $BindingInfo.SubString($BindingInfo.IndexOf(":")+1,$BindingInfo.LastIndexOf(":")-$BindingInfo.IndexOf(":")-1)
  100.             if ($Port -eq "$PortSelect")
  101.             {       $Websitenameup = $Site.name
  102.                     $PortAction = new-object -comobject wscript.shell
  103.                     $intAnswer = $PortAction.popup("The Website `"$Websitenameup`" already use the port $Port. `rDo you want to stop this Website? If not, select an other port.", `
  104.                     0,"Port already used",4)
  105.                         If ($intAnswer -eq 6)
  106.                         {
  107.                         Stop-Website $Websitenameup
  108.                         }
  109.                         else
  110.                         {
  111.                        
  112.                         # Go to function call-form_tcp     
  113.                        
  114.                         }
  115.     }
  116. }
  117.  
  118. #AppPool & IIS site configuration
  119.  
  120. $PortSelected = ":"+$PortSelect+":"
  121. $WebSiteName = "MyWebsite"
  122. $iisAppPoolName = $WebSiteName
  123. $iisAppPoolDotNetVersion = "v2.0"
  124. $iisAppName = $WebSiteName
  125. $WebSiteFolder = 'C:\inetpub\wwwroot\'+$WebSiteName
  126. $directoryPath = $WebSiteFolder
  127. cd IIS:\AppPools\
  128. if (!(Test-Path $iisAppPoolName -pathType container))
  129. {
  130.     $appPool = New-Item $iisAppPoolName
  131.     $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
  132.     $appPool | Set-ItemProperty -Name "enable32BitAppOnWin64" -value true
  133. }
  134. cd IIS:\Sites\
  135. if (!(Test-Path $iisAppName -pathType container))
  136. {
  137.     return
  138. }
  139. $iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=$PortSelected} -physicalPath $directoryPath -ApplicationPool $WebSiteName -force
  140. Set-ItemProperty IIS:\AppPools\$WebSiteName processmodel.identityType -Value 0
  141. Write-Host "Job done!"
  142. Pause

Re: String Value form NumericUpDown Powershell

Posted: Thu Feb 18, 2016 11:20 am
by jvierra
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.

Re: String Value form NumericUpDown Powershell

Posted: Thu Feb 18, 2016 3:46 pm
by jvierra
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.

Re: String Value form NumericUpDown Powershell

Posted: Thu Feb 18, 2016 11:57 pm
by downtoo2015
Thanks a lot, I will look at that.