Powershell – DNS lookup

param (
$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 DC_01
server2016.domain.local DC_02
server2016.domain.local DC_09

PowerShell – Set VM permissions

Here is my PS way of granting user access to a VM in vSphere

$vmname = read-host "Enter Virtual Machine name"
$username = read-host "Enter User name (i.e. DOMAIN\username)"
$roles = Get-VIRole | select name | % {$counter = -1} {$counter++; $_ | Add-Member -Name Role_ID -Value $counter -MemberType NoteProperty -PassThru}
$roles | ft -auto
$myRole = read-host "select Role ID"
$role_selection = $roles[$myRole]
get-vm $vmname | New-VIPermission -Role (Get-VIRole -Name $role_selection.name) -Principal $username

PowerShell – Run script in new console window

Have you ever had to run powershell script in a new console window with your for example admin account?

param (
runas /user:domain\user_name "powershell.exe -noExit -command $command"

I have that saved as “run-powershel_command.ps1

run-powershell_command.ps1 "get-aduser bob"

this will ask you for the password for the hardcodded username once that is provided it will open new window and execute the command.

Exchange 2013 and 2016 Server maintenance

I have a two node DAG with the servers ex01 and ex02. I will be putting ex01 into maintenance mode:
1. Launch Exchange Management Shell
2. Use the Set-ServerComponentState command to set the HubTransport component into a draining state
3. Set-ServerComponentState ex01 –Component HubTransport –State Draining –Requestor Maintenance
4. Use the Redirect-Message command to redirect messages currently in the queue – You may get an error message here.
5. Redirect-Message –Server ex01 –Target ex02
6. Use the Suspend-ClusterNode command to pause ex01 from participating in the cluster
7. Suspend-ClusterNode –Name ex01
8. Disable the database copy auto-activation on ex01
9. Set-MailboxServer ex01 –DatabaseCopyActivationDisabledAndMoveNow $true
10. Set DatabaseCopyAutoActivationPolicy to blocked
11. Set-MailboxServer ex01 –DatabaseCopyAutoActivationPolicy Blocked
12. Confirm that there are no Databases mounted on ex01. The command below should return no results.
13. Get-MailboxDatabaseCopyStatus –Server ex01 | Where {$_.Status –eq “Mounted”}
14. If mailboxes are still on ex01 move them to ex02.
15. Move-ActiveMailboxDatabase DB1 -ActivateOnServer ex02
16. Now place the server in Maintenance Mode
17. Set-ServerComponentState ex01 –Component ServerWideOffline –State InActive –Requestor Maintenance

At this point I can perform my maintenance on ex01 (i.e Windows/Exchange updates etc.).
And here are the steps below to bring it back into the cluster.
1. Take ex01 out of maintenance mode
2. Set-ServerComponentState ex01 –Component ServerWideOffline –State Active –Requestor Maintenance
3. Resume cluster operations on ex01
4. Resume-ClusterNode –Name ex01
5. Set Database Copy Auto Activation Policy to Unrestricted
6. Set-MailboxServer ex01 –DatabaseCopyAutoActivationPolicy Unrestricted
7. Set Database Copy Activation Disabled and Move to FALSE
8. Set-MailboxServer ex01 –DatabaseCopyActivationDisabledAndMoveNow $false
9. Set the Hub Transport component to Active
10. Set-ServerComponentState ex01 –Component HubTransport –State Active –Requester Maintenance


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 (
$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