Archives de catégorie : Fun

Integrating Unity 3D with Powershell

This is a project I’ve been working on for the past few weeks, and I thought of publishing an article about it in English for a wider audience since I believe it’s quite « different » from the Powershell articles I usually write.

In this article we will look into integrating Unity with Powershell for creating a multi-purpose monitoring system. This system can be used with SCOM to display information about servers and their status such as Powershell DSC configuration drifts, Active Directory replication, etc.

So, what’s Unity?

Unity is super cool! It’s a 3D engine and an integrated development environment for building games. It supports Javascript and C# and can be used for creating applications for a variety of platforms such as Windows, Unix, Mac OS, Android, IOS, and a bunch of consoles.

It’s quite easy to learn Unity, and the on-line documentation, forums, and Youtube is full of tutorials and examples. One of the tutorials shows you how to create a full asteroids game in less than 2 hours. Which is pretty amazing.

Unity provides a number of namespaces for C#, these can be used to manipulate objects such as providing behavior, animations, effects. Its user interface is quite complete, and object manipulation is quite intuitive.

Unity does a lot for us, it hides [most] of the horrendous math required for implementing physics, and animations, and allows us to focus more on what we want to achieve. The asset store is great, and a lot of the reusable components are free, or have a free version. This means that we can build something quite fast without having to reinvent the wheel all the time.

I wrote PSUnity with just free components from the Asset store, from various 3D Models on the Web, as well as models found on the NASA website.

Why integrate Powershell with Unity?

Unity provides a visually compelling interface, it allows displaying amazing graphics and lets you decide what objects you display and how you display them, you can define how objects interact with other objects, their characteristics, their physics. Pretty much anything goes and the only limitation is really your imagination.

Powershell can interface with pretty much anything and can retrieve all sort of information.

Driving Unity from Powershell makes sense as it provides a lot of flexibility in how the data is retrieved and updated in the PSUnity environment.

For example, we could use Powershell to connect to SCOM and retrieve the status of monitored systems. These monitored systems can be of any type, it’s then up to you to decide what and how you represent your findings within Unity. As we will see I chose a number of scenarios to display different kinds of objects.

It’s true that since Unity supports C#, I could have used .NET and C# to retrieve information about servers and display it. But then only Devs would have used PSUnity, and I really want IT pros to use it. IT pros use Powershell, not C#.

 

Structure of PSUnity

The following image depicts how PSUnity is structured:

PSUnity

The PSUnity server can be accessed by Powershell cmdlets for creating and updating objects that will be displayed in PSUnity. Powershell scripts can interface to a variety of remote solutions to retrieve information such as, SCOM, Powershell DSC, ADSIPS, and others such as SSH (I use it to connect to a remote EMC Isilon NAS cluster and retrieve status information).

The PSUnity client connects to a PSUnity server via TCP/IP and pulls the latest information on a regular basis, and Unity does the rest.

PSUnity cmdlets

PSUnity comes with a number of cmdlets which are used for updating the PSUnity server. This part is still work in progress, and new cmdlets are created for new needs., so watch this space on GitHub.

  • New-PSUnitySession: creates a new PSUnity session. Once executed, Powershell is connected to PSUnity.
  • Remove-PSUnitySession: closes the PSUnity session
  • Set-PSUnityServer: creates or modifies a server object in PSUnity. Attributes are name, location, role, status (value between 0-4), description, position (x, y in 2D coordinates), rotation. If the server object does not exist, it is created. If the object exists it is updated. Objects in PSUnity are uniquely identified by their name attribute. The status defines the behavior of the server object. If the value of status is 2, there are sparks on the keyboard. 3 means smoke comes out of the server, and if it’s 4, the server is on fire. The color of the name of the server reflects the status too: Green, Blue, Yellow, Orange, Red. Servers are visible only in the Servers view.
  • Set-PSUnitySite: creates or modifies a platform that can be used to group servers. Sites are visible in the Servers view. A Site has a label that displays its name and location.
  • Set-PSUnityCity: Cities are predefined in PSUnity and are all disabled by default. This cmdlet enables a city (i.e. make it visible) in the Globe view. A city can receive an alternate name, this can be useful if you are using a naming convention for locations, such as location code or data centre name. Cities also have a status and description. The status defines the color of the object representing the city, Green, Blue, Yellow, Orange and Red.
  • Set-PSUnityDomain: these are representing Active Directory domains.
  • Set-PSUnityCloud: defines a VM in a cloud (cloud view).
  • Set-PSUnityCloudInfo: defines a datacentre managing a cloud (cloud view).

The PSUnity server

The PSUnity server is actually two servers in one. It accepts commands received by PSUnity cmdlets and maintains and updates the list of monitored objects such as servers, cities, sites, domains etc., it also acts as a policy server for the Unity Web client, without which it wouldn’t be able to connect to a port to retrieve object information. This is a security feature of Unity for preventing malicious games to send information to a third party while you are playing.

The PSUnity server is just an exe, and you start it by double clicking it.

The server will just wait for clients to connect. Clients can be PSUnity applications, or Powershell scripts.

Here is an example of Powershell sending data to the PSUnity server. Note that this example has hard-coded values. These values can come from SCOM or other Powershell enabled sources.

 

PSUnity views

Server view

When the application starts, it shows the Server view, disconnected.

newpsunity

The view shows a lonely R2D2 and on the left hand-side some text boxes used to connect to a PSUnity server and define a refresh frequency.

Since we do have a PSUnity server up and running and a Powershell script was able to send some information, let’s hit Update (maybe I should change that to Connect).

 

Let’s understand what we are looking at here. The Update button transformed into a Stop button with a countdown. When the countdown is at 0, a refresh happens.

Clicking on Console showed a window. R2D2 is going around to check each server and retrieve the Description attribute, that value will display in the console. R2D2 figures out where each server is, walks to it, spends a couple of seconds on each server, then moves to the next.

Hitting the « Tab » Key makes the camera follow R2D2. I added this because there might be servers placed outside the view, R2D2 will go to them, and we want to follow him doing so.

Then we see servers of different kinds and something that looks like a platform.

The platform is a PSUnity Site added with this line:

Note that it has a label above with the name and location.

Servers are of type 0, 1 and 2, type 0 being the default and most common one.  Type 1 is the cluster you see on the left representing an EMC Isilon cluster (NAS1 in the pic).

The color codes are defined by the status of the server.

If your Powershell scripts retrieves new values, for example a server becomes critically unavailable then you might want to update what you see.

Then the result would be this:

 

Server1 is now on fire: status 4. Note that we changed the role of the server on the fly.

You can move servers around, change their rotation, status location and description, all with Powershell.

If you update a server position R2D2 will find out and go to it, thanks to Unity gameobject.transform class.

Globe view

The Globe view, is the Earth with a bunch of cities highlighted as seen in the first image in this article.

All the cities are invisible, unless you update them with the following command:

You can add or update a description by calling

globe

On the right hand-side we can see the domains and their statuses, they were added with these commands:

And yes, there are some satellites flying by in low orbit. Courtesy of NASA free 3D models available on their website.

Every now and then an astronaut even flies by the camera. I’m sure at some point we will put the face of someone on it. This astronaut has a much slower orbit making him/her appear only every few minutes, so it’s not too disruptive.

 

Galaxy view

There is another view, actually a few more view, but they are work in progress, and I will eventually talk about them in new articles.

This galaxy view is quite interesting, particularly because the original C++ code comes from Github, and you can read about it here and the algorithm is from the 1960s, but not only, there are over 12.000 objects running around in this spiral galaxy.

3000 stars, 3000 dust objects, 300 arms stars. That’s over 6000.

And each object is actually 2 objects, a sphere, and a particle element. So the following image is displaying 12,000 objects on the screen rotating around together as a spiral galaxy. Amazing how Unity can scale up.

 

galaxy2

This is one of the work-in-progress view, It could represent a cloud of virtual machines. But for that, I’m working on a different view, undisclosed for now, let’s just say that in that new view when a VM fails over to the backup site, it’s a truck that takes it there :).

Cloud

Again, the only limit is your imagination.

More about these views later.

octocatGitHub

PSUnity is based on OpenSource models, and will be OpenSource. I will use releases to allow people to download complete projects or the executables. I just need to try it, and ensure it works, as some of these executables are over 15 MB is size.

The github repository for PSUnity can be found here : https://github.com/MickyBalladelli/PSUnity

Enjoy,

Micky Balladelli

2048 en Powershell

Ecrit par @mickyballadelli

Voici une version du jeu 2048 écrit en Powershell. Le code original est écrit par Gabriele Cirulli en JavaScript. J’en ai fait une version simple en Powershell avec un GUI basé sur les formes et les labels.

Enjoy!

Le but est de combiner plusieurs cases entre elles lorsque leurs valeurs sont les mêmes et d’atteindre 2048. Le jeu peut aller jusqu’à 8192.

Utiliser les flèches pour les déplacements.

Partager ce contenu