Skip to content

Instantly share code, notes, and snippets.

@adbertram
Created May 27, 2024 13:19
Show Gist options
  • Save adbertram/e2a675e3c3f248f1a0325af1e9a9b673 to your computer and use it in GitHub Desktop.
Save adbertram/e2a675e3c3f248f1a0325af1e9a9b673 to your computer and use it in GitHub Desktop.
# Define a custom object for a server or whatever object you want
$server = [pscustomobject]@{
Name = "Server1"
IPAddress = "192.168.0.1"
}
# Add a ScriptMethod to check if the server is online
## This can run any code you may like to associated with an object
$server | Add-Member -MemberType ScriptMethod -Name "IsOnline" -Value {
param ($Timeout = 1000)
Test-Connection -ComputerName $this.IPAddress -Count 1 -Quiet
}
## Call the script method
$server.IsOnline()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment