About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Thursday, February 28, 2008

Finding all variables and functions in Powershell

To get the list of all the variables or functions that have been declared and pre-existing:

ls variable:*

ls function:*

Note: You can change the wildcard(*) if you are looking for something more specific.

ls can obviously be substituted with dir or get-childitem if you are more familiar with those key-words.

Something Interesting:

If you want to look more closely at what the function is actually doing then you just need to type: $function:<name of function> like so:

$function:mkdir

which will return:

param([string] $name) New-Item $name -type directory

Monday, February 18, 2008

Lights Out Game (XAML)

I figured I should start learning Xaml, since it is used in WPF and Silverlight. I found this nice tutorial on Code Project: http://www.codeproject.com/KB/silverlight/SilverlightsOut.aspx on how to make a game as a introduction to silverlight.

I however first decided to make it a WPF application to try my hand at that first. One of the first things I learned was about the error:

The calling thread cannot access this object because a different thread owns it.

In the original he had it setup to use the HTMLtimer use the Tick event to move the background of stars like so:

System.Windows.Browser.HtmlTimer timer =

   new System.Windows.Browser.HtmlTimer();

timer.Interval = 1;

timer.Enabled = true;

timer.Tick += new EventHandler(timer_Tick);

 

I figured that I would need to change this to use the Timer class:

Timer timer_error = new Timer();

timer_error.Interval = 1.0;

timer_error.Enabled = true;

timer_error.Start();

timer_error.Elapsed += new ElapsedEventHandler(timer_error_Elapsed);

Then I got the error, which I then found:

"The .NET Framework 2.0 supports several timer objects, among them System.Timers.Timer, System.Threading.Timer, and System.Windows.Forms.Timer. However, these timers cannot be used directly by a WPF application since they run in a different thread than the one running the WPF UI."

(source: http://blogs.msdn.com/wpfsdk/archive/2006/06/23/Animating-Traffic-Map-Image-Data.aspx)

The solution is to use the System.Windows.Threading.DispatcherTimer like so:

System.Windows.Threading.DispatcherTimer timer  = new DispatcherTimer();  

timer.Interval = new TimeSpan(0,0,0,0,85);

timer.Start();

timer.Tick += new EventHandler(timer_Tick);

 

I also had to change little things through out the program to get it to work, such as:

void timer_Tick(object sender, EventArgs e)

{

   double currentLeft = (double)background.GetValue(Canvas.LeftProperty);

   // get current left position of background

   if (currentLeft <= 0)

   {

      // move background pixels over

      background.SetValue(Canvas.LeftProperty, currentLeft + 2);

   }

   else

   {

     // reset backgrounds position

     background.SetValue(Canvas.LeftProperty, currentLeft - 340); 

     //instead of

    background.SetValue(Canvas.LeftProperty, -340);

   }

}

 

I still have lots to learn. Currently I'm still adding more functionality to the app. I'm thinking that I should add a "restart" and make a auto-solve.

Technorati Tags: ,,,

Thursday, February 07, 2008

Signing your PowerShell script

I got an error "File ... cannot be loaded. The file ... is not digitally signed. The script will not execute on the system. Please see "get-help about_signing" for more details.." when starting PowerShell today, after installing the new Windows SDK.

So, I scrambled to find a solution on how to make a certificate and sign it to a PowerShell script. On Scott Hanselman's Blog: http://www.hanselman.com/blog/SigningPowerShellScripts.aspx 
he shows a easy to follow step by step on how to do so.

 

P.S. 

I also had to rest my Home directory.

Tuesday, February 05, 2008

Running Powershell in C#

Found this interesting article on Code Project the other day, on how to run PowerShell scripts from within C#.

http://www.codeproject.com/KB/cs/HowToRunPowerShell.aspx

Prerequisites:

Code:

Add a reference System.Management.Automation from C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0

Import

using System.Management.Automation;

using System.Collections.ObjectModel;

using System.Management.Automation.Runspaces;

 

Note: I did have to edit his function, to get it to work for me, and also added a try/catch to spew out the error.

private string RunScript(string scriptText)

{

    // create Powershell runspace

    Runspace runspace = RunspaceFactory.CreateRunspace();

 

    // open it

    runspace.Open();

 

   // create a pipeline and feed it the script text

   Pipeline pipeline = runspace.CreatePipeline();

   pipeline.Commands.AddScript(scriptText);

 

   // add an extra command to transform the script output objects into nicely formatted strings

   // remove this line to get the actual objects that the script returns. For example, the script

   // "Get-Process" returns a collection of System.Diagnostics.Process instances.

   pipeline.Commands.Add("Out-String");

 

   // execute the script

   Collection<PSObject> results = new Collection<PSObject>();

   try

   {

      results = pipeline.Invoke();

   }

   catch (Exception ex)

   {

      results.Add(new PSObject((object)ex.Message));

   }

 

 

   // close the runspace

   runspace.Close();

 

   // convert the script result into a single string

   StringBuilder stringBuilder = new StringBuilder();

   foreach (PSObject obj in results)

   {

       stringBuilder.AppendLine(obj.ToString());

   }

 

   return stringBuilder.ToString();

}

 

Technorati Tags: ,,,