Enough automation - it's time to draw an owl in PowerShell and teach the console to speak
Today is PowerShell's high point: a time of weird scripts and games created with Power Shell. If you are tired of a serious routine and want to look at other PowerShell talents, I invite you to cat.
Talking console
Let's start with the simplest entertainment - the talking console. The speech synthesis interface has existed in the Windows family for a long time: in the .Net library it is called System.Speech, and for those who prefer 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 votes can be installed in the system - there are options for 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 choose the right voice with the command:
$voice.SelectVoice("имя")
Armed with this knowledge, you can collect monitoring that would indicate a server crash, or surprise colleagues with your favorite remote access tool to their machines. You can use our material “ 1000 ++ way to run commands on a remote computer ”.
As ready-made examples, I’ll leave a few links:
- PowerShell Pranks on Matt Blogs IT Blog .
- Fun and Games with PowerShell on the Mike F Robbins Blog .
- Funtalk.ps1 script by Jorge Rodríguez Galán .
But one opportunity to tell us, of course, is not enough. How about the console still cheering with the melody on opening?
Hello% USERNAME%
PowerShell has the concept of a profile. You can read more about this in the Windows PowerShell Profiles article on TechNet. I only note that with the help of profiles you can configure the following entities:
- Aliases teams.
- The size of the console window.
- Color and title bar.
- Start folder instead of user profile.
- Run tasks when you open the console.
The last option allows, for example, to play music every time someone opens a PowerShell window. As preparation, you need to create a profile, if it is still not there, with the following command:
New-Item -path $profile -typefile -force
Now you can change the profile with any editor - for example, PowerShell ISE:
PowerShell_ISE.exe $profile
As an example of starting a task, spiceworks portal user 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 use the material from the previous section, so that the console at the start tells a fairy tale or importantly welcomes the system administrator by name and patronymic. And you can open the DVD-ROM (who still have those) with the command:
(New-Object-com "WMPlayer.OCX.7").cdromcollection.item(0).eject()
Here, to open the "coffee stand", the WMPlayer.OCX.7 object is used. It can also be used for its intended purpose - to play music.
Making a console player
If for some reason I don’t like the good old mpd and want to invent a bicycle - or rather look at the finished one - then Prateek Singh , an engineer, developer and just a good person, suggested on his blog an interesting option for using the system object to manage Windows Media Player and PowerShell . The peculiarity of the script is that it can mix reproduced tracks.
The script can be immediately added to the PowerShell profile, and then run the player with one command:
Music"путь к файлам с музыкой"
Player launch.
To control playback, a command can be launched with the parameters Shuffle (shuffle), Loop (loop playback) and Stop (stop playback), and when moving to the next track, a system notification will appear in the lower corner.
Notification of the launch of a new track.
The full script is available in the author repository .
Prateek Singh's blog has many scripts of varying degrees of usefulness, so I’ll give links to only a few of them:
- Error decoder for HTTP, network card manufacturer code by MAC address, protocol description by port, etc.
- Module for building diagrams.
- Weather forecast with authentic console graphics.
Weather forecast on PowerShell. Because we can.
Since we hit on multimedia, I’ll 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 posted a blog post, Create mixed reality models in PowerShell , which explored the possibility of generating 3D model files using PowerShell.
If you parse the .obj file, you can find there the coordinates of points with the notation “ v ”, and the coordinates of the planes under the letter “ f” . For example, a simple cube would look like this in a file:
v000
v 001
v 011
v 010
v 100
v 101
v 111
v 110
f 1234
f 8765
f 1562
f 4851
f 2673
f 3784
If you save the resulting file and open it with a graphical viewer (for example, “a mixed reality viewer”), then a cube will really be drawn.
Freshly created cube.
Manually creating files with more complex structures is more difficult, so math and PowerShell come to the rescue.
For example, you can create a glass of wine for the glory of Dionysus or Bacchus. Since the glass is a figure of rotation, it is enough to create only half the glass. Then, during rotation, the desired vessel will be obtained.
When you rotate this figure, you get a glass.
Next, add some materials, remember the school course of mathematics, arrange in the form of a script and get a wonderful glass.
Ready glass.
Ready-made scripts and textures are available in the author’s GitHub account in the obj-in-powershell project . Now let's move on to the more practical use of PowerShell and 3D.
Unity Monitoring
Just imagine: a beautiful map on the big screen in the IT office, which displays the objects of the monitoring system and their status. And when there are no prying eyes, you can collapse the map and watch The Simpsons.
The card provider can be the same Zabbix or Fpinger . But despite the excessive informativeness, their beauty is not very good. Micky Balladelli undertook to correct this omission by applying Unity capabilities as a set of PSUnity applications for visualization .
The idea is simple - a server is launched on a specific port, and a set of PowerShell cmdlets is used to add monitoring objects or change their status. The client connects to the server and displays the resulting picture. The statuses of objects can be taken from a familiar monitoring system - for example, from Zabbix or SCOM.
When starting the server without objects, you can observe a lone R2D2 in a clean field:
The droid we are looking for.
Using the View button, you can switch modes - data center, planet, space. Pretty deserted.
Uninhabited monitoring planet.
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 -RoleExchange -Location Maui -Status0 -Description "EXSRV1 has drifted its configuration."
The above command will add a server with an ominous description.
They added the server to the card, and R2D2 begins 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" -Status3Set-PSUnityCity -Session $Session -Name"Saint Petersburg" -Status1
Two cities added.
Another interesting view is the two data centers with virtual machines and their statuses. Add a couple:
Set-PSUnityCloudInfo -Session $Session -Site 2 -Name"Амазон" -Role"Ожидаем" -Location "Запасной ДЦ" -Status4Set-PSUnityCloudInfo -Session $Session -Site 1 -Name"Хетцнер" -Role"Активный" -Location "Основной ДЦ" -Status1
Now we will create many virtual machines in the main data center:
$color = 4for ($i = 0; $i -lt1500; $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 this result:
Virtual computers are spinning. Beauty!
You can download the server itself with the client, a set of cmdlets and sample scripts 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 "blew up" a game called 2048 . Initially, the browser version received many adaptations, including for mobile devices. Of course, there was a clone on PowerShell created by the already mentioned Micky Balladelli . The script is available on 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 already managed to break the smartphone in a rage and forget the game like a nightmare - let me remind you of it as a clone on PowerShell.
The clone itself is available in the Flappy Shell repository of engineer Apoorv Verma .
We remember the game, collect the walls, we get the negative.
At the end of the article, I would like to give a link to the adaptation of my favorite childhood game - Space Invaders . The PowerShell solution is available for download in an Adrian Milliner article titled Space Invaders . A script is not just a script, but a full-fledged game with sound. Be careful at work.
Nostalgic with PowerShell, fighting alien invaders.
If you have your own interesting and unusual PowerShell scripts, you know what to do.