locked
Needed Button Click Event to the powershell Code RRS feed

  • Question

  • Hi,

    Please help in this to add all the below code into a button event to the WIN Forms.

    # # Script3.ps1 # Add-Type -AssemblyName System.Windows.Forms Import-Module ActiveDirectory param([String]$PFXPath,[String]$PFXPassword,[String]$CertSubject,[switch]$ExcludeLocalServerCert) # Set Powershell Compatibility Mode Set-StrictMode -Version 2.0 $ScriptPath = {Split-Path $MyInvocation.ScriptName} $Form = New-Object system.Windows.Forms.Form $Form.Size = New-Object System.Drawing.Size(370,470) $Form.Text = "Certiface Renewal Tool" $Form.TopMost = $true $Form.BackColor = "Cyan" $FilePath = New-Object system.windows.Forms.Label $FilePath.Text = "File Path:" $FilePath.AutoSize = $true $FilePath.Width = 25 $FilePath.Height = 10 $FilePath.location = New-object system.drawing.point(16,26) $FilePath.Font = "Microsoft Sans Serif,10" $Form.controls.Add($FilePath) $CertName = New-Object system.windows.Forms.Label $CertName.Text = "CName:" $CertName.AutoSize = $true $CertName.Width = 25 $CertName.Height = 10 $CertName.location = New-object system.drawing.point(19,26) $CertName.Font = "Microsoft Sans Serif,10" $Form.controls.Add($CertName) $PFXPath = New-Object system.windows.Forms.Label $PFXPath.Text = "PFX Path:" $PFXPath.AutoSize = $true $PFXPath.Width = 25 $PFXPath.Height = 10 $PFXPath.location = New-object system.drawing.point(21,26) $PFXPath.Font = "Microsoft Sans Serif,10" $Form.controls.Add($PFXPath) $LogFile = New-Object system.windows.Forms.Label $LogFile.Text = "Log File Path:" $LogFile.AutoSize = $true $LogFile.Width = 25 $LogFile.Height = 10 $LogFile.location = New-object system.drawing.point(24,26) $LogFile.Font = "Microsoft Sans Serif,10" $Form.controls.Add($LogFile) $btn_Execute = New-Object system.windows.Forms.Button $btn_Execute.Text = "Renew" $btn_Execute.BackColor = "Gray" $btn_Execute.Width = 249 $btn_Execute.Height = 30 $btn_Execute.Add_MouseClick({ if ([String]::IsNullOrEmpty($PFXPath)) { $PFXPath = "C:\Users\Anand\Desktop\aaa.com.pfx" } if ([String]::IsNullOrEmpty($PFXPassword)) { $PFXPassword = "Welcome" } if ([String]::IsNullOrEmpty($CertSubject)) { $CertSubject = "CN=b2b.microsoft.com" } if (!($ExcludeLocalServerCert.IsPresent)) { $ExcludeLocalServerCert = $True } # Set to the Web Site $sitename = "Default Web Site" # Set to the Port number $port = 443 $invalidChars = [io.path]::GetInvalidFileNamechars() $datestampforfilename = ((Get-Date -format s).ToString() -replace "[$invalidChars]","-") # Get the script path $ScriptPath = {Split-Path $MyInvocation.ScriptName} $ScriptName = [System.IO.Path]::GetFilenameWithoutExtension($MyInvocation.MyCommand.Path.ToString()) $Logfile = "$ScriptName-$($datestampforfilename).txt" $logPath = "$($env:windir)\Temp" $logfile = "C:\Users\Anand\Desktop\Log.txt" # Start the logging Start-Transcript $logFile Write-Output "Logging to $logFile" #------------------------------------------------------------- Write-Output "Start Certificate Installation" Write-Output "Loading the Web Administration Module" try{ Import-Module webadministration } catch{ Write-Output "Failed to load the Web Administration Module" } Write-Output "Deleting existing certificate from Store" try{ $cert = Get-ChildItem cert:\LocalMachine\MY | Where-Object {$_.subject -like "$CertSubject*" -AND $_.Subject -notmatch "CN=$env:COMPUTERNAME"} $thumbprint = $cert.Thumbprint.ToString() If (Test-Path "cert:\localmachine\my\$thumbprint") { Remove-Item -Path cert:\localmachine\my\$thumbprint -DeleteKey } } catch{ Write-Output "Unable to delete existing certificate from store" } Write-Output "Running certutil to import certificate into Store" try{ $ImportError = certutil -f -importpfx -p $PFXPassword $PFXPath #certutil -f -importpfx -p $strPassword $strPFXPath } catch{ Write-Output "certutil failed to import certificate: $ImportError" } Write-Output "Locating the cert in the Store" try{ If ($ExcludeLocalServerCert) { $cert = Get-ChildItem cert:\LocalMachine\MY | Where-Object {$_.subject -like "$CertSubject*" -AND $_.Subject -notmatch "CN=$env:COMPUTERNAME"} } Else { $cert = Get-ChildItem cert:\LocalMachine\My | Where-Object {$_.subject -like "$CertSubject*"} } $thumbprint = $cert.Thumbprint.ToString() Write-Output $cert } catch{ Write-Output "Unable to locate cert in certificate store" } Write-Output "Removing any existing binding from the site and SSLBindings store" try{ # Remove existing binding form site if ($null -ne (Get-WebBinding -Name $sitename | where-object {$_.protocol -eq "https"})) { $RemoveWebBinding = Remove-WebBinding -Name $sitename -Port $Port -Protocol "https" Write-Output $RemoveWebBinding } # Remove existing binding in SSLBindings store If (Test-Path "IIS:\SslBindings\0.0.0.0!$port") { $RemoveSSLBinding = Remove-Item -path "IIS:\SSLBindings\0.0.0.0!$port" Write-Output $RemoveSSLBinding } } catch{ Write-Output "Unable to remove existing binding" } Write-Output "Bind your certificate to IIS HTTPS listener" try{ $NewWebBinding = New-WebBinding -Name $sitename -Port $Port -Protocol "https" Write-Output $NewWebBinding $AddSSLCertToWebBinding = (Get-WebBinding $sitename -Port $Port -Protocol "https").AddSslCertificate($thumbprint, "MY") Write-Output $AddSSLCertToWebBinding #$RemoveWebBinding = Remove-WebBinding Name $sitename -Port $Port -Protocol "https" Remove-WebBinding -Name "NoHostHeader" -BindingInformation "*:443:" Write-Output $RemoveWebBinding } catch{ Write-Output "Unable to bind cert" } Write-Output "Completed Certificate Installation" }) $btn_Execute.location = new-object system.drawing.point(17,335) $btn_Execute.Font = "Microsoft Sans Serif,10" $Form.controls.Add($btn_Execute) [void]$Form.ShowDialog() $Form.Dispose() # Stop logging Stop-Transcript


    Thursday, March 15, 2018 1:49 PM

All replies

  • Are you talking of adding an action when a button is clicked? If that's the case, you can use the following code:

    $Button.Add_Click({<#Insert code that needs to be executed here.#>})
    
    # Alternatives
    # Using a variable
    $var = {<#code goes here#>}
    $Button.Add_Click($var)
    
    # Using a function
    Function foo () {
        #Insert code here
    }
    $Button.Add_Click({foo})

    Thursday, March 15, 2018 1:59 PM
  • $button.Add_Click({
      
        #CODE HERE
    
    )}
    


    Learn PowerShell

    Script Requests

    -Remember to mark the correct response as the answer-

    Thursday, March 15, 2018 2:00 PM