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"
        return
    }
    $t = New-Object Net.Sockets.TcpClient
    # We use Try\Catch to remove exception info from console if we can't connect
    try
    {
        $t.Connect($ip,$port)
    } catch {}

    if($t.Connected)
    {
        $t.Close()
       $true
    }
    else
    {
     $false                              
    }

}

Find HP machine age from powershell script

Collects warranty start date from HP warranty website

Link to the script: find_hp_warranty

Powershell:


$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 ("http://h20564.www2.hp.com/hpsc/wc/public/find") -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 ("http://h20564.www2.hp.com/hpsc/wc/public/find") -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"
$result
}

Set Out of Office on a Mailbox using Powershell for Exchange

To get the existing settings

Get-MailboxAutoReplyConfiguration “username”

To set the Out of Office (internal and external)

Set-MailboxAutoReplyConfiguration -identity “username” -AutoReplyState enabled -EndTime 08/11/2011 -InternalMessage “username will be out of the office from Friday 21st October until Tuesday 8th saving the world.” -ExternalMessage “username will be out of the office from Friday 21st October until Tuesday 8th saving the world.” 

To disable Out of Office/Auto Reply

Set-MailboxAutoReplyConfiguration “username” -AutoReplyState disabled

To disable Out of Office/Auto Reply and clear the Internal/External message

Set-MailboxAutoReplyConfiguration -identity “username” -AutoReplyState disabled -InternalMessage “” -ExternalMessage “”

Powershell mail all files from directory and move them

Get-ChildItem “C:\FilePorts\ValidationPDFs\*” -include *.pdf | foreach { Send-MailMessage -from “from@mail.com” -to “to@mail.com” -subject $_.name -attachments $_.fullname -smtpServer mail.server }
Get-ChildItem “C:\FilePorts\ValidationPDFs\*” -include *.pdf | move-item -destination “C:\FilePorts\ValidationPDFs\printet”

Powershell script, get locked account from multiple Windows 2008 servers

Param(
[Parameter(Mandatory=$True)]
[string]$Username,

[Parameter(Mandatory=$False)]
[array]$Computers=@(“ad-server1”, “ad-server2”)
)

$Date = [DateTime]::Now.AddDays(-2)
Get-EventLog “Security” -after $Date -computer $Computers | Where-Object { $_.InstanceID -eq 4740 -or $_.InstanceID -eq 4625} | ? { $_.message -match $Username} | fl TimeGenerated, Message