September 21, 2015

PowerShell - SessionState

SessionState is an object that reflects the current state of your PowerShell environment. You can find this object in the $ExecutionContext automatic variable:

$executioncontext.SessionState

Drive                         : System.Management.Automation.DriveManagementIntrinsics
Provider                    : System.Management.Automation.CmdletProviderManagementIntrinsics
Path                         : System.Management.Automation.PathIntrinsics
PSVariable                : System.Management.Automation.PSVariableIntrinsics
LanguageMode          : FullLanguage
UseFullLanguageModeInDebugger : False
Scripts                      : {*}
Applications              : {*}
Module                     :
InvokeProvider           : System.Management.Automation.ProviderIntrinsics
InvokeCommand       : System.Management.Automation.CommandInvocationIntrinsics


PSVariable sub-object will retrieve the value of any variable and can also be used to modify variables:
$value = "Test"

Lets retrieve variable contents by using PSVariable:
$executioncontext.SessionState.PSVariable.GetValue("value")
Test

Similarly use Set method to modify variable contents:
$executioncontext.SessionState.PSVariable.Set("value", 100)

$value
100

Remove method will simply remove the variable:
$executioncontext.SessionState.PSVariable.remove("value")
Now if you try to get the contents of variable you will get nothing:
$executioncontext.SessionState.PSVariable.GetValue("value")

You can list down all the available methods in PSVariable object by following cmdlet:
$executioncontext.SessionState.PSVariable | Get-Member -MemberType Methods

Drive subobject lets you manage drives in PowerShell. You could retrieve the current drive by Current property:
$executioncontext.SessionState.Drive.Current

GetAll() lists all available drives and is equivalent to the Get-PSDrive cmdlet:
$executioncontext.SessionState.Drive.GetAll()

September 10, 2015

MS SQL Server - Configure / Send Emails by Database Mail

This post is written by following SQL 2008 R2.

MS SQL Server have the feature to send emails. Let see how we can start using it:

There we have a Stored Prcocedure named msdb.dbo.sp_send_dbmail used to send emails. But if you call this SP straight away like:
EXEC msdb.dbo.sp_send_dbmail  
         @recipients    = 'myID@yahoo.com'
        ,@subject        = 'Test Subject'
        ,@body            = 'Test Body'
        ,@body_format    = 'HTML' 


Enable Database Mail XPs

After running the above stored procedure, you may get this error:
SQL Server blocked access to procedure 'dbo.sp_send_dbmail' of component 'Database Mail XPs' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Database Mail XPs' by using sp_configure. For more information about enabling 'Database Mail XPs', see "Surface Area Configuration" in SQL Server Books Online.

This is because first you have to configure your server for sending emails. To fix this error, you can connect to SQL Server Instance with System Administrator (SA) Privileges and execute this script to enable:
USE MASTER
GO

--Enable 'Show Advanced Options'
SP_CONFIGURE 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO

-- Enable Database Mail XPs Advanced Options in SQL Server */
SP_CONFIGURE 'Database Mail XPs', 1
RECONFIGURE WITH OVERRIDE
GO

--Diable 'Show Advanced Options'
SP_CONFIGURE 'show advanced options', 0
RECONFIGURE WITH OVERRIDE
GO
Now after this if you try to run the same Stored Procedure to send your email, this time you might get this error:
No global profile is configured. Specify a profile name in the @profile_name parameter.

Configure Database Mail Profile Account

If you have not already configured Database Mail Profile Account, follow these steps to create a new one:
  1. Go to SQL Server Object Explorer.
  2. Expand Management Node.
  3. Right click on Database Mail and select Configure Database Mail and proceed with the wizard.
     

    SQL - Database Mail Node

    SQL - Welcome Database Mail Configuration

  4. From Select Configuration Task screen, select Set up Database Mail by performing the following tasks.
    SQL - Select Configuration Task
  5. Put your Profile name and Description, and in SMTP accounts grid, select Add to set up a new SMTP account.
  6. A popup box will appear, click on New Account.
    SQL - Mail - New Profile
  7. Now  provide all details for your SMTP account setup. I have setup with my yahoo account. If you use yahoo mail service, you have to provide following details for yahoo:

Server name: smtp.mail.yahoo.com
Port number: 587
And mark the checkbox for Secure Connection (SSL).

       SQL - Mail - Create Account

       Rest of the things you provide your email id with password.
       Click Next and Finish the wizard.

Now you have setup this new SMTP account, then second thing you have to do is to Enable Database Mail XPs as described below.


Configure a Default or Global Database Mail profile

If you have already configured Database Mail Profile Account then you have to follow these steps:
  1. Go to SQL Server Object Explorer.
  2. Expand Management Node.
  3. Right click on Database Mail and select Configure Database Mail and proceed with the wizard.
  4. From Select Configuration Task screen, select Manage Profile Secrurity.
    SQL - Database Mail - Manage Security
  5. Now select your desired profile (mark CheckBox as Public) and make it Default Profile by select Yes.
    SQL - Database Mail - Manage Profile Security
  6. Click Next and Finish the wizard.

Now you can send emails from SQL Server.

If you find this post helpful or have any suggestion, please post your valuable comments.

September 9, 2015

AngularJs routing not working

I started learning AngularJS, and faced problem that routing is not getting worked. It only displays my index page and I was unable to display my partial views. Even it was not showing any error message while try to trace using firebug.

These are the points you have to consider if you get stuck with Angular Routes not working:

1- While going to define your config for routing, remember to put 'ngRoute' dependency parameter.
For example if following is your code snippet:
var myApp = angular.module('myApp');
{
    $routeProvider
    .when('/view1',
    {
        controller: 'myController',
        templateUrl: 'view1.html'
    })
    //code goes on...
}

You have to change it by putting 'ngRoute' dependency.
var myApp = angular.module('myApp', ['ngRoute']);
{
    $routeProvider
    .when('/view1',
    {
        controller: 'myController',
        templateUrl: 'view1.html'
    })
    //code goes on...
}
2- You may be started Angular JS demos by referencing angular.js library. Note that for routing get worked, we have to reference another library angular-route.js (http://code.angularjs.org/1.2.13/angular-route.js)

3- It should work now. If still not work, try to run your application by hosting in a webserver. For example try run it through Visual Studio

September 1, 2015

PowerShell - ScriptBlock

The ScriptBlock is a special form of command. It can contains multiple lines of code. It is defined by braces. You can put the call operator to execute a scriptblock: 
& { "Get date: " + (get-date) }

The call operator normally runs only a single commands. With script block you can run multiple lines of instructions since scriptblocks can consist of any number of commands.
& {Get-Process | Where-Object { $_.Name -like 'a*'}}

Similarly you can pass the same sort of command block to Invoke-Expression cmdlet, and you waill get the same results.
Invoke-Expression 'Get-Process | Where-Object { $_.Name -like "a*"}'

Just remember to put code after Invoke-Expression in single quotation marks. If you use double quotation marks, PowerShell will replace all the variable names in the string with the variable contents.

A scriptblock can also uses the param statement to define a parameter. You could easily define your own anonymous scriptblock with arguments. The following scriptblock accepts two parameters and multiplies them:
{ param($value1, $value2) $value1 * $value2 }

To invoke the scriptblock, use the call operator:
& { param($value1, $value2) $value1 * $value2 } 10 5