Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Tuesday, April 14, 2015

Exchange 2013: Database copy has been blocked from automatic activation on server by an administrative action. Reason: None specified.

After updating two Exchange 2013 mailbox servers in a DAG environment to CU8 and running Test-Replicationhealth, Database Availability reported an error:
I tried to use -autosize to show the entire error which wouldn't work, but after googling around I found this:

 $r = Test-ReplicationHealth  
 $r | ?{$_.Result.Value -ne 'Passed'} | fl  

The entire error is as follows: Database copy 'DB01' has been blocked from automatic activation on server 'EXCH2' by an administrative action. Reason: None specified..

Its actually an easy fix, go into the ECP, Servers -> databases, suspend the passive healthy DB, wait a moment then Resume it.
Now test-replicationhealth again and you should be good.

Tuesday, November 18, 2014

Powershell: Format-table -Autosize, must remember

I am typing this one out so I remember the -Autosize switch when using Powershell.  Dealing with data in Powershell is practically hopeless without it.  Example:


Sorry the AD tree is so large it can't show the users name, but the black space after the name feild is just offending.

How to fix this?

Powershell is an object based language, and most objects are going to have a lot of data, so when that data gets pushed to a terminal sometimes things don't look so pretty, and in the example above, completely worthless.  Thankfully the Powershell dev's had the same opinion and made the Format-Table command-let in order to help with displaying data on the console.  In this example, just add the Format-Table -Autosize switch to auto width the columns to show the entire string.  For short use ft for Format-Table, and pipe your commandlet to it:
| ft -Autosize


Usable data!

Wednesday, August 13, 2014

Powershell: Script to purge old files from a folder

So I need to backup the SQL DB of a mission critical app.  The app sits on a virtual machine and backups need to get written to a NAS device.  But if there is a network or NAS issue then the backups won't get written, so no current backups.  What I do in this scenario is write the backups locally first, to c:\DB_backups, then script a task to sync the backup directory to the NAS.  If I have daily backups then the NAS will store all of the daily rolling backups, but that will eat up space on the C drive, which is bad.  So I need to purge the DB_backups folder, there are a few ways to do this and the easiest I found is to use powershell:
 Get-ChildItem 'C:\DB_Backup' -Recurse | Where {$_.creationtime -lt (Get-Date).AddDays(-10)} | Remove-Item  
This is pretty clever on powershell's part, lets step through what it does.
Get-ChildItem 'C:\DB_Backup' -Recurse
What this does is list all of the items in this folder as objects, and I am recursing through all of the folders in this folder, but that is not necessarily needed for this job.
Where {$_.creationtime -lt (Get-Date).AddDays(-10)}
The objects in this folder is then piped to this line.  Where is an alias for Where-Object.  We are using where because we only want to select files with specific attributes from this directory.  These attributes are defined by the $_.creationtime.  The creation time is then compared to today's date (Get-Date) using -lt (less than).  But we only want the files older than 10 days ago, so we subtract 10 from Get-Date, using the AddDays modifier.  So now we are listing all of the files older than 10 days, pretty neat, now to delete them.
Remove-Item
The cmd way of doing things is using del, which is actually an alias for -Remove-Item, neat.  This will remove the 10 day old objects we piped to it in the previous line.

Save it all as a .ps1 file and schedule the job in task scheduler to run daily to keep your C drive to size.

Tuesday, August 12, 2014

Hyper-V Server: Enable firewall rule to allow Hyper-V Replica over HTTP or HTTPS

The server is Hyper-V Server 2012 r2 Core and we need to enable the inbound replica traffic on the firewall.  There is no firewall GUI in server core but there is predefined rules for Hyper-V Replica which we need to enable via powershell.  There are several ways for remote access to server core, this way will use RDP because of my wonderful RDP organization with mRemoteNG.  Log on to your Hyper-V Server, move the sconfig window to the side and use the command prompt to open a powershell session.


Powershell can be ran from any directory.  Once in powershell list the default firewall rules by using the Get-NetFirewallRule command-let:

 Get-NetFirewallRule | format-table name, displaygroup, action, direction, enabled -autosize  

The table is pretty long and you may have to widen your console window to show all of the rows, but you will be looking for something like this:


Hyper-V Replica HTTP and Hyper-V Replica HTTPS are the two rules, I went with HTTP in my Hyper-V settings so I will enable that rule by using the Enable-NetFirewallRule command-let:

 Enable-NetFirewallRule -DisplayGroup "Hyper-V Replica HTTP"  

And now we have flowing replica traffic: