Powerfull like PowerShell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework.

Scripting and Task automation

There are some tasks that you do on a computer that are repetitive and boring. Best solution is to automate those common tasks.

How to apply a theme on your GUI?

Some tips on how to apply a theme on your GUI ...

Tuesday, 2 August 2016

Saturday, 11 June 2016

USMT - Offline Migration


          This will be a new topic in which I will not talk about powershell. This time I’m going to talk about USMT.
What’s USMT?
USMT (User State Migration Tool) is a tool that enables you to gather user’s data from a computer and migrate them to a new fresh computer equipped with windows 7 or higher version.

There exist two kind of migration possible:
 - Offline Migration
 - Online Migration

Share:

Friday, 3 June 2016

Powershell_WPF - Part III - How to load a splash screen


What is a splash screen?
A splash screen is a graphical control element consisting of window containing an image, a logo and the current version of the software. A splash screen usually appears while a game or program is launching. According to Wikipedia J

That says everything there is to say. There’s nothing more to add I think.
More specifically, it's something like we see below:



What has that to do with us?

Well, you certainly had spent your time in scripting and writing tons of code in powershell and you found it funny. But at a certain point, you probably realize that the longer your script is, the more time it takes to load.
And before your computer, when you load your script, you’re like:


To sum up, if your application is too big, it will take time before your entire GUI is loaded and and you don't really want your user to wonder "hey! what's going on?". That’s where splash screen plays its role.

The idea of a splash screen came to me when I was asked to create a tool that wrapped ImageX for creating a WIM image. I realized that my tool took too much time to load and I needed something to keep the user busy.

How do we do it in Powershell?

The tips is: runspace

We are just going to use a runspace to load a second form before loading your code and close it when it’s finished. That is the big idea behind. You may be saying: what the heck is he talking about?
I only have a shallow knowledge about runspace too but, all I can say is that it’s really helpful when you want to do multi-threading (to have many processes working at the same time - in noobs way of speaking J).

In my previous post how to apply a theme in Powershell, I introduced Mahapps which uses a metro theme. We are going to use one of the component included in that theme for creating the splash screen. I think you’ve guessed it already: it’s for the progressbar in the splash screen.

Let’s move on to the code.

How to use the splash screen?

You just have to copy and paste the following code in your main ps1 file at the top. This way the runspace which contains the second form will be loaded before everything and can be called at any time inside the script.


If you want to add your logo and text, just edit:
$hash.Logo.Source=".\form\resources\powershel64x64.png"
$hash.LoadingLabel.Content= "Your Code is loading"  

As you can see, there are two functions:
- Start-splashScreen
- Close-splashScreen

Those functions are used to open and close the runspace.
You just have to put Start-splashScreen before the script block

Start-SplashScreen

try {

########################
# your incredible code
########################

}
catch{
    $ErrorMessage = $_.Exception.Message
    Write-Host $ErrorMessage
}

It’s better to use a try and catch if your code potentially generates error. So the splash screen will be closed in any case at the end.

And at the end of your file, before you call ShowDialog() on your form, or when the code you wanted to be executed is finished, close the splash screen.

close-SplashScreen
$Form.ShowDialog() | Out-Null

Of course, as you could understand, you can use a splash screen for many purposes. Let your imagination take you away.

Here is a little preview:


You can download all files here
And that’s it for this third part, hope you enjoyed it. J And see you in the next part

Share:

Monday, 30 May 2016

Powershell_WPF - Part II - How to apply a theme



      In this post I’m going to explain how to apply a theme on your GUI.There are many themes available on the web that you can apply on your WPF form. However, it’s solely explained for C# language use. In our case, we need it for powershell so how do we do it?
Don’t worry anymore! We are going to fix this. J I’ll show you how to integrate two kind of themes in this article.

  • First is based on the use of XAML resources
  • Second is based on dll resources assembly
For the first one, there is an interesting pack of theme from stanislawswierc that you can find here: https://www.nuget.org/packages/Wpf.Themes/ 

And for the second, it’s a project started by Paul Jenkins which uses a Metro theme. You can find it here:  http://mahapps.com/ . To use its full potential you need to have .NET framework 4.5 installed on your computer.

One thing I haven’t introduced in the first Part of this series is the use of StackpanelThe StackPanel is a simple and useful layout panel. As its name imply, It stacks its child elements below or beside each other, depending on the orientation. That coupled with HorizontalAlignement and VertcalAlignment properties; it becomes easy to handle elements.
You don’t have to specify explicit value for element’s position, it will automatically fits the space available. It will become very handy when we will create dynamic GUI later.

I’m going to use the same ps1 file as in the first article "How to create a GUI" ,and here's the GUI that I'm going to use:


Let’s begin!

Part 1: XAML resource

First, How to get the theme?
Launch visual studio, create a new WPF project and open the package manager console
Tools > Nuget package Manager > Package Manager Console

And copy this line into it:

PM> Install-Package Wpf.Themes

Now you should have something like this


      And you can see in the "Solution Explorer" window at your right that a new folder Themes containing different XAML files has been added in the treeview. 


For now, we are on the right track. Now, Browse to your project’s files location and copy the "Themes"  folder that has just been created to the location of the xaml and ps1 you want to apply the theme. At this point, you should have something like these in your project folder:
Now, that we have everything we need, we can edit our xaml. Before, editing your xaml, make sure that your form appears normally and that no error pops up of the powershell console.
In your xaml file, right after the Window tag add the following lines:

Those lines are used to tell that some external resources will be used in the form.

As you can see here, we’re using the Bureau Blue theme located in the folder Themes as resources. And that’s all you needed to apply the theme. Fast and easy!

Here are some previews of the themes provided:

                                                                                   


Part 2: Dll resources

Now let’s move on, here we are going to use dll resource for our theme. This is the theme I like the most: Mahapps.

Again, open visual studio and create a new WPF project, and load the package manager console: Tools > Nuget package Manager > Package Manager Console

And copy this line into it:

PM> Install-Package MahApps.Metro

To install this package, you should have at least version 4.0 .NETframework. To change the version of framework of your project, double clisk on Settings in the Solution Explorer; a new window will appear and change the target framework to 4.0. If you chose, higher version of framework when creating the project, you can skip this step.


You should have this message in your package Manager console after executing the package installation.


Browse to where your Visual studio project is saved on your disc and open this sub-folder:

.\packages\MahApps.Metro.1.2.4.0\lib\net40

You should see these files:
Copy MahApps.Metro.dll and System.Windows.Interactivity.dllCreate a new folder in where your ps1 file and xaml is and rename it to “Assembly”. Paste all the *.dll in this new folder.

Edit your Ps1 file and paste the following lines at the top: 

Also, edit this line if you have renamed your xaml file:

In my case, I renamed it into “window.metro.xaml”.

In your XAML, replace:

With

Don't forget to do as well with the "window" closing tag.  

After the window tag paste these lines:

If you are unlucky, you’ll have this error J when you try to execute your scripts:

Could Not Load Assembly: Operation not supported (Exception from HRESULT: 0x80131515)

Don't worry! This is a normal issue. The dll file is just blocked, this sometimes happens if the file comes from another computer.

To solve the issue: For both files, you need to open the file properties > go to general Tab > Security > check unblock and apply.



If you launch it again you should have something like this:


It's beautiful don't you think? I suggest you to go take look at their website, to see what you can do with other controls.

You can download all files used in this post here.

That ends this little topic. If you have any question, you can leave a reply or send me an e-mail, I'll answer as soon as I can. Thanks for reading and see you in the next part. J

Share:

Powershell_WPF - Part I - How to create a GUI



This is the first part of a series of tutorial that I’m going to write about powershell and WPF. How to create a GUI, apply a theme, manage event and more subjects... 
In this first post, I’m going to explain how to create a simple GUI using powershell and xaml. 

You can create a GUI in powershell in two different ways.
  • First is using the System.Windows.Forms
  • And second is System.Windows.Controls 
In this tutorial, I will use the latter one. 
Why? 
It’s because, writing and editing with the first one is really a burden (not to say a pain in the ***J). You need to place each element one by one and later if you need to modify the interface, you will be in trouble because you’ll be forced to touch all the other elements as well. Quite troublesome isn’t? I advise you not to dive in for your GUI in powershell.

Enough talking, let’s begin:

What do you need?
  • Powershell ISE: if you begin in powershell. It’s installed by default in your computer. Just type Powershell ISE from the Start Menu and launch it.
  • Visual studio: I used visual studio Community 2015 for this article. You can download it on Microsoft website.
  • Notepad++ or a simple text editor with syntax highlighting features.

Step 1

Open visual studio and create a new project. You should see this window appear:


1. Select Templates > visual C# in the left column 
2. Select WPF Application in the middle window 
3. Leave the rest with default value and click OK.


Visual Studio is going to do his stuff and then you’ll have this new window:



It’s here that we’re going to add our element and edit our GUI. Pull down the ToolBox menu at your left, you will see all commonly used WPF controls listed such as TextBox, Label, Image, Button …

Add all elements you need to the Window in the middle and design every element to your taste. I placed basic elements on mine: an Image, a TextBox and a Label.


When you are satisfied with the result, we can go to the next step.

Step 2

You’ll see that you have a window called XAML like shown in the picture below. It’s there that we’re going to get our XAML code. Copy its content to a new text file (*.txt) and change the file extension to XAML (*.xaml).


You can now close visual Studio as we don’t need it anymore. Open the new XAML file that we have just created in Notepad++, and delete the emphasized lines



So that we only have these lines left:



Now Create a new text file and save it with a *.ps1 extension. We should have two files in our directory now. The xaml we just edited (I called MainWindow.xaml) and a Powershell file (I called MainForm.ps1)


Right click on MainForm.ps1 and select "Edit", this will open Powershell ISE. After that, paste these few lines:

Save the file and then launch your code:

Our window is quite ugly for now. Let’s make some customization: add some picture, change color , … J

Open your XamlFile in your favorite text editor and, proceed like this:

Add Background="Cornflowerblue" in Window tag
This will fill the background form with a type of blue color

Add Source=".\dev4sys.png" in the Image tag
Note that ”.\” means the file is in the current directory. So if your picture is in child folder called “toto”; you need to write “.\toto\mypicture.ext”.

Edit Text="This is the second textbox" in TextBox tag
It will change the TextBox content with "This is second a textBox".
Important: Parameters in Xaml are case sensitive: If you write "text" instead of "Text", it will not work, so pay attention in your tags when editing.

All those modifications can be done from within visual studio but it’s better to go through this if you want to have the hang of it. So, here is the new output:


Well, it’s still ugly I admit but not as much as before.

This ends this first part, I hope you liked it. Next time I’m going to show you how to apply a theme on our GUI. Thanks for reading and see you soon.

Share:

Popular Posts

Author

STATS

Contact Form

Name

Email *

Message *