Thursday 8 May 2014

Powershell to find User details from AD

Seen a computername hitting your databases not sure who it is.  This will help you find the owner information of the computer.  Enter the PC name, and it will show the details stored in AD under owner.

$strResponse

= "Quit"

do

{

$machine

= Read-Host "Please supply computer to lookup"

$strFilter

= "(&(objectCategory=Computer)(name="+$machine+"))"

$objDomain

= New-Object System.DirectoryServices.DirectoryEntry

$objSearcher

= New-Object System.DirectoryServices.DirectorySearcher

$objSearcher

.SearchRoot = $objDomain

$objSearcher

.PageSize = 1000

$objSearcher

.Filter = $strFilter

$objSearcher

.SearchScope = "Subtree"

$colProplist

= "name", "description"

foreach

($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults

= $objSearcher.FindAll()

foreach

($objResult in $colResults)

{
$objItem = $objResult.Properties; $objItem.name; $objItem.description}

 


$strResponse

= Read-Host "Are you sure you want to quit application? (Y/N)"}


until ($strResponse -eq "Y")

Ckeck whats using the Log

Ever wnated to truncate a log and found that its in use?  Or that it cant be shrunk, even after a backup.  Well maybe its in use.  Use this to find out...

 DECLARE @DatabaseName VARCHAR(50);
 SET @DatabaseName = '<Databasename>'
 SELECT name, recovery_model_desc, log_reuse_wait_desc
   FROM sys.databases
   WHERE name = @DatabaseName

Wednesday 7 May 2014

Fail all Availibility Groups to local node

Use this to fail over AG's back to the local node.

select 'ALTER AVAILABILITY GROUP ['+name+'] FAILOVER;
go'
from sys.availability_groups
order by name desc