Sunday, March 26, 2017

Removing all Partitions from a Disk

Today I thought of writing an article on how to remove all the partitions from a disk so you can re-use the disk.

If you tried removing existing partitions using the Computer Management (type computer management on the start menu to open it) tool in Windows you will find that, you can delete partitions by right clicking on the partition / volume and clicking on Delete Volume option.

image

However you will notice Computer Management is useless on some of the protected volumes since the options to work on them will not be available.In this case you will need to use the DiskPart utility in the Command Prompt.

image

In the Start type Cmd or Command Prompt to open a command prompt. Then type DiskPart, it is a powerful utility which can even work on protected volumes.

In my case, I would like to work on Disk 3 to remove all the existing volumes on it. Below are the steps required to clean the drive.

1. Select the disk.

List all the disks by typing List Disk, this will list all the disks connected and active at the time in your computer including the disks connected via USB ports.

image

Once you identify the disk you need, selection can be made by using the command Select Disk x. In my case Select Disk 3.

To confirm, list all the partitions on the selected list by using the command List Partition.

image

2. Delete all partitions by using the Clean command.

Ensure the proper disk is selected by Listing all disks by command List Disk, the currently focused disk will have a star at the beginning since recovering disks is not easy.

Type Clean and press enter. This will remove all the partitions from the disk. You can check the results by re issuing the List Partition command.

image

Close the DiskPart tool by entering the command Exit.

Computer Management will now show that the disk is uninitialized as below, Initialize the disk and create partitions / volumes as necessary to use it again.

image

Removing a Partition from a Disk

You can remove simple partitions by using the Computer Management tool found in Windows. Search for it on the Start menu to launch it.

But if it is a protected partition you will need to use the tool DiskPart. In the Start menu type Cmd or Command Prompt to open a command prompt. Then type DiskPart, it is a powerful utility which can even work on protected volumes.

To delete one partition, follow the steps.

1. List all disks by using the command List Disk.

2. Select the disk by using Select Disk x command.

3. You can confirm the disk by listing all the partitions on it by using the command List Partition.

4. Select the partition by using the command, Select Partition x command.

5. Delete the partition by command Delete Partition Override.

Override parameter needs to be passed if the partition is not a simple data partition.

You can confirm the deletion by re-listing the partitions on the disk by List Partition command.

image_thumb[15]

Sunday, March 19, 2017

SQL – The target principal name is incorrect. Cannot generate SSPI context.

Recently in one of my Virtual Machine (VM)s I received the above error message when trying to connect to a SQL Server which I used to connect on other times.

image

After having a look I found that the trust between my virtual machine and the domain was broken. You can find the status of the secure channel by using the PowerShell command Test-ComputerSecureChannel.

image

This will also be evident when you try to login to the computer using a domain account. It will generate the following message.

image[6]

To fix this you can try using the below methods.

1. PowerShell

Use the command Test-ComputerSecureChannel.

If your current login has the required access in the domain you can use the below command.

Test-ComputerSecureChannel –Repair

If you need to use another account than the current logged user then you need to use the –Credential parameter when calling the command.

Test-ComputerSecureChannel –Repair –Credential MyDomain\MyUser

2. Joining the domain again.

This will also get fixed by removing the machine from the domain and adding it back. Before removing the computer from the domain make sure you have access to a local administrator account on the computer. Otherwise you will not have a way to login to the computer.

This can be achieved by going to computer system properties,

  1. removing the computer from the current domain,
  2. restarting the computer.
  3. adding the computer to the domain again
  4. restarting the computer

To avoid the two restarts you can try using the following PowerShell commands.

$myPC = Get-WmiObject Win32_ComputerSystem
$myPC.UnjoinDomainOrWorkGroup("Account Password", "Account Username”, 0)
$myPC.JoinDomainOrWorkGroup("Domain", "Account Password", "Account Username", $null, 3)
Restart-Computer -Force

Monday, December 26, 2016

Open Live Writer – Plugin Not Working

While trying to setup and use the Code Plugin by Rich Hewlett I had trouble getting the plugin loaded into Live Writer. I did add the registry entry required as mentioned in the site, but still the plugin did not load.

Registry

Location - HKEY_CURRENT_USER\SOFTWARE\OpenLiveWriter\PluginAssemblies

Key - SyntaxHighlight_WordPressCom_OLWPlugIn

Value - C:\Users\Arjuna\AppData\Local\OpenLiveWriter\Plugins\SyntaxHighlight_WordPressCom_OLWPlugIn.dll

After a while I figured out that, when loading the plugin Live Writer encounters the following error.

"System.IO.FileLoadException: Could not load file or assembly 'file:///C:\Users\Arjuna\AppData\Local\OpenLiveWriter\Plugins\SyntaxHighlight_WordPressCom_OLWPlugIn.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
File name: 'file:///C:\Users\Arjuna\AppData\Local\OpenLiveWriter\Plugins\SyntaxHighlight_WordPressCom_OLWPlugIn.dll' ---> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.

This is due to a security feature of .Net Framework 4 or later. Before framework 4, if a DLL is downloaded (which is created in another computer), they used to run in full trust in the zone the assembly is running, but with frameworks 4 and later, downloaded DLLs will not run by default. To make them run, simply grant the DLL full access by going to file properties and selecting the Unblock checkbox in the security section as seen in the below image. This will apply to any plugin you download from internet. Also keep in mind to do this to DLLs you trust safe. Otherwise your computer will be unsafe.

image

To troubleshoot plugin and other errors you can refer to Open Live Writer log file located in C:\Users\Arjuna\AppData\Local\OpenLiveWriter\Open Live Writer.log.

Saturday, September 17, 2016

Monitoring Windows Services using PowerShell

Recently I was interested in finding a way to monitor a windows service. What I needed was to check whether the service is running and if not running get a notification and try to restart the service. Following PowerShell script does exactly that, it checks for Microsoft CRM Asynchronous Service and the Microsoft CRM Asynchronous Maintenance Service activity and send 2 emails to Admin and Dev. This needs to be then scheduled using Windows Task Scheduler or SQL Server Job.

### Checking for CRM Async and Maintenance service failure and try restarting, if failing send an email notification.

 

## Function to send mail notification.

function Send_Email ([string]$strEmailSubject, [string]$strEmailBody)

{

       $EmailFrom = "Arjuna@Email.com"

       $EmailTo = "Admin@Email.com, Dev@Email.com"

       $EmailSubject = $strEmailSubject

       $EmailBody = $strEmailBody

       $EmailSMTPServer = "SMTP.server.com"

       ## Creating Mail Message object.

       $SMTPMessage = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo, $EmailSubject, $EmailBody

       ## Enabling HTML mail body.

       $SMTPMessage.IsBodyHtml = $true

       ## Creating SMTP client object.

       $SMTPClient = New-Object System.Net.Mail.SMTPClient $EmailSMTPServer

       ## Sending mail.

       $SMTPClient.Send($SMTPMessage)

       ## Sending mail method 2.

       ##send-mailmessage -from "Arjuna@Email.com" -to "Admin@Email.com, Dev@Email.com" -subject "CRM Async Service Failed" -body "Please check." -smtpserver "SMTP.server.com"

       ## Sending mail method 2 using parameters.

       ##send-mailmessage -from $EmailFrom -to $EmailTo -subject $EmailSubject -body $EmailBody -smtpserver $EmailSMTPServer

}

 

## Function to check the service activity.

function Check_Service

{

       ## Get all services which has a Name like MSCRMAsyncService, Start Mode is Auto and service State is Running.

       $FailedAsyncService = Get-WmiObject Win32_Service | Where-Object {$_.Name -like 'MSCRMAsyncService' -and $_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | Select-Object DisplayName

       ## For Testing.

       ##Write-Host "A: " $FailedAsyncService

 

       ## Get all services which has a Name like MSCRMAsyncService$maintenance, Start Mode is Auto and service State is Running.

       $FailedAsyncMainteService = Get-WmiObject Win32_Service | Where-Object {$_.Name -like 'MSCRMAsyncService$maintenance' -and $_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | Select-Object DisplayName

       ## For Testing.

       ##Write-Host "B: " $FailedAsyncMainteService

 

       ## Checking whether the Async Service has failed.

       if ($FailedAsyncService -ne $NULL)

       {

              ## Trying to start the failed Async Service.

              Start-Service -displayname "Microsoft Dynamics CRM Asynchronous Processing Service"

              ## Service Name can also be used to start the servie.

              ##Start-Service MSCRMAsyncService

              ## Get all services which has a Name like MSCRMAsyncService, Start Mode is Auto and service State is Running.

              $AsyncServiceStarted = Get-WmiObject Win32_Service | Where-Object {$_.Name -like 'MSCRMAsyncService' -and $_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | Select-Object Name

              ## Checking the service to see whether it started.

              if ($AsyncServiceStarted -ne $NULL)

              {

                     ## Calling Send_Mail function to notify.

                     Send_Email ("CRM Async Service Failed.") ("System has detected that the following CRM Async Service has failed. System automatically tried restarting the service but it was unsuccessful. Try manual start. <BR/><BR/>" + $FailedAsyncService)

              }

              else

              {

                     ## Calling Send_Mail function to notify.

                     Send_Email ("CRM Async Service Restarted.") ("System has detected that the following CRM Async Service has failed. System automatically tried restarting the service and it was successful. <BR/><BR/>" + $FailedAsyncService)

              }

       }

 

       ## Checking whether the Async Maintenance Service has failed.

       if ($FailedAsyncMainteService -ne $NULL)

       {

              ## Trying to start the failed Async Maintenance Service.

              Start-Service -displayname "Microsoft Dynamics CRM Asynchronous Processing Service (maintenance)"

              ## Get all services which has a Name like MSCRMAsyncService$maintenance, Start Mode is Auto and service State is Running.

              $AsyncMainteServiceStarted = Get-WmiObject Win32_Service | Where-Object {$_.Name -like 'MSCRMAsyncService$maintenance' -and $_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | Select-Object Name

              ## Checking the service to see whether it started.

              if ($AsyncMainteServiceStarted -ne $NULL)

              {

                     ## Calling Send_Mail function to notify.

                     Send_Email ("CRM Async Service Failed.") ("System has detected that the following CRM Async Service has failed. System automatically tried restarting the service but it was unsuccessful. Try manual start. <BR/><BR/>" + $FailedAsyncMainteService)

              }

              else

              {

                     ## Calling Send_Mail function to notify.

                     Send_Email ("CRM Async Service Restarted.") ("System has detected that the following CRM Async Service has failed. System automatically tried restarting the service and it was successful. <BR/><BR/>" + $FailedAsyncMainteService)

              }

       }

}

 

## Calling the Check_Service function.

Check_Service