Items filtered by date: March 2015

We, Microsoft Exchange Admins, usually or frequently edit hosts file to verify some changes prior to the production. The below script will enable you to modify (adding or removing host entries) the hosts file from a remote computer. The script will also record the machine names against the host entries are changed.

There are two scripts, first one to add the entries to the hosts file, and the second one for removing it once any testing is completed.

Script for Adding Entries to Hosts file

function add-host([string]$filename)
{  
   $addip = "10.10.10.100"
   $addhostnames = "casarray.domain.local"
   foreach ($addhostname in $addhostnames)
       {
       $HostEntries = Get-Content $filename
       $Count = 0
       foreach ($line in $HostEntries)
           {
           $bits = [regex]::Split($line, "\t+")
           if ($bits.count -eq 2)
               {
               if ($bits[1] -eq $addhostname) {$Count+=1}                
               }
           }
       if($Count -eq 0)
           {
           Write-Host "Adding Host Entry" $addip "`t`t" $addhostname
           $addip + "`t`t" + $addhostname | Out-File -encoding ASCII -append $filename
           }
       else
           {
           Write-Host "Host Entries already exists"
           }
   }
}
function add-list([string]$ComputerList,[string]$ComputerName)
{
$ComputerNames = Get-Content $ComputerList
$count = 0
foreach($computer in $ComputerNames)
   {
   if ($computer -eq $ComputerName){$count+=1}
   }
if($count -eq 0)
   {
   $ComputerName | Add-Content $ComputerList
   Write-Host "Computer Name," $ComputerName "added in the list"
   }
   cmd /c pause
}
$date = Get-Date -Format "dd_MM_yyyy_hh_mm"
$computers = Get-Content "c:\Script\ComputerList.txt"
$ModifiedComputerNames = "C:\Script\ModifiedComputerNames.txt"
foreach ($computer in $computers)
{
$file = “\\”+ $computer + "\C$\Windows\System32\drivers\etc\hosts"
$fileCopy = “\\”+ $computer + "\C$\Windows\System32\drivers\etc\hosts.bak." + $date
cpi $file $fileCopy
add-host $file
add-list $ModifiedComputerNames $computer
}

You need to enter the list of computers for which the hosts file needs to be edited in the file c:\Script\ComputerList.txt. You may use a different path and file name, but remember to amend the script accordingly.

Script for Removing Entries to Hosts file

Following script will help you remove the temporarily added hosts entries for list of computers.

function remove-host([string]$filename )
{
$rmhostnames = "casarray.domain.local"
foreach ($rmhostname in $rmhostnames)
{
$c = Get-Content $filename
$newLines = @()
foreach ($line in $c)
{
$bits = [regex]::Split($line, "\t+")
if ($bits.count -eq 2)
   {
   if ($bits[1] -ne $rmhostname)
       {
       $newLines += $line
       }
   }
else
   {
   $newLines += $line
   }
}
# Write file
Clear-Content $filename
foreach ($line in $newLines)
   {
   $line | Out-File -encoding ASCII -append $filename
   Start-Sleep -m 100
   }
}
}
$computers = Get-Content "C:\Script\ModifiedComputerNames.txt"
foreach ($computer in $computers)
{
Write-Host $computer
$file = “\\”+ $computer + "\C$\Windows\System32\drivers\etc\hosts"
remove-host $file
}

In both the script, I have added a time wait to avoid network file editing errors. Which means, if the script executes really fast, the lock on the file might create writing errors. A slight time wait function will manage this situation efficiently.

Share your comments if any.

-Praveen

Published in Solutions

Couple of times in the past, I observed this behavior that the database copy status turns to DisconnectedAndResynchronizing from DisconnectedAndHealthy after removing the activation restriction.

DisconnectedAndResynchronizing

This situation arise because, the copy try to replay the recent logs from other nodes. If your database copy could not find any copy to copy the pending logs, the status would stay the same for longer period.

Solution:

If you wish to have 2 DAG cluster with one of the cluster node as DR instance, then it is not a good idea to keep the ActivationSuspended parameter to true. Following are the approach you should consider to ensure a seamless recovery in case of a failover.

Option 1:

      1. Disable the ActivationSuspended value in the second node

      2. The database would failover immediately in the event of a node failure.

Option 2:

      1. Enable the ActivationSuspended value in the DR node

      2. In the event of active node failure, follow the DR procedure to evict the failed node prior to release the Activation suspension

It is recommended that, activation suspension can be released either when the nodes are available or after evicting failed nodes from the DAG cluster.

-Praveen

Published in Solutions
theme by reviewshub