Of how to use PowerShell?

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

A bit shocked, offered answers that boil down to 'download and install additional software'... and so the system is already under 20Gb some shit something useful, which is called by the operating system... sorry, it slipped.

When in Windows XP used for this task a simple utility mplay, though the GUI but the task scheduler normally starts, but in Windows 7 removed it and even sndrec32. Start wmplayer monstrously feel stupid, so you have to turn to scripting languages.

Examples of one-line scripts on vbs for windows scripting host you can dig up on the Internet, here is a simple 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 has earned on windows 7 as compared to windows xp a number of activex objects are gone (like WMPlayerClass) and no one knows when it will disappear or how to change the SAPI class, and this is not the case, a microscope to hammer nails. Therefore, this decision is considered wrong and not logical, and ActiveX is dying slowly, it is logical to kill .NET environment.

I tried to solve the problem of new-fangled PowerShell (can't cook, but specially rummaged in the documentation), my hair stood on end from how it combines great ideas with horrible implementation. For example you are not able to run the script from the command line without creating and without signing the file (I understand, safety is paramount, as a result of the questions, whether to use it).

Here is a simple code:
$s=new-Object System.Media.The SoundPlayer;$s.SoundLocation='C:\Windows\Media\tada.wav';$s.Play();
works Fine inside powershell if you have it insert and execute manually!

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

Tried to use redirection:
echo $s=new-Object System.Media.The SoundPlayer;$s.SoundLocation='C:\Windows\Media\tada.wav';$s.Play(); | powershell -command -
And was shocked by the result — was launched was the default player for wav file (for me it was winamp). Why?

And without use -command -:
echo $s=new-Object System.Media.The SoundPlayer;$s.SoundLocation='C:\Windows\Media\tada.wav';$s.Play(); | powershell
there is an easy launch PowerShell with the expectation of entering commands.

Okay, I give up, trying to allow non-signed scripts (run under privileged shelom):
powershell Set-ExecutionPolicy Unrestricted
Create and run the file with the above-proposed code (placing it in c:\a.ps1):
powershell c:\a.ps1
And get nothing! Silence, and no error messages (when not allowed to run unsigned scripts receive a corresponding message).

if respected Hebraist to solve this problem is simple and racially the right way? As I understand it, the last test showed that epic certificates are not worth starting, because the problem is somewhere else.

UPD TheBits and Paul: Elephant I did not notice, the script completes do not play the game, and run the code from command line, you can arrange with significant groupings and the symbol &:
powershell -Command "&{$s=new-Object System.Media.The SoundPlayer; $s.SoundLocation='C:/Windows/Media/tada.wav'; $s.Play(); Sleep -Seconds 9}"


UPD2 based On number of comments: the Final decision itself may be subject to the automatic determination of the duration of the file and the exception variable $s:
powershell -Command (new-object Media.SoundPlayer "C:\Windows\Media\notify.wav").playsync();
But most importantly, it never solves the original problem — System.Media.The SoundPlayer can not play mp3 files, i.e. you need something more complicated to model, but I thought the purpose of this post to put a solution to the original problem.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Tactoom. How about the middle of blogging?

SumIT Weekend of 18-19 February, the idea for iPad and Hackathon

Knowledge base. Part 2. Freebase: make requests to the Google Knowledge Graph