Home » Posts tagged 'One-liner'

Tag Archives: One-liner

Advertisements

Get all cluster groups in an AD domain and their current owners

The purpose of this article is to show you how to get all cluster groups in an AD domain and their current owners. This script has been tested on Windows 2008 clusters in an Active Directory domain.

Script :

Get-Cluster -Domain domain.local | % {Get-Cluster -Name $_.name | Get-Clustergroup | select cluster,name,ownernode}

Source: https://www.shellandco.net/get-all-cluster-groups-in-an-ad-domain-and-their-current-owners/

Advertisements

Find Largest (Or Smallest) Files In A Directory Or Drive With PowerShell

One of our SQL servers was running low on disk space and I needed to quickly find the largest files on the drive to know what was eating up all of the disk space, so I wrote this PowerShell line that I thought I would share:

1
2
# Get all files sorted by size.
Get-ChildItem -Path 'C:\SomeFolder' -Recurse -Force -File | Select-Object -Property FullName,@{Name='SizeGB';Expression={$_.Length / 1GB}},@{Name='SizeMB';Expression={$_.Length / 1MB}},@{Name='SizeKB';Expression={$_.Length / 1KB}} | Sort-Object { $_.SizeKB } -Descending | Out-GridView

If you are still only running PowerShell 2.0, it will complain that it doesn’t know what the -File switch is, so here’s the PowerShell 2.0 compatible version (which is a bit slower):

1
2
# Get all files sorted by size.
Get-ChildItem -Path 'C:\SomeFolder' -Recurse -Force | Where-Object { !$_.PSIsContainer } | Select-Object -Property FullName,@{Name='SizeGB';Expression={$_.Length / 1GB}},@{Name='SizeMB';Expression={$_.Length / 1MB}},@{Name='SizeKB';Expression={$_.Length / 1KB}} | Sort-Object { $_.SizeKB } -Descending | Out-GridView

Just change ‘C:\SomeFolder’ to the folder/drive that you want scanned, and it will show you all of the files in the directory and subdirectories in a GridView sorted by size, along with their size in GB, MB, and KB. The nice thing about using a GridView is that it has built in filtering, so you can quickly do things like filter for certain file types, child directories, etc.

Here is a screenshot of the resulting GridView:

FilesSortedBySize

 

And again with filtering applied (i.e. the .bak at the top to only show backup files):

FilesSortedBySizeAndFiltered

All done with PowerShell; no external tools required.

Source:http://blog.danskingdom.com/find-largest-or-smallest-files-in-a-directory-or-drive-with-powershell/

One-Liner: Find Manually Created Replication Connection Objects

Replication connections between domain controllers are created by the Knowledge Consistency Checker (KCC). The KCC runs every 15 minutes and will adjust the replication topology in response to configuration changes or to a domain controller becoming unavailable.

Here’s a smashing picture of an Active Directory replication topology, created and optimised by the KCC. The blue arrows are the replication connections.

 

 

Now, sometimes, admins take it on themselves to manually create replication connections between domain controllers… believe me, you’re more often than not best off letting the KCC do its remarkable stuff. Not only is the result optimised, it’s also resilient.

 

How to find manually created replication connections in your domain?

You’ll need PS v3 + and, specifically, the Active Directory replication cmdlets introduced with Windows Server 2012. Here’s a one-liner, using Get-ADReplicationConnection, to retrieve manually created replication connection objects:

Get-ADReplicationConnection -Filter {AutoGenerated -eq $False} |

Select-ObjectName,AutoGenerated,ReplicateToDirectoryServer,ReplicateFromDirectoryServer |

Out-GridView

 

The filter applied to Get-ADReplicationConnection – "AutoGenerated -eq $False" – speaks for itself. Select-Objectspecifies certain properties. Out-Gridview provides a graphical output. Here’s a sample:

 

Source:  http://blogs.technet.com/b/poshchap/archive/2014/05/23/one-liner-find-manually-created-replication-connection-objects.aspx

One-liner: Find Authorised DHCP Servers

The DHCP PowerShell module has the Get-DhcpServerInDC cmdlet to show you the DHCP servers that have been authorised in Active Directory.

This cmdlet was introduced with Windows Server 2012 and v3 of PowerShell.

What if you don’t have access to the above? What if you want to impress your PoSh Chickens and get a list of authorised DHCP servers by using the Active Directory PowerShell module?

What, indeed!

Run this:

Get-Adobject -Filter {objectclass -eq “dhcpclass”} -SearchBase”CN=NetServices,CN=Services,CN=Configuration,DC=halo,DC=net” | Where-Object{$_.name -ne “DhcpRoot”}

Source: http://blogs.technet.com/b/poshchap/archive/2016/01/22/use-get-adobject-to-find-authorised-dhcp-servers.aspx