From 88d9b3f4d7deaa2b2212717691a1baa5dabb8872 Mon Sep 17 00:00:00 2001 From: John Klann Date: Thu, 14 Apr 2016 15:37:36 -0400 Subject: [PATCH 1/5] Update README.md --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index ffbd3d7..27d23a0 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,15 @@ These are Modules I have created over time. If you would like to contribute please feel free to Fork. If you have any issues please reach out. +## Contributing to PowerKeePass +1. Only fork the dev branch. +2. Pull requests to master will not be considered. +3. Include comments on your code. +4. Keep comments up to date. +5. Create issues for any code corrections on the dev branch, fork then pull. +6. Use [Allman Style](https://en.wikipedia.org/wiki/Indent_style#Allman_style) indentation or very similar for code style. +7. Ask questions. + ## Clipboard-1 This Module allows for basic malnipulation of the windows clipboard. From b5a4d24a9b93dc55878586664c9a5789c103c5a7 Mon Sep 17 00:00:00 2001 From: John Klann Date: Sat, 21 May 2016 03:28:37 -0400 Subject: [PATCH 2/5] Adding Base Toast Notification Module --- ToastNotify-1/ToastNotify-1.psm1 | 75 ++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 ToastNotify-1/ToastNotify-1.psm1 diff --git a/ToastNotify-1/ToastNotify-1.psm1 b/ToastNotify-1/ToastNotify-1.psm1 new file mode 100644 index 0000000..ae5a962 --- /dev/null +++ b/ToastNotify-1/ToastNotify-1.psm1 @@ -0,0 +1,75 @@ +function New-ToastNotification +{ + [CmdletBinding()] + param + ( + [Parameter( + Position = 0, + Mandatory = $true + )] + [String] $AppName, + [Parameter( + Position = 1, + Mandatory = $true + )] + [String] $Title, + [Parameter( + Position = 2, + Mandatory = $true + )] + [String] $Body + ) + process + { + $null = [System.Reflection.Assembly]::LoadFile("C:\bin\assembly\NotificationsExtensions.Win10.NETCore.dll") + $null = [Windows.Data.Xml.Dom.XmlDocument,Windows.Web,ContentType=WindowsRunTime] + $null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] + $null = [Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] + $ToastContent = New-Object NotificationsExtensions.Toasts.ToastContent + $ToastContent.Launch = $AppName + $ToastText = New-Object NotificationsExtensions.Toasts.ToastText + $ToastText.Text = $Title + $ToastTextBody = New-Object NotificationsExtensions.Toasts.ToastText + $ToastTextBody.Text = $Body + $AppLogo = New-Object NotificationsExtensions.Toasts.ToastAppLogo + $AppLogo.Crop = [NotificationsExtensions.Toasts.ToastImageCrop]::Circle + $AppLogo.Source = New-Object NotificationsExtensions.Toasts.ToastImageSource("C:\sys\ui\3.png") + $ToastVisual = New-Object NotificationsExtensions.Toasts.ToastVisual + $ToastVisual.AppLogoOverride = $AppLogo + $ToastVisual.TitleText = $ToastText + $ToastVisual.BodyTextLine1 = $ToastTextBody + $ToastContent.Visual = $ToastVisual + $ToastContent.Duration = "long" + $ToastContent.ActivationType = "Background" + $Content = $ToastContent.GetContent() + $xml = New-Object Windows.Data.Xml.Dom.XmlDocument + $xml.LoadXml($Content) + $toast=[Windows.UI.Notifications.ToastNotification]::new($xml) + $toast.ExpirationTime = (Get-Date).AddDays(1000) + $notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppName) + $notifier.Show($toast) + + ##Old## + # $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText04) + # $ToastXml = [xml] $template.GetXml() + # $ToastElements = $ToastXml.GetElementsByTagName("text") + # # $null = for($i=0;$i -lt $ToastElements.Id.Count; $i++){$ToastElements[$i].AppendChild($ToastXml.CreateTextNode("Line $i"));} + # $null=$ToastElements[0].AppendChild($ToastXml.CreateTextNode($NotificationTitle)) + # $null=$ToastElements[1].AppendChild($ToastXml.CreateTextNode($NotificationBody)) + # # $null=$ToastElements[2].AppendChild($ToastXml.CreateTextNode($NotificationBody)) + # [string] $imagePath = "C:\sys\ui\3.png" + # $imageElements = $ToastXml.GetElementsByTagName("image"); + # # $x=$imageElements[0].Attributes.GetNamedItem("src") + # # $x.'#text' = $imagePath + # $xml = New-Object Windows.Data.Xml.Dom.XmlDocument + # $xml.LoadXml($ToastXml.OuterXml) + # $toast=[Windows.UI.Notifications.ToastNotification]::new($xml) + # $toast.Tag = $NotificationTitle + # $toast.Group = $NotificationTitle + # $toast.ExpirationTime = (Get-Date).AddHours(2) + # $notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($NotificationTitle) + # $notifier.Show($toast) + } +} + + From aa96132e29ea5c259264e750259050bec2cedbf0 Mon Sep 17 00:00:00 2001 From: John Klann Date: Sat, 21 May 2016 03:32:00 -0400 Subject: [PATCH 3/5] adding package tracker --- TrackPackage-1/TrackPackage-1.psm1 | 146 +++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 TrackPackage-1/TrackPackage-1.psm1 diff --git a/TrackPackage-1/TrackPackage-1.psm1 b/TrackPackage-1/TrackPackage-1.psm1 new file mode 100644 index 0000000..380aa9b --- /dev/null +++ b/TrackPackage-1/TrackPackage-1.psm1 @@ -0,0 +1,146 @@ +function Get-TrackPackageStatus +{ + [CmdletBinding()] + Param + ( + [Parameter( + Position = 0, + Mandatory = $true + )] + [ValidateNotNullOrEmpty()] + [String] $TrackingNumber, + [Parameter( + Position = 1, + Mandatory = $true + )] + [ValidateSet("UPS","USPS")] + [String] $Carrier + ) + process + { + #Build Carrier URI + switch($Carrier) + { + "UPS" {$TrackingURI = "https://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=$($TrackingNumber)"} + "USPS" {$TrackingURI = "https://tools.usps.com/go/TrackConfirmAction?tLabels=$($TrackingNumber)"} + } + #GetSite Content and Parse + $Site = Invoke-WebRequest -Uri $TrackingURI + if($Carrier -eq "USPS") + { + $HTMLTable = ($Site.AllElements | Where-Object { $_.Class -eq "zebra-table" } | Select-Object -exp innerHtml) + if($HTMLTable -eq $null) + { + Write-Warning -Message "Could Not Find Tracking Number." + exit + } + [Array] $ColumnHeaders = @() + [Array] $RowValues = @() + $str="`n" + $chr= [Convert]::ToChar($str) + foreach($line in $HTMLTable.Split($chr)) + { + if($line -match '") + { + $ColumnHeaders += ([Regex]::Match($line,'(?<=>).*?(?=)').Groups[0].Value -replace " "," " -replace "&" -replace " +"," ") + if ($line -match ".+") + { + $IsBeginRow = $false + } + } + elseif($IsBeginRow -eq $true -and $line -match ")[^<>]+(?=<)').Groups[0].Value -replace " "," " -replace " +"," ") + if ($line -match ".+") + { + if ($RowValues.Length -gt $ColumnHeaders.Length) + { + $diff = $RowValues.Length - $ColumnHeaders.Length + if($diff -eq 1) + { + $RowValues[$RowValues.Length -2] = "$($RowValues[$RowValues.Length -2])$($RowValues[$RowValues.Length -1])" + } + # $Counter = 0 + # $EmptyIndex = @() + # foreach($cell in $RowValues) + # { + # if ($cell -eq "") + # { + # $EmptyIndex += $counter + # } + # $counter++ + # } + # if ($EmptyIndex.Length -le $diff) + # { + # $TempRowValues = @() + # foreach($cell in $RowValues) + # { + # if ($cell -eq "") + # } + # } + } + $IsBeginRow = $false + $row = New-Object -TypeName PSObject + for($i = 0; $i -lt $ColumnHeaders.Length; $i++) + { + $row | Add-Member -MemberType NoteProperty -Name $ColumnHeaders[$i] -Value $RowValues[$i] + } + $RowValues = @() + $row + } + } + } + } + elseif($Carrier -eq "UPS") + { + $HTMLTable = ($site.AllElements | Where-Object { $_.Class -eq "dataTable" } | Select-Object -exp innerHTML) + if($HTMLTable -eq $null) + { + Write-Warning -Message "Could Not Find Tracking Number." + exit + } + [Array] $ColumnHeaders = @() + [Array] $RowValues = @() + $str="`n" + $chr= [Convert]::ToChar($str) + foreach($line in $HTMLTable.Split($chr)) + { + if($line -match ').*?(?=)').Groups[0].Value + if ($line -match ".+") + { + $IsBeginRow = $false + } + } + elseif($IsBeginRow -eq $true -and $line -match ").*?(?=)').Groups[0].Value + if ($line -match ".+") + { + $IsBeginRow = $false + $row = New-Object -TypeName PSObject + for($i = 0; $i -lt $ColumnHeaders.Length; $i++) + { + $row | Add-Member -MemberType NoteProperty -Name $ColumnHeaders[$i] -Value $RowValues[$i] + } + $RowValues = @() + $row + } + } + } + } + } +} + + + + From b93db9c41d59bfc0cd3a70e84f012ab63a6683e3 Mon Sep 17 00:00:00 2001 From: John Klann Date: Sat, 21 May 2016 03:29:48 -0400 Subject: [PATCH 4/5] s --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2c01a26..b7771a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -CopyStructure-1/CopyStruc.ps1 \ No newline at end of file +CopyStructure-1/CopyStruc.ps1 +DnsLookup-1/DnsLookup-1.psm1 +TrackPackage-1/.vscode/launch.json \ No newline at end of file From be02add813c35224c90280eb6207c122a934a099 Mon Sep 17 00:00:00 2001 From: John Klann Date: Tue, 27 Sep 2016 13:48:11 -0400 Subject: [PATCH 5/5] Added Server State Monitor --- .gitignore | 3 +- ServerStateMonitor/ServerStateMonitor.psm1 | 100 ++++++++++++++++ ServerStateMonitor/Test.ps1 | 128 +++++++++++++++++++++ 3 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 ServerStateMonitor/ServerStateMonitor.psm1 create mode 100644 ServerStateMonitor/Test.ps1 diff --git a/.gitignore b/.gitignore index b7771a6..6c74f25 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ CopyStructure-1/CopyStruc.ps1 DnsLookup-1/DnsLookup-1.psm1 -TrackPackage-1/.vscode/launch.json \ No newline at end of file +TrackPackage-1/.vscode/launch.json +ServerStateMonitor/.vscode/launch.json \ No newline at end of file diff --git a/ServerStateMonitor/ServerStateMonitor.psm1 b/ServerStateMonitor/ServerStateMonitor.psm1 new file mode 100644 index 0000000..7d58715 --- /dev/null +++ b/ServerStateMonitor/ServerStateMonitor.psm1 @@ -0,0 +1,100 @@ +function Watch-Server +{ + [CmdletBinding()] + param + ( + [Parameter( + Position = 0, + Mandatory = $true, + ParameterSetName = "Server" + )] + [ValidateNotNullOrEmpty()] + [String] $ComputerName, + + [Parameter( + Position = 0, + Mandatory = $true, + ParameterSetName = "IPV4" + )] + [ValidateNotNullOrEmpty()] + [String] $IPV4, + + [Parameter( + Position = 1, + Mandatory = $true, + ParameterSetName = "IPV4" + )] + [Parameter( + Position = 1, + Mandatory = $true, + ParameterSetName = "Server" + )] + [ValidateSet("StateChange","Reboot","Up","Down")] + [String] $MonitorType, + + [Parameter( + Position = 2, + Mandatory = $false, + ParameterSetName = "IPV4" + )] + [Parameter( + Position = 2, + Mandatory = $false, + ParameterSetName = "Server" + )] + [Int] $TimeOut = 300 + ) + process + { + + if ($PsCmdlet.ParameterSetName -eq 'Server') + { + $IsRealHost = nslookup $ComputerName + if ($IsRealHost -match "can't find") + { + Write-Warning -Message "Unable to Find Host: $ComputerName" + break; + } + } + else + { + $ComputerName = $IPV4 + } + + $null = Start-Job -Name "Png $ComputerName" -ScriptBlock { + param($ComputerName, $TimeOut) + $AppName = "ServerMonitor" + + $counter = 0 + $CheckResult = Test-Connection -ComputerName $ComputerName -Count 1 -ErrorAction SilentlyContinue -ErrorVariable $TestErrorVariable + while($true) + { + $result = ping "$ComputerName" -n 1 -4 -l 32 + if($result -match "bytes=32") + { + New-ToastNotification -AppName $AppName -Title "[UP]: $ComputerName" -Body "$ComputerName is Up. Ping Count: $counter" + if($MonitorType -eq "StateChange") + { + + } + break; + } + elseif($result -match "Ping request could not find host") + { + New-ToastNotification -AppName $AppName -Title "[FAIL]: $ComputerName" -Body "Unable to Lookup: $ComputerName. Ping Count: $counter" + break; + } + else + { + if($counter -ge $TimeOut) + { + New-ToastNotification -AppName $AppName -Title "[TIMEOUT]: $ComputerName" -Body "$ComputerName Has been unpingable for the max TimeOut: $TimeOut. Ping Count: $counter" + break; + } + $counter++ + Start-Sleep 1 + } + } + } -argumentlist $ComputerName, $TimeOut + } +} \ No newline at end of file diff --git a/ServerStateMonitor/Test.ps1 b/ServerStateMonitor/Test.ps1 new file mode 100644 index 0000000..771831d --- /dev/null +++ b/ServerStateMonitor/Test.ps1 @@ -0,0 +1,128 @@ +function Watch-Server +{ + [CmdletBinding()] + param + ( + [Parameter( + Position = 0, + Mandatory = $true + )] + [ValidateNotNullOrEmpty()] + [String] $ComputerName, + + [Parameter( + Position = 1, + Mandatory = $false + )] + [ValidateNotNullorEmpty()] + #5 hours + [Int] $TimeOutSeconds = $(5*60*60) + + # [Parameter( + # Position = 2, + # Mandatory = $false + # )] + # [ValidateNotNullorEmpty()] + # [Int] $IntervalSeconds = 1 + ) + process + { + #Build Job Name to be unique + $JobName = $ComputerName + $JobNameCount = (get-job | Where-Object Name -like $JobName*).Count + $JobName = "$($JobName)_$($JobNameCount)" + + #Define and Start job + # $Job = Start-Job -Name $JobName -ScriptBlock { + # param($ComputerName, $TimeOut) + + $CheckState = $null + $Count = 1 + + while ($true) + { + if ($Count -gt $TimeOutSeconds) + { + ##Notify Exceeded TimeOut + New-ToastNotification -AppName "PNG" -Title "$ComputerName" -Body "$ComputerName Cancelled due to timeout. Ping Count: $Count." + break; + } + ##Test Connection + $null = Test-Connection -ComputerName $ComputerName -Count 1 -ErrorAction SilentlyContinue -ErrorVariable ErrorPing + ###Interpret Results + if(-not $ErrorPing) + { + $ConnectionState = $true + } + elseif($ErrorPing.Exception.InnerException -eq 'No such host is known') + { + # $ConnectionState = $false + ##Notify Host Does Not exist + New-ToastNotification -AppName "PNG" -Title "$ComputerName" -Body "$ComputerName Does Not Exist. Ping Count: $Count." + break; + } + else + { + $ConnectionState = $false + } + + ##Check if State has changed + ###If first check set initial check state. + if(-not $CheckState) + { + $CheckState = $ConnectionState + } + elseif($ConnectionState -ne $CheckState) + { + if($ConnectionState) + { + ##Notify Server up + New-ToastNotification -AppName "PNG" -Title "$ComputerName Up" -Body "$ComputerName is Up. Ping Count: $Count." + break; + } + else + { + ##Notify StateChange + if($ConnectionState) + { + $StateDescription = 'Up' + } + else + { + $StateDescription = 'Down' + } + New-ToastNotification -AppName "PNG" -Title "$ComputerName $StateDescription" -Body "$ComputerName State has Changed: $StateDescription. Ping Count: $Count" + } + + ##Update CheckState + $CheckState = $ConnectionState + } + + # if($ConnectionState) + # { + # Start-Sleep -seconds 1 + # } + ##Always Count. + $Count++; + } + # } -ArgumentList $ComputerName,$TimeOutSeconds + + # #Create Event to clean up job after complete + # $null = Register-ObjectEvent -InputObject $Job -EventName "StateChanged" -Action { + + # #Logic to handle state change + # if($sender.State -match "Complete"){ + # Remove-Job $sender.Id + # } + # else{ + # Write-Warning -Message "Job $($sender.Name) completed with an unexpected status: $($sender.State)." + # } + + # #Unregister event and remove event job + # Unregister-Event -SubscriptionId $Event.EventIdentifier + # Remove-Job -Name $event.SourceIdentifier + # } + } +} + +Watch-Server -ComputerName 'jklann10-v1' \ No newline at end of file