String Value form NumericUpDown Powershell
Posted: Wed Feb 17, 2016 3: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:
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