Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
CopyStructure-1/CopyStruc.ps1
CopyStructure-1/CopyStruc.ps1
DnsLookup-1/DnsLookup-1.psm1
TrackPackage-1/.vscode/launch.json
ServerStateMonitor/.vscode/launch.json
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
100 changes: 100 additions & 0 deletions ServerStateMonitor/ServerStateMonitor.psm1
Original file line number Diff line number Diff line change
@@ -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
}
}
128 changes: 128 additions & 0 deletions ServerStateMonitor/Test.ps1
Original file line number Diff line number Diff line change
@@ -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'
75 changes: 75 additions & 0 deletions ToastNotify-1/ToastNotify-1.psm1
Original file line number Diff line number Diff line change
@@ -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)
}
}


Loading