Query Plans In The Race Of Extended Events

In the previous articles we took a look at some simple ways to hunt for those valuable query plans and then at how we can accomplish the same using SQL Trace. There is yet another way of discovering and extracting query plans from an in-use SQL Server and that is to use Extended Events.

Continue reading “Query Plans In The Race Of Extended Events”

How To Join Object Lists In PowerShell

Question

So I have these two arrays of data objects in my PowerShell code:

$Customers = (
    @{ Id = 1; Name = "Customer 1" },
    @{ Id = 2; Name = "Customer 2"; },
    @{ Id = 3; Name = "Customer 3"; }
);

$Orders = (

    @{ Id = 1; CustomerId = 1; Product = "Red Pumpkin" },
    @{ Id = 2; CustomerId = 1; Product = "Blue Pumpkin" },
    @{ Id = 3; CustomerId = 2; Product = "Green Pumpkin" }
);

How can I mimic a SQL-like JOIN on these two lists and output a new list with the joined data?

Short Answer

Looping and New-Object are your friends:

# loop all customers
foreach ($c in $Customers)
{
    # loop al orders
    foreach ($o in $Orders)
    {
        # decide whether to join
        if ($o.CustomerId -eq $c.Id)
        {
            # output a new object with the join result
            New-Object PSObject -Property @{
                CustomerId = $c.Id;
                OrderId = $o.Id;
                CustomerName = $c.Name;
                OrderProduct = $o.Product;
            };
        }
    }
}

What would be really cool though would be to have a CmdLet that would do this work for us dynamically whilst allowing us to pipe data through, wouldn’t it?

Continue reading “How To Join Object Lists In PowerShell”

How To Access .NET Property Types Dynamically In PowerShell

Question

So I have this given list of objects. I don’t know their structure beforehand. I want to be able to access their property values and underlying data types to accomplish some other task.

Here is some dummy data as an example:

$DataList = (
    @{ Id = 1; Name = "Red Pumpkin" },
    @{ Id = 2; Name = "Green Pumpkin" },
    @{ Id = 3; Name = "Blue Pumpkin" }
) | %{ New-Object PSObject -Property $_ };

How do I do this?

Continue reading “How To Access .NET Property Types Dynamically In PowerShell”

How To Find And Remove Old Files In PowerShell

Question

So I have all these files in my backup folder. How can I keep only the files modified at most seven days ago, whilst removing the rest?

Short Answer

We can use a simple chain like this:

Get-ChildItem -Recurse |

    # change the extension as appropriate
    ?{ $_.Extension -eq ".bak" } |

    # change the time window as appropriate
    ?{ $_.LastWriteTime -lt (Get-Date).AddDays(-7) } |

    # now remove the results
    Remove-Item

We can then add or remove filters to this pattern as appropriate to your scenario.

So what more stuff can we add?

Continue reading “How To Find And Remove Old Files In PowerShell”

C#: Why Are My Random Numbers Not Random?

Question

So I have this code, which is supposed to generate a new random number every time it is called:

        public static int GetSomeRandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

In my program, I’m using the above method to let me select five pumpkin colors at random (just don’t ask why):

        enum PumpkinColor
        {
            Red = 0,
            Green = 1,
            Blue = 2
        }

        static void Main(string[] args)
        {
            // write five random pumpkin colors
            for (int i = 0; i < 5; ++i)
            {
                PumpkinColor color = (PumpkinColor)GetSomeRandomNumber(0, 3);
                Console.WriteLine(color);
            }
        }

However, when I run my program, this is the output:

Blue
Blue
Blue
Blue
Blue

Blimey. Not very random now, is it? What gives?

Short Answer

Don’t use a local Random instance in quick loops. Use a shared instance instead:

        private static Random random = new Random();

        public static int GetSomeRandomNumber(int min, int max)
        {
            return random.Next(min, max);
        }

This new implementation now returns:

Green
Red
Red
Blue
Blue

But why does this happen?
And what care do I need to have with static implementations?

Continue reading “C#: Why Are My Random Numbers Not Random?”