📜 ⬆️ ⬇️

Enough automation - it's time to draw an owl on PowerShell and teach the console to speak


Today is PowerShell's finest hour: the time of strange scripts and games created with the help of “Powerful Shell”. If you are tired of a serious routine and want to look at other talents of PowerShell, I invite you to the cat.


Talking console


Perhaps we will begin with the simplest entertainment - the talking console. In the Windows family, the speech synthesis interface has been around for a long time: in the .Net library it is called System.Speech, and for preferring COM objects it is called SAPI.SpVoice. PowerShell allows you to use both methods.


Check the operation of the interface with the following script:


Add-Type -AssemblyName System.Speech $voice = New-Object System.Speech.Synthesis.SpeechSynthesizer $voice.Speak("-- —    ,    !") 

Several voices can be installed in the system - there are variants of different nationalities, ages and genders, each with its own name. You can view the general list with the command:


 $voice.GetInstalledVoices().VoiceInfo 


Irina and Zira are ready.


Additional “imaginary friends” can be downloaded along with the input language through the standard Windows interface, from the Microsoft website or from external sources .

You can select the appropriate voice with the command:


 $voice.SelectVoice("") 

Armed with this knowledge, you can collect monitoring, which would speak about the fall of the server, or surprise your colleagues with the help of your favorite tool for remote access to their machines. You can use our material " 1000 ++ way to run commands on a remote computer ."


As ready examples I will leave some links:



But one opportunity to tell us, of course, is not enough. How about that, so that the console still welcomed melody at the opening?


Hi% USERNAME%


PowerShell has a profile concept. You can read more about this in the Windows PowerShell Profiles material on TechNet. I note only that with the help of profiles you can configure the following entities:



The latter option allows, for example, to play music every time someone opens the PowerShell window. As a preparation, you need to create a profile, if you still don’t have it, with the following command:


 New-Item -path $profile -type file -force 

Now you can change the profile with any editor - for example, PowerShell ISE:


 PowerShell_ISE.exe $profile 

As an example of launching a task, the user of the spiceworks portal Luke Murray suggested playing the imperial march with the following script:


 start-job { [console]::beep(440,500) [console]::beep(440,500) [console]::beep(440,500) [console]::beep(349,350) [console]::beep(523,150) [console]::beep(440,500) [console]::beep(349,350) [console]::beep(523,150) [console]::beep(440,1000) [console]::beep(659,500) [console]::beep(659,500) [console]::beep(659,500) [console]::beep(698,350) [console]::beep(523,150) [console]::beep(415,500) [console]::beep(349,350) [console]::beep(523,150) [console]::beep(440,1000) } 

As a task, you can also use the material from the previous section, so that the console tells a fairy tale when it starts up or importantly greeted the system administrator by name and patronymic. And you can simply open the DVD-ROM (who still have these) with the command:


 (New-Object -com "WMPlayer.OCX.7").cdromcollection.item(0).eject() 

Here, the WMPlayer.OCX.7 object is used to open the “coffee stand”. It can be used for its intended purpose - to play music.


Making console player


If for some reason you don’t like the good old mpd and you’d like to reinvent the wheel - or rather look at the finished one - then the engineer, developer, and just a good person . The peculiarity of the script is that it can mix reproducible tracks.


The script can be immediately added to the PowerShell profile, and then run the player with one command:


 Music "    " 


Run player.


To control playback, you can run the command with the parameters Shuffle (shuffle), Loop (loop playback) and Stop (stop playback), and when you move to the next track, a system notification appears in the lower corner.



Notification of the launch of a new track.


The full text of the script is available in the repository of the author .


Prateek Singh has a lot of scripts of varying degrees of utility, so I’ll give links to just some of them:




Weather forecast on PowerShell. Because we can.


If we hit in multimedia, I will tell you about the option of creating objects for 3D printing, modeling and other augmented reality.


How to draw an owl, now in 3D!


In January, Cosmos Darwin published in his blog the article “ Create mixed reality models in PowerShell” , which discusses the possibility of generating 3D model files using PowerShell.


If you disassemble the file .obj, then you can find there the coordinates of points with the designations " v ", and the coordinates of the planes under the letter " f" . For example, a simple cube would look like this in a file:


 v 0 0 0 v 0 0 1 v 0 1 1 v 0 1 0 v 1 0 0 v 1 0 1 v 1 1 1 v 1 1 0 f 1 2 3 4 f 8 7 6 5 f 1 5 6 2 f 4 8 5 1 f 2 6 7 3 f 3 7 8 4 

If you save the resulting file and open it with a graphic viewer (for example, “a mixed reality viewer”), then a cube will actually be drawn.



Freshly created cube.


Creating files with more complex structures manually is more difficult, so mathematics and PowerShell come to the rescue.


For example, you can create a glass of wine to the glory of Dionysus or Bacchus. Since the glass is a figure of rotation, it is enough to create only half of the glass. Then, with rotation, you get the desired vessel.



With the rotation of this figure you get a glass.


Next, add a few materials, remember the school course of mathematics, arrange in the form of a script and get a great glass.



Ready to glass.


Ready scripts and textures are available in the account of the author GitHub in the project obj-in-powershell . We now turn to a more practical application of PowerShell and 3D.


Monitoring on unity


Just imagine: a beautiful map on the big screen in the IT room, which displays the objects of the monitoring system and their status. And when there are no prying eyes, you can roll the map and watch the Simpsons.


As the supplier of the card can be the same Zabbix or Fpinger . But despite the excessive information content, with the beauty they have not. Micky Balladelli undertook to correct this omission, using the capabilities of Unity as a set of PSUnity applications for visualization .


The idea is simple - a server is started on a specific port, and a set of PowerShell cmdlets is used to add objects to monitor or change their status. The client connects to the server and displays the resulting picture. Statuses of objects can be taken from the usual monitoring system - for example, from Zabbix or SCOM.


When you start the server without objects, you can watch a single R2D2 in the open field:



The droid we are looking for.


By the View button you can switch modes - data center, planet, space. Pretty deserted.



Unpopulated planet monitoring.


The situation changes when we start adding objects using PowerShell.


You can connect to the server itself with the command:


 $Session = New-PSUnitySession -ComputerName localhost -Port 7777 

You can add objects to the map with the droid using the Set-PSUnityServer cmdlet:


 Set-PSUnityServer -Session $Session -X 0 -Y 5 -Name EXSRV1 -Role Exchange -Location Maui -Status 0 -Description "EXSRV1 has drifted its configuration." 

The above command will add a server with an ominous description.



Added a server to the map, and R2D2 starts to study it.


To add a city to the planet and change its status, you can use the following commands:


 Set-PSUnityCity -Session $Session -Name "Moscow" -Status 3 Set-PSUnityCity -Session $Session -Name "Saint Petersburg" -Status 1 


Two cities added.


Another interesting view is two data centers with virtual machines and their statuses. Add a couple:


 Set-PSUnityCloudInfo -Session $Session -Site 2 -Name "" -Role "" -Location " " -Status 4 Set-PSUnityCloudInfo -Session $Session -Site 1 -Name "" -Role "" -Location " " -Status 1 

Now let's create a lot of virtual machines in the main data center:


 $color = 4 for ($i = 0; $i -lt 1500; $i++) { Set-PSUnityCloud -Session $Session -Site 1 -Name "VM$i" -VMHost "Hostname" -Cluster "ClusterID" -Status $color -Description Running $color++; if ($color -eq 5) { $color = 0 } } 

The output will be the following result:



Virtuals are spinning. Beauty!


Download the server itself with the client, a set of cmdlets and sample scripts can be in the releases section of the PSUnity project. Once we got to Unity, it's time for a couple of straightforward PowerShell games.


Gamedev and PowerShell


A few years ago, the world was “blown up” by a game called 2048 . Initially, the browser version received a lot of adaptations, including for mobile devices. Of course, there was also a clone on PowerShell, created by the aforementioned Micky Balladelli . The script is available in the author's blog, in article 2048 en Powershell .



Game interface


The second popular game is Flappy Bird , originally created for mobile devices. If you have already managed to break the smartphone in rage and forget the game as a bad dream - let me remind you of it with a clone on PowerShell.


The clone itself is available in the repository of Flappy Shell by the engineer Apoorv Verma .



We recall the game, collect the walls, get the negative.


At the end of the article I would like to provide a link to the adaptation of the favorite childhood game - Space Invaders . The PowerShell solution is available for download in an Adrian Milliner article with the telling title Space Invaders . The script is not just a script, but a full-fledged game with sound. Be careful at work.



Nostalgic with PowerShell, fighting the alien invaders.


If you have your own interesting and extraordinary PowerShell scripts - you know what to do.


')

Source: https://habr.com/ru/post/352726/


All Articles