$socket = new-object Net.Sockets.TcpClient
$socket.Connect($IPAddress,$Port)
$socket.Connected
Just supply an IP Address and Port number and it will return true or false, based on if it was able to connect.
$socket = new-object Net.Sockets.TcpClient
$socket.Connect($IPAddress,$Port)
$socket.Connected
Test-NetConnection -Computer $Server -CommonTCPPort RDP
PS C:\> $Server = 'AD01' >> Test-NetConnection -Computer $Server -CommonTCPPort RDP ComputerName : DC01 RemoteAddress : 10.10.5.12 RemotePort : 3389 InterfaceAlias : vEthernet (External (NIC)) SourceAddress : 10.10.5.200 TcpTestSucceeded : True
Function Check-HyperThreading($ComputerName){
# Get the Processor information for the computer
$Proc = Get-WMIObject Win32_Processor -ComputerName $ComputerName
# Determine if hyper-threading is enabled
$CPUCount = @($Proc).count
$NumberOfLogicalProcessors = $($Proc | measure-object -Property NumberOfLogicalProcessors -sum).Sum
$NumberOfCores = $($Proc | measure-object -Property NumberOfCores -sum).Sum
# Output the results as a PS object
[pscustomobject]@{
Computer = $ComputerName
CPUCount = $CPUCount
NumberOfLogicalProcessors = $NumberOfLogicalProcessors
NumberOfCores = $NumberOfCores
HyperThreading = $($NumberOfLogicalProcessors -gt $NumberOfCores)
}
}
PS C:\> Function Check-HyperThreading($ComputerName){ >> # Get the Processor information for the computer >> $Proc = Get-WMIObject Win32_Processor -ComputerName $ComputerName >> # Determine if hyper-threading is enabled >> $CPUCount = @($Proc).count >> $NumberOfLogicalProcessors = $($Proc | measure-object -Property NumberOfLogicalProcessors -sum).Sum >> $NumberOfCores = $($Proc | measure-object -Property NumberOfCores -sum).Sum >> # Output the results as a PS object >> [pscustomobject]@{ >> Computer = $ComputerName >> CPUCount = $CPUCount >> NumberOfLogicalProcessors = $NumberOfLogicalProcessors >> NumberOfCores = $NumberOfCores >> HyperThreading = $($NumberOfLogicalProcessors -gt $NumberOfCores) >> } >> } >> >> Check-HyperThreading -ComputerName $env:COMPUTERNAME Computer : MAT-F0CXE4G CPUCount : 1 NumberOfLogicalProcessors : 4 NumberOfCores : 2 HyperThreading : True
Invoke-RestMethod 'https://api.ipify.org?format=json' | Select-Object -ExpandProperty IP
PS C:\> Invoke-RestMethod 'https://api.ipify.org?format=json' | Select-Object -ExpandProperty IP 72.30.35.10
[System.Net.Dns]::GetHostbyAddress($IPAddress)
PS C:\> $IPAddress = "8.8.8.8" >> [System.Net.Dns]::GetHostbyAddress($IPAddress) HostName Aliases AddressList -------- ------- ----------- google-public-dns-a.google.com {} {8.8.8.8}
[System.Net.Dns]::GetHostAddresses($ServerName)
PS C:\> $ServerName = 'DC01' >> [System.Net.Dns]::GetHostAddresses($ServerName) Address : 167840266 AddressFamily : InterNetwork ScopeId : IsIPv6Multicast : False IsIPv6LinkLocal : False IsIPv6SiteLocal : False IsIPv6Teredo : False IsIPv4MappedToIPv6 : False IPAddressToString : 10.10.1.10
Get-WmiObject Win32_OperatingSystem -ComputerName $computer | Select @{Label = "Computer"; Expression = {$computer}},
@{Label = "MemoryUsage"; Expression = {100 - [math]::Round(($_.FreePhysicalMemory/$_.TotalVisibleMemorySize)*100,0)}}
PS C:\> $computer = 'localhost' >> Get-WmiObject Win32_OperatingSystem -ComputerName $computer | Select @{Label = "Computer"; Expression = {$computer}}, >> @{Label = "MemoryUsage"; Expression = {100 - [math]::Round(($_.FreePhysicalMemory/$_.TotalVisibleMemorySize)*100,0)}} Computer MemoryUsage -------- ----------- localhost 69
Get-WmiObject win32_processor -ComputerName $computer | Measure-Object -property LoadPercentage -Average |
Select @{Label = "Computer"; Expression = {$computer}},@{Label = "CpuUsage"; Expression = {$_.Average}}
PS C:\> $computer = 'localhost' >> Get-WmiObject win32_processor -ComputerName $computer | Measure-Object -property LoadPercentage -Average | >> Select @{Label = "Computer"; Expression = {$computer}},@{Label = "CpuUsage"; Expression = {$_.Average}} Computer CpuUsage -------- -------- localhost 15
Get-NetIPAddress
PS C:\> Get-NetIPAddress IPAddress : 10.10.1.10 InterfaceIndex : 12 InterfaceAlias : Ethernet AddressFamily : IPv4 Type : Unicast PrefixLength : 24 PrefixOrigin : Manual SuffixOrigin : Manual AddressState : Preferred ValidLifetime : Infinite ([TimeSpan]::MaxValue) PreferredLifetime : Infinite ([TimeSpan]::MaxValue) SkipAsSource : False PolicyStore : ActiveStore
Get-WmiObject Win32_PNPEntity | Sort-Object -Property PNPClass | Format-Table Name, PNPClass, DeviceID, Manufacturer, Status
Source: Thorsten E.
# Open the specified Registry Hive on a remote machine specified. LocalMachine = HKLM / CurrentUser = HKCU
$computer = 'localhost'
$w32reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$computer)
# Open the specific registry key (exclude hive from path)
$keypath = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI'
$SubKey = $w32reg.OpenSubKey($keypath)
# Return data for a specific value
$SubKey.GetValue('LastLoggedOnUser')
# List all values
$SubKey.GetValueNames()
# Return data for all values to hashtable
$AllValues = @{}
$SubKey.GetValueNames() | ForEach-Object{
$AllValues.Add($_,$SubKey.GetValue($_))
}
$AllValues
This script that will send a reboot command to a remote computer. It will then monitor for it to go offline and then come back online. Optionally, you can have it monitor for a service to return to running.
# The remote computer to reboot
$computer = 'YourComputer'
# the name of a service to check for after reboot
$Service = 'WinRM'
# send reboot command to remote computer
Restart-Computer -ComputerName $computer -Force -ErrorAction Stop
# ping computer every second until it goes offline
do{
$test = Test-Connection -ComputerName $Computer -Count 1 -Quiet
Start-Sleep -s 1
}while($test -ne $false)
# ping computer every 10 seconds until it comes back online
while($test -eq $false){
$test = Test-Connection -ComputerName $Computer -Count 1 -Quiet
Start-Sleep -s 10
}
# wait for service to show running
if(-not [string]::IsNullOrEmpty($Service)){
do{
$test = Get-Service -Name -ComputerName $Computer -ErrorAction SilentlyContinue
Start-Sleep -s 10
}while($test.Status -ne 'Running')
}
Get-ScheduledTask | Select-Object * | Out-Gridview
Source: Guy Leech