Thursday, December 31, 2015

Domain join a vm to DC/AD in Azure


Refer to Install AD Forest + DC in Azure Only in ARM (aka v2) in 30 min how to set up the primary DC/AD.

There are two ways that you can add a vm to a domain:

1.       Join the domain while provisioning the vm

          "properties": {

            "publisher": "Microsoft.Powershell",

            "type": "DSC",

            "typeHandlerVersion": "2.8",

            "settings": {

              "ModulesUrl": "[concat(parameters('assetLocation'),'Configuration.zip')]",

              "ConfigurationFunction": "Configuration.ps1\\DomainJoin",

              "Properties": {

                "DomainName": "[parameters('domainName')]",

                "AdminCreds": {

                  "UserName": "[parameters('adminUsername')]",

                  "Password": "PrivateSettingsRef:adminPassword"

                }

              }

            },

            "protectedSettings": {

              "Items": {

                "adminPassword": "[parameters('adminPassword')]"

              }

            }

          }

        }



2.       Spin a vm and then domain join


b.       Manually J open up a computer management and it to the domain.



Potential problem:

If the vm doesn’t seem the DNS in approach 1:

1.)    Add the DNS to the vnet

2.)    If still doesn’t solve the problem, fall back to approach #2.

Monday, December 28, 2015

Send messages using AMQP in Java in Azure Event Hub and use Azure Stream Analytics to read and save in Azure Storage


Steps:

1. Create, and configure, and write code to send messages using Azure Event Hub:


Sample json message:

{
   "time":"12-27-2015:23:53:372 -0800",
   "code":"1984567839209"
}

2. Create Azure Storage and container

Create SA in classic aka v1 portal. For easy on-screen configuration. You could also try with v2. Either way it works great.

3. Create Azure Stream Analytics job, use the above SA created above.

Follow the steps mentioned in the href in the Reference section.

4. Configure Azure Stream Analytics with input as Event Hub and Azure Storage as output. Configure a query.

Follow the steps mentioned in the href in the Reference section.

Start with a sample query:
SELECT
*
INTO
[<sa account/output name>]
FROM
[<event hub>]

Please note that the square brackets remain. Replace all of the angle bracket along with your configuration.

5. Start the job

Follow the steps mentioned in the href in the Reference section.

6. Send messages (json with UTF-8)

Follow the href provided above in step 1.

7. Verify that the message has landed in storage blob container

Whichever tool you have, validate that the content has been pushed by ASA to SA blob container.


8. Check stats of Azure Stream Analytics job – storage account


9. Check stats of Azure Stream Analytics job - metrics

Check it under Monitor section.

10. Fix errors

e.g. if the json format is bad, etc. the message should be ample clear J

Act accordingly. E.g. test out the input using ASA test tool.

Stop the job. Fix the error. Start the job.



Sunday, December 27, 2015

Send/Receive messages using AMQP in Java in Azure Event Hub

Steps:
1. Create Event Hub
2. Configure Event Hub with Send/Receive policy (this step will provide the keys to connect)
3. Encode the key
4. Ensure to use proper partition
5. Ensure to use the proper Consumer group in the Receiver.
6. AMQP libraries
7. Sender code
8. Receiver code

Create Event Hub:

Configure Event Hub with rules:

Encode the keys:
Collect the keys from EventHub configuration from the above steps.
            String myKey = "myKey=";
            URLEncoder.encode(myKey);

Ensure to use proper partition:
This is an important step. This is unlike other AMQP implementation. Event hub creates partitions for parallel processing, ensure that your sender and receiver uses the correct partitions. The properties could be specified by properties file.
The partition information is highlighted in the servicebus.properties files below.
Tips:
1.       Partition while sending
Your connection to the Queue will go through.
2.       Partition while receiving
Your default connection will not go through and you will receive error.
Exception in thread "main" javax.jms.JMSException: Invalid EventHub address. It must be either of the following. Sender: <EventHubName>. Partition Sender: <EventHubName>/Partitions/<PartitionNumber>. Partition Receiver: <EventHubName>/ConsumerGroups/<ConsumerGroupName>/Partitions/<PartitionNumber>. TrackingId:59faccff54a742ba8eabf5a9d07ecb4b_G8,TimeStamp:12/28/2015 3:18:51 AM

Ensure to use the proper Consumer group in the Receiver:
Highlighted below in servicebus.properties in Receiver below.

AMQP libraries:
            <dependency>
                  <groupId>org.apache.qpid</groupId>
                  <artifactId>qpid-amqp-1-0-common</artifactId>
                  <version>0.32</version>
            </dependency>
            <dependency>
                  <groupId>org.apache.qpid</groupId>
                  <artifactId>qpid-amqp-1-0-client</artifactId>
                  <version>0.32</version>
            </dependency>
            <dependency>
                  <groupId>org.apache.qpid</groupId>
                  <artifactId>qpid-amqp-1-0-client-jms</artifactId>
                  <version>0.32</version>
            </dependency>
            <dependency>
                  <groupId>org.apache.geronimo.specs</groupId>
                  <artifactId>geronimo-jms_1.1_spec</artifactId>
                  <version>1.1.1</version>
            </dependency>

Sender code:
·         servicebus.properties:
connectionfactory.SBCF = amqps://SendRule(your policy):<<url encoded value>>@<your name space>.servicebus.windows.net/?sync-publish=false
queue.EventHub = <<your event hub>>/Partitions/0

e.g.
connectionfactory.SBCF = amqps://SendRule:nju%asadfasdf@xyz-ns.servicebus.windows.net/?sync-publish=false
queue.EventHub = xyz/Partitions/0

·         Initialize connection factory:
            // http://people.apache.org/~rgodfrey/qpid-java-amqp-1-0-client-jms.html
            // Configure JNDI environment
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY,
                        "org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory");
            env.put(Context.PROVIDER_URL, "servicebus.properties");
            Context context = new InitialContext(env);

            ConnectionFactory cf = (ConnectionFactory) context.lookup("SBCF");

            Destination queue = (Destination) context.lookup("EventHub");

            // Create Connection
            Connection connection = cf.createConnection();
·         Create a session:
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
·         Create Message Producer:
            sender = session.createProducer(queue);
·         Send Message:
            BytesMessage message = session.createBytesMessage();
            message.writeBytes(messageCode.getBytes("UTF-8"));
            sender.send(message);

Receiver code:
·         servicebus.properties:
connectionfactory.SBCFR = amqps://ReceiveRule:<<your encoded key>>@xyz-ns.servicebus.windows.net
queue.EventHub = xyz/ConsumerGroups/$Default/Partitions/0

·         Initialize connection factory:
Same as above
·         Create a session:
Same as above
·         Create Message Producer:
            consumer = session.createConsumer(queue);
·         Receive Message:
o   Implement a listener
class Listener implements MessageListener {
      @Override
      public void onMessage(Message arg0) {
o   Read message
            int messageLength =  (int) message.getBodyLength();
            if (messageLength != 0) {
                  byte[] readBytes = new byte[messageLength];
                  message.readBytes(readBytes);
                  return new String(readBytes);
            }
o   Register the listener
            Listener myListener = new Listener();
            consumer.setMessageListener(myListener);
o   Important: Start the connection factory
            connection.start();





References:

Thursday, December 24, 2015

Check if there is already a login session in Azure Powershell.

function Check-Session () {
    $Error.Clear()

    #if context already exist
    Get-AzureRmContext -ErrorAction Continue
    foreach ($eacherror in $Error) {
        if ($eacherror.Exception.ToString() -like "*Run Login-AzureRmAccount to login.*") {
            Login-AzureRmAccount
        }
    }

    $Error.Clear();
}

#check if session exists, if not then prompt for login
Check-Session


Tuesday, December 22, 2015

Find VM status of all VMs in Azure; Start/Stop VMs in bulk (in ARM)

Get-AzureRmSubscription | Where-Object {$_.SubscriptionId -like "*499"} | Select-AzureRmSubscription

#improvise the script per your need

#select your subscription or iterate through subscriptions if desired

$action="stop"
#$action="start"

$excludeStartVMs = @("winweb1hdicp", "b", "c")
$includeStartVMs = @("MyWindowsVM", "e")

$excludeStopVMs = @("1", "2", "3")
$includeStopVMs = @("4", "5")


function Take-Action($vmname, $action) {
    if ($action -eq "stop") {
        return Generic-Action $vmname $excludeStopVMs, $includeStopVMs
    }
    if ($action -eq "start") {
        return Generic-Action $vmname $excludeStartVMs $includeStartVMs
    }
    return $true;
}

function Generic-Action($vmname, $excludeArray, $includeArray) {
    #iterate through exclusion
    foreach ($exclusionVM in $excludeArray) {
        if ($exclusionVM -eq $vmname) {
            return $false
        }
    }
    #iterate through inclusion
    foreach ($exclusionVM in $includeArray) {
        if ($exclusionVM -eq $vmname) {
            return $true
        }
    }

    #or else...
    return $true;
}

#to handle situation where a vm is in weired situation, you dont want to delete it to troubleshoot, but the operations kills your time
function Include-VM($vmname) {
    foreach ($exclusionVM in ($excludeStartVMs + $excludeStopVMs)) {
        if ($exclusionVM -eq $vmname) {
            return $false
        }       
    }
    return $true
}

#select your subscription or iterate through subscriptions if desired
$sw = [Diagnostics.Stopwatch]::StartNew()
foreach ($rg in Get-AzureRmResourceGroup)
{
    foreach($vm in Get-AzureRmVM -ResourceGroupName $rg.ResourceGroupName){
        Write-Host $rg.ResourceGroupName  $vm.Name
        if (Include-VM $vm.Name) {
            #get status
            $status = ((Get-AzureRmVM -ResourceGroupName $rg.ResourceGroupName -Name $vm.Name -Status).Statuses).DisplayStatus
            Write-Host $status
           
            if ($action -eq "stop") {
                if ($status -like ("*running*")) {
                    if (Take-Action $vm.Name $action) {
                        Write-Host "Going to stop ..." $rg.ResourceGroupName $vm.Name
                        Stop-AzureRmVM -ResourceGroupName $rg.ResourceGroupName -Name $vm.Name

                        #doesnt prompt for confirmation...(for non-interactive/batch mode)
                        #Stop-AzureRmVM -ResourceGroupName -Force $rg.ResourceGroupName -Name $vm.Name
                        Write-Host "Stopped:" $rg.ResourceGroupName $vm.Name
                    }
                }
            }#end of stop action
            if ($action -eq "start") {
                if ($status -like ("*Stopped*") -or $status -like ("*deallocated*")) {
                    if (Take-Action $vm.Name $action) {
                        Write-Host "Going to start ..." $rg.ResourceGroupName $vm.Name
                        $confirmStatus=Read-Host 'Are you sure you want to start the VM Yes(1)/No(2) followed by <Enter> [default No(2)]'
                        if ($confirmStatus -eq $null -or $confirmStatus.Length -eq 0) {
                            $confirmStatus="2"
                        }
                        if ($confirmStatus -eq "1") {
                            Start-AzureRmVM -ResourceGroupName $rg.ResourceGroupName -Name $vm.Name
                            Write-Host "Started:" $rg.ResourceGroupName $vm.Name
                        }

                    }
                }
            }#end of start
        } #end of include vm

    }
 }
$sw.Stop()
$sw.Elapsed