Sort using LINQ to XML

The hobby for the last few days was to “play” with XML and LINQ syntax.

Something that I’ve tried doing was to sort an XML document not using XSL stylesheet but using LINQ. I’ve start by sorting the root elements from the XML documents.

Next step was to sort  deep XNodes. In order to this we need to identify the node:

… then based on the node we just do the sorting

… then just replace the container with the one sorted

Take.(5) means that we are just taking the lsat 5 elemenst from the query.

All the above will be done in a foreach statement:

The full schema based on which the XML was created and the LINQ query applied was looking as the one below:

 

 

Orphaned Snapshots in VMWare

Considering that the snapshot are not listed in VMWare snapshot manager and presuming that already we’ve tried to create a new snapshot ant then delete all and nothing is happening, in order to remove the orphaned snapshots we need:

  • First step is to find all snapshots and delta disks on the datastores: from SSH on the host

    (if you know the VM this is not required)
  • This will list the vmdk’s which  are mounted by VMX file for the affected VM

    (this should be checked also because we don’t know if all disks which resides in the VM folder are used)
  • If you use cat on this vmdk files you should see what underlying file on the datastore they reference under  Extent Description and  Change tracking file

    Then you should check if the vmkd file are locked by any hosts –delta , -ctk and –flat

    (If it was locked by an ESXi host the MAC of the host would be shown in the owner)
  • Then make sure they are not locked by a process, touch them and see that the timestamp updates:

    Being able to touch the file, run vmkfstools -D finding no locks, find no references in vmdk descriptor files generally means it isn’t in active use and is safe to move/remove. (Please be aware this command should be used from other host, not from the one where VM resides )
  • Create a new directory and move the suspect files to it and check for problems with the VM

  • Check the VM state and if it is operating normally delete the directory

 

Netstat and Powershell

 

The correct name should be: how to trace connections and monitor them using PowerShell. The wide used tool to see open connection in and out from a computer is through netstat command from cmd promt. Of course we can use Wireshark to do deep troubleshooting.

How about capturing the output and store them in an array so we can automate the process and do periodic checks to see if we have any errors. Usually the output will display listening and established states but something due to network bottlenecks and/or applications hanging we may get other states.

The script below is capturing the netstat -ano output and split it so we can store it in an array and use it after. This function has few parameters:

All the above parameters are not mandatory so we can execute this function with or without them.

The output will look like:

We can also use parameters:

The above function will be useful in case we want to trace broken connections or we want to see when our computer is generating request on web or trace specific protocols. I’m usual using this to monitor open ports and listen for incoming connection when debugging some applications.

Active Directory health check – DCDIAG friendly view

We can monitor all Domain Controllers events using different monitoring tools like System Center Operation Manager or any other tools from any other 3rd parties. But how about automating a health check of a domain controller and instead of having a text document with all the information inside where we need to look after  errors and warning we can create a user friendly table easy to read and interpret. We just need somehow to run a DCDIAG and then parse the output using Regex and display it.

DCDIAG is as an end-user reporting program, a command-line tool that encapsulates detailed knowledge of how to identify abnormal behavior in the system. Dcdiag displays command output at the command prompt.

What we need now is to create a Powershell function that takes the output from dcdiag, analyze the output and then present it in a easy readable format.

The function will be:

There is just one mandatory parameter which is -domain. Also this function has other non-mandatory parameters like: -namingconvention and -tmpfile. Domain was set as mandatory parameter in order to be able to run this function on multiple domains in one go. We can set it as Mandatory=$false and replace the null value with $env:userdomain. This variable will pick up the domain from where you run the command.

In order to have a easy readable output I’ve used a regex syntax, Regex stands for regular expression.

All about Regular expresion can be found here:https://en.wikipedia.org/wiki/Regular_expression

For all the servers we will get an output like:

Based on the above output we can create a table and read it easily. Also we can colour the output so in case some test failed we can spot it quickly.

dcdiag2

Powershell & C# Notifyicon

Some time ago I’ve managed to create a tool to show me in real time when a new high priority incident is being logged and when one of the priority incidents was closed. In the background the tool was querying Service Now and extracting some XML file which then were stored in memory and displayed in a grid. To make some room on my desktop, the tool was configured to run in the background and display small notification balloons in the corned with what’s changed in the database.

Based on the above, I’ve tried replicating the same in Powershell. This may help when running time consuming scripts and  when we want periodic notification regarding the status of the script.

In C# the code is pretty simple:

The result:

CsharpCapturePNG

 

In PowerShell in order to use a Notifyicon we will need to create an object type System.Windows.Forms.NotifyIcon. After that, the code is almost as the C# one.

The full function will be:

We can then use this function in a time consuming script and when we need something to be displayed we will append this function. The mandatory parameters are -text and -title

The output:

notifyIcon

Management – Different methodologies

Do you want to be successful in your business? Are you applying any methodologies in your day to day work?

The tools and techniques we’ve all been using have been only partially successful. Activities or tasks are still experience significant problems like poor quality, large backlogs, missed deadlines and overruns, inadequate management, inaccurate estimating methods, and an unacceptable level of delivered task.

Many IT managers have all but surrendered, but IT companies should instead begin evaluating maturing methodologies and wide related tools; good planning simply makes activities of any kind more productive. Development organizations should embrace and implement integrated methodologies now widely available, but little used or even understood.

There are numerous methodologies on the market as Kanban (a former inventory and control system used by Toyota which creates a wide overview about the input and output of task), Scrum (which is widely used in software development as a strategy framework), ITIL (a set of practices for IT service management with the role of aligning IT services with the needs of businesses), Agile (used in project management for optimization), Six Sigma ( a data-driven approach for eliminating defects or errors in any process).

All the methodologies are just frameworks and can be applied to all domain activities from manufactures to sales. The below table will just highlight some of the key items that we need to accomplish in order to be more successful.

scrum3