PowerShell – get windows 10 versions

Below script lists all Windows 10 versions that are installed on computers in Active Directory:

$win10 = get-adcomputer -filter 'OperatingSystem -like "Windows 10*"' -prop *
$win10count = ($win10 | Sort OperatingSystemVersion).count
$subject = "All Win 10 systems $win10count"
$win10_versions = $win10 | Sort OperatingSystemVersion | Select OperatingSystemVersion -unique

$result = @()
foreach ($ver in $win10_versions) {

$version = $ver.OperatingSystemVersion
$count = ($win10 | where OperatingSystemVersion -like $ver.OperatingSystemVersion | select OperatingSystemVersion | measure ).count
$result += New-Object psobject -Property @{
Win10_version = $version
Count = $Count
}
}

Send-MailMessage -From email@address.com -To email@address.com -subject $Subject -Body ($result | out-string) -smtpServer MAIL_SERVER -port 25

Example output:
.\get-Windows10_versions.ps1

Win10_version Count
------------- -----
10.0 (10240)      1
10.0 (10586)      9
10.0 (14393)     14
10.0 (15063)      4
10.0 (16299)     23
10.0 (17134)     18
10.0 (17763)     21
10.0 (18362)     35
10.0 (18363)    140
10.0 (19041)      1

PowerShell – Out-Notepad function

There are many out-notepad functions out there but for some reason I found them either too long or too complicated so here is mine, short and sweet:

Function Out-Notepad {
 [CmdletBinding()]
    Param
      (  
        [Parameter(Mandatory=$true,
        ValueFromPipeline=$true,
        Position=0)]
        $StrText
       )
$fso=new-object -com scripting.filesystemobject
$filename=$fso.GetTempName()
$tempfile=Join-Path $env:temp $filename
$strText | Out-File $tempfile
notepad $tempfile
#tidy up
sleep 3
if (Test-Path $tempfile) {del $tempfile}
}

Powershell – Exchange – enable/disable junk mail rule in a mailbox

Set-MailboxJunkEmailConfiguration -Enabled <$true | $false>

To check:

Get-MailboxJunkEmailConfiguration

To disable the junk email rule on all mailboxes in Exchange:

$All = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited
$All | foreach {
Set-MailboxJunkEmailConfiguration $_.Name -Enabled $false
}

Powershell – DNS lookup

param (
[Parameter(Mandatory=$true)]
[string]$HostName
)
$output = $null
$output = @()
$DNSServers = Get-ADDomainController -Filter *
$DNSServers | foreach {
$query = Resolve-DnsName -Name $hostname -Server $_.HostName
$result = New-Object psobject -Property @{
dnsserver = $_.Name
hostname = $query.name
IPAddress = $query.ipaddress
}
$output += $result
}
$output | select hostname,ipaddress,dnsserver | sort ipaddress

save it as for example: dns-lookup.ps1 then

PS C:\Scripts> .\dns-lookup.ps1 server2016
hostname IPAddress dnsserver
-------- --------- ---------
server2016.domain.local 192.168.1.53 DC_01
server2016.domain.local 192.168.1.53 DC_02
server2016.domain.local 192.168.1.53 DC_09

PowerShell – count netstat established connections

I wanted to see how many established connection are there on a server and group per address/host. So I put together this little script.

param (
[Parameter(Mandatory=$true)]
[string]$ComputerName
)
$a = Invoke-Command -ComputerName $ComputerName { netstat -a }
$b = ($a[3..$a.count] | ConvertFrom-String | where p5 -eq "ESTABLISHED" | select -ExpandProperty p4)
$b -replace ":.*" | group | select count,name

I was mainly interested in the Foreign Address netstat column.
The output looks like this:

Count Name
----- ----
3 lhr25s07-in-f3
1 ec2-52-22-131-91
5 lhr25s07-in-f10
3 lhr25s11-in-f35
5 lhr25s07-in-f14
1 lhr25s11-in-f14
5 ec2-34-235-109-75
2 lhr35s05-in-f74