📜 ⬆️ ⬇️

How can you use PowerShell?

Inspired by q & a: How to play mp3 from the command line in Windows 7?

A bit shocked by the suggested answers, which boil down to 'download and install additional software' ... and so in the system already under 20GB of some kind of shit something useful that is called an operating system ... I apologize, it broke out.

Once in Windows XP, I used the simplest utility mplay for this task, although the GUI but the task scheduler normally starts, but in Windows 7 it was deleted and even sndrec32. I think it’s foolish to run a monstrous wmplayer, so I have to turn to scripting languages.
')
Examples of single-line vbs scripts for windows scripting host can be found on the Internet, here is the simplest example:
Set oVoice = CreateObject( "SAPI.SpVoice" )
set oSpFileStream = CreateObject( "SAPI.SpFileStream" )
oSpFileStream.Open "c:\Windows\Media\tada.wav"
oVoice.SpeakStream oSpFileStream
oSpFileStream.Close

By the way, it’s a miracle that the code worked on windows 7, since compared to windows xp a number of activex objects disappeared (like WMPlayerClass) and nobody knows when the SAPI class will disappear or how the SAPI class will change, and it’s not a matter of hammering nails. Therefore, I think this solution is wrong and not logical, and ActiveX is slowly dying off, it is quite logical to kill .NET environment.

I tried to solve the problem with the new-fashioned PowerShell (I don’t know how to cook, but I rummaged in the documentation), my hair stood on end on how great ideas were combined with a terrible implementation. For example, there is no normal possibility to run a script from the command line without creating and signing the file (I understand everything, safety is above all, as a result of questions whether it is worth using it).

Here is the simplest code:
$s=new-Object System.Media.SoundPlayer;$s.SoundLocation='C:\Windows\Media\tada.wav';$s.Play();
Works great inside powershell, if you insert it and execute it manually!

But in this way:
powershell -Command $s=new-Object System.Media.SoundPlayer;$s.SoundLocation='C:\Windows\Media\tada.wav';$s.Play();
Nothing happens, because each command is executed in its context (that is, at each step $ s is not defined).

I tried to use redirection:
echo $s=new-Object System.Media.SoundPlayer;$s.SoundLocation='C:\Windows\Media\tada.wav';$s.Play(); | powershell -command -
And the result was shocked - the registered player was launched by default on a wav file (it turned out to be winamp for me). Why on earth?

And without using -command -:
echo $s=new-Object System.Media.SoundPlayer;$s.SoundLocation='C:\Windows\Media\tada.wav';$s.Play(); | powershell
PowerShell starts up and waits for commands to be entered.

Okay, give up, try to resolve unsigned scripts (run under the preferred shell):
powershell Set-ExecutionPolicy Unrestricted
Create and run a file with the above code (putting it in c: \ a.ps1):
powershell c:\a.ps1
And we get nothing! Silence, and no error messages (when running unsigned scripts is not allowed, I receive a corresponding message).

Will the respected habrasoobshchestvo help this task in a simple and racial way in the right way? As I understand it, the last test showed that the epic with certificates is not worth starting because the problem is somewhere else.

UPD from TheBits and Paul : I didn’t notice the elephant, the script ends up without completing the play, and the launch of the code from the command line can be organized with the help of grouping and the & symbol:
powershell -Command "& {$s=new-Object System.Media.SoundPlayer; $s.SoundLocation='C:/Windows/Media/tada.wav'; $s.Play(); Sleep -Seconds 9}"


UPD2 Based on a number of comments: The final decision, of course, may be so, taking into account the automatic determination of the file duration and the exclusion of the $ s variable:
powershell -Command (new-object Media.SoundPlayer "C:\Windows\Media\notify.wav").playsync();
But the most important thing is that it does not solve the original problem - System.Media.SoundPlayer cannot play mp3 files, i.e. you need something more difficult to sculpt, but I did not think the purpose of this post to put the solution to the original problem.

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


All Articles