Created
November 5, 2015 23:47
-
-
Save PrateekKumarSingh/00eb2b78c1852c52cfe2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter(Mandatory=$true,Position=0)] $ImportModuleFrom, | |
[Parameter(Position=1)] $LogPath | |
) | |
Function Create-Form | |
{ | |
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null | |
# Create the Conatainer Form to place the Labels and Textboxes | |
$Form = New-Object “System.Windows.Forms.Form”; | |
$Form.Width = 350; | |
$Form.Height = 200; | |
$Form.Text = 'Enter the required details' | |
$Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen; | |
$Form.TopMost = $true | |
#Define Label1 | |
$Label1 = New-Object “System.Windows.Forms.Label”; | |
$Label1.Left = 10; | |
$Label1.Top = 15; | |
$Label1.Text = 'Server Name'; | |
#Define Label2 | |
$Label2 = New-Object “System.Windows.Forms.Label”; | |
$Label2.Left = 10; | |
$Label2.Top = 50; | |
$Label2.Text = 'Scope ID'; | |
#Define Label3 | |
$Label3 = New-Object “System.Windows.Forms.Label”; | |
$Label3.Left = 10; | |
$Label3.Top = 85; | |
$Label3.Width = 180 | |
$Label3.Text = 'No. of IP Lease to be removed ?'; | |
#Define TextBox1 for input | |
$TextBox1 = New-Object “System.Windows.Forms.TextBox”; | |
$TextBox1.Left = 200; | |
$TextBox1.Top = 10; | |
$TextBox1.width = 100; | |
#Define TextBox2 for input | |
$TextBox2 = New-Object “System.Windows.Forms.TextBox”; | |
$TextBox2.Left = 200; | |
$TextBox2.Top = 50; | |
$TextBox2.width = 100; | |
#Define TextBox3 for input | |
$TextBox3 = New-Object “System.Windows.Forms.TextBox”; | |
$TextBox3.Left = 200; | |
$TextBox3.Top = 85; | |
$TextBox3.width = 100; | |
#Define OK Button | |
$OKbutton = New-Object “System.Windows.Forms.Button”; | |
$OKbutton.Left = 20; | |
$OKbutton.Top = 120; | |
$OKbutton.Width = 100; | |
$OKbutton.Text = “OK”; | |
############# This is when you have to close the Form after getting values | |
$eventHandler = [System.EventHandler]{ | |
$TextBox1.Text; | |
$TextBox2.Text; | |
$TextBox3.Text; | |
$Form.Close();}; | |
$OKbutton.Add_Click($eventHandler) ; | |
#Add controls to the Form | |
$Form.Controls.Add($OKbutton); | |
$Form.Controls.Add($Label1); | |
$Form.Controls.Add($Label2); | |
$Form.Controls.Add($Label3); | |
$Form.Controls.Add($TextBox1); | |
$Form.Controls.Add($TextBox2); | |
$Form.Controls.Add($TextBox3); | |
$Form.ShowDialog()|Out-Null | |
Return $TextBox1.Text,$TextBox2.Text,$TextBox3.Text | |
} | |
Function Remove-DHCPLease($Server, $ScopeID, $Count) | |
{ | |
$Start = Get-Date | |
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null | |
# Create the Conatainer Form to place the Labels and Textboxes | |
$Form = New-Object “System.Windows.Forms.Form”; | |
$Form.Text = 'Collecting Target IP Addresses' | |
$Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen; | |
$Form.AutoSize = $true | |
$Form.TopMost = $true | |
#Define Label1 | |
$Label1 = New-Object “System.Windows.Forms.Label”; | |
$Label1.Left = 20; | |
$Label1.Top = 20; | |
$Label1.AutoSize = $true | |
$Label1.Text = "Searching first $count IP Address(es) that are not responding to PING Requests`n`nScope : $ScopeID `nServer : $($server.ToUpper()) `n`nPlease wait for a while, this could take few minutes" | |
#Define OK Button | |
$OKbutton = New-Object “System.Windows.Forms.Button”; | |
$OKbutton.Left = 20; | |
$OKbutton.Top = $Form.Height - 50 | |
$OKbutton.Width = 100; | |
$OKbutton.Text = “OK”; | |
$OKbutton.Enabled = $false | |
#Define Cancel Button | |
$Cancelbutton = New-Object “System.Windows.Forms.Button”; | |
$Cancelbutton.Left = 250; | |
$Cancelbutton.Top = $Form.Height - 50 | |
$Cancelbutton.Width = 100; | |
$Cancelbutton.Text = “Cancel”; | |
$OKbutton.Add_Click( | |
{ | |
#$Form.Controls.Remove($OKbutton) | |
$OKbutton.Enabled = $false | |
$Label1.text = "Releasing IP Addresses from the scope now ... Please wait." | |
$TargetIPAddr | Remove-DhcpServerv4Lease -ComputerName $server -ErrorVariable +err | |
if($?) | |
{ | |
if($LogPath) | |
{ | |
If($LogPath[-1] -ne '\') | |
{ | |
$LogPath = "$LogPath\DCHP_Release_Logs.txt" | |
} | |
else | |
{ | |
$LogPath = $LogPath+"DCHP_Release_Logs.txt" | |
} | |
} | |
else | |
{ | |
$LogPath = "$((Get-Location).path)\DCHP_Release_Logs.txt" | |
} | |
"`n**[$((Get-Date).datetime)] Following IP Address(es) were released by USER : $env:USERDOMAIN\$env:USERNAME`n $($TargetIPAddr | %{"`n$($_.hostname) - $($_.IPaddress)"})" -replace "`n", "`r`n" | set-Content $LogPath | |
$PercentUsed = Get-DhcpServerv4ScopeStatistics -ScopeId $ScopeID -ComputerName $server | select PercentageInUse -Expand PercentageInuse | |
$Label1.text = "IP Addresses successfully released from the scope and is $(100 - [Int]$PercentUsed)% Free. You can close this window now`n`n`n`nA Log file has been genrated on below path to record this DHCP IP address release(s)`n`n$LogPath" | |
} | |
Else | |
{ | |
$Label1.text = "Something went wrong. Please close this Window and run the script again. `n $err" | |
} | |
} | |
) | |
$CancelButton.add_click( | |
{ | |
$Form.Close() | |
} | |
) | |
#Add controls to the Form | |
$Form.Controls.Add($Label1) | |
$Form.Controls.Add($OKbutton) | |
$Form.COntrols.Add($CancelButton) | |
$form.Visible = $true | |
$TargetIPAddr = Get-DhcpServerv4lease -computer $server -ScopeId $ScopeID |?{$_.addressstate -notlike '*reservation*' -and $(test-Connection $_.ipaddress -count 4 -Quiet) -eq $false} | select -First $Count | |
$Finish = Get-Date | |
$TimeTaken = $Finish - $Start | |
$Form.Visible = $false | |
$OKbutton.Enabled = $true | |
$Form.Text = 'Target IP Addresses' | |
$Label1.Text = "`nTIME TAKEN : $($TimeTaken.Minutes) Mins and $($TimeTaken.Seconds) Secs`n"+ $($TargetIPAddr| %{"`n$($_.hostname) - $($_.IPaddress)"})+"`n`nPlease Confirm if you want to delete these IP from SCOPE : $scopeID on SERVER: $($server.ToUpper()) ?`n`nPress OK to Continue" | |
$Form.ShowDialog() | Out-Null | |
} | |
Import-Module -PSSession (New-PSSession -ComputerName $ImportModuleFrom -Credential (Get-Credential 'Domain\Username')) -Name dhcpserver | |
$Server,$ScopeID,$Count = Create-Form | |
$Start = (Get-Date).ToShortTimeString() | |
Remove-DHCPLease $Server $ScopeID $Count | |
Get-PSSession | Remove-PSSession |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment