Function for Powershell to test if TCP port answers

function Test-Port($hostname, $port)
    # This works no matter in which form we get $host - hostname or ip address
    try {
        $ip = [System.Net.Dns]::GetHostAddresses($hostname) | 
            select-object IPAddressToString -expandproperty  IPAddressToString
        if($ip.GetType().Name -eq "Object[]")
            #If we have several ip's for that address, let's take first one
            $ip = $ip[0]
    } catch {
        Write-Host "Possibly $hostname is wrong hostname or IP"
    $t = New-Object Net.Sockets.TcpClient
    # We use Try\Catch to remove exception info from console if we can't connect
    } catch {}



Remote powershell

#Allow remote powershell
Enable-PSRemoting -Force 
Enable-WSManCredSSP -Role Server -Force
Get-Service winrm | Start-Service 
Enable-WSManCredSSP -Role Client -DelegateComputer * -Force 
Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowFreshCredentials -Name WSMan -Value WSMAN/* 
Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\Credssp\PolicyDefaults\AllowFreshCredentialsDomain -Name WSMan -Value WSMAN/*  

#Test from an other machine: 
Invoke-Command -ComputerName Remote-Server -ScriptBlock {Get-host}

#Allow crossdomain:
#On remote server do the above and:
New-Itemproperty -name LocalAccountTokenFilterPolicy -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -propertyType DWord -value 1

#On remote and local machine run:
Set-item wsman:localhost\client\trustedhosts -value 'server1,server2,server3'

#Test from an other machine: 
$credential = Get-Credential
Invoke-Command -ComputerName RM-SERVER -ScriptBlock {Get-Process} -Credential $credential

Send SMS via modem and Powershell

$comport = "COM10"
$msg = get-date
$ext = "+45"
$number = $ext + "12345678"

$serialPort= new-Object System.IO.Ports.SerialPort $comport,9600,None,8,one
$serialPort.Write("AT+CMGF=1`r`n") ; $serialPort.Readline()
$serialPort.Write("AT+CMGS=`"$number""`r`n"); $serialPort.Readline()
$serialPort.Write("$msg`r`n") ; $serialPort.Readline()
$serialPort.Write($([char] 26))

Find HP machine age from powershell script

$sn = "CZC12068T4"
$country = "DK"

$body = "rows%5B0%5D.item.serialNumber=" + $sn + "&rows%5B0%5D.item.countryCode=" + $country + "&rows%5B1%5D.item.serialNumber=&rows%5B1%5D.item.countryCode=US&rows%5B2%5D.item.serialNumber=&rows%5B2%5D.item.countryCode=US&rows%5B3%5D.item.serialNumber=&rows%5B3%5D.item.countryCode=US&rows%5B4%5D.item.serialNumber=&rows%5B4%5D.item.countryCode=US&rows%5B5%5D.item.serialNumber=&rows%5B5%5D.item.countryCode=US&rows%5B6%5D.item.serialNumber=&rows%5B6%5D.item.countryCode=US&rows%5B7%5D.item.serialNumber=&rows%5B7%5D.item.countryCode=US&rows%5B8%5D.item.serialNumber=&rows%5B8%5D.item.countryCode=US&rows%5B9%5D.item.serialNumber=&rows%5B9%5D.item.countryCode=US&submitButton=Submit"

$r=Invoke-WebRequest -Uri ("") -Method POST -Body $body

$ss = $r.RawContent.IndexOf("Wty: HP Support for Initial Setup")

# Extra run, in case of not found. 
if($ss -lt 100) {
$r=Invoke-WebRequest -Uri ("") -Method POST -Body $body
$ss = $r.RawContent.IndexOf("Wty: HP Support for Initial Setup")

if($ss -gt 100) {
$result = $r.rawcontent.substring($ss+42,20)
$end = $result.IndexOf("</")
$result = $result.Substring(0,$end)
get-date -Date $result -UFormat "%Y/%m/%d"

else {
$result = "not found"