茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1670876 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00034.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

微軟帝國 : [轉貼]Playing sounds in PowerShell

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Playing sounds in PowerShell

Playing sounds in PowerShell

You can play sounds in PowerShell in different ways and integrate them in your scripts to provide some kind of indication. One way is with write-host. You can pass the escape sequence `a  to write host. `a is the Alert corresponding special character:

# play a screech sound (through the internal speaker, not useful on laptops)

write-host "`a";

 

Another way is with a . NET class (.wav format).

# play the file once
$sound = new-Object System.Media.SoundPlayer;
$sound.SoundLocation="c:\WINDOWS\Media\notify.wav";
$sound.Play();


Note that the following process is asynchronously, meaning that the script wont hang until sound has finished playing. This example plays the file repeatedly until a condition met

$sound = new-Object System.Media.SoundPlayer;
$sound.SoundLocation="c:\WINDOWS\Media\notify.wav";
$sound.PlayLooping();
$flag=$false;

1..10 | foreach {
    if($_ -gt 5){$flag=$true} else{sleep -s 1}
    if($flag) { $sound.Stop() }
}

write-host "Done";

There is another class called System.Media.SystemSounds. Pipe the class to get-member to reflect its static members:

 

PS C:\Scripts> [System.Media.SystemSounds] | gm -static

   TypeName: System.Media.SystemSounds

Name            MemberType  Definition

----               ----------         ----------
(...)
Asterisk        Property         static System.Media.SystemSound Asterisk {get;}
Beep             Property         static System.Media.SystemSound Beep {get;}
Exclamation  Property         static System.Media.SystemSound Exclamation {get;}
Hand            Property         static System.Media.SystemSound Hand {get;}
Question       Property         static System.Media.SystemSound Question {get;}

 

Each property emitted represents a system sound type. Now, Pipe each property to get-member

 

PS C:\Scripts> [System.Media.SystemSounds]::Asterisk | gm

   TypeName: System.Media.SystemSound

Name         MemberType   Definition
----            ----------         ----------
(...)
Play           Method           System.Void Play()
ToString    Method           System.String ToString()

 

Great, we can use the Play method,test each line in your console:

[System.Media.SystemSounds]::Asterisk.Play();
[System.Media.SystemSounds]::Beep.Play();
[System.Media.SystemSounds]::Exclamation.Play();
[System.Media.SystemSounds]::Hand.Play();

[System.Media.SystemSounds]::Question.Play();

 

Finally, here is a one-liner for the job. It Utilizes the SoundPlayer(String) Constructor. It Initializes a new instance of the SoundPlayer class, and attaches the specified .wav file.

(new-object Media.SoundPlayer "C:\WINDOWS\Media\notify.wav").play();


原文出處: $cript Fanatic: Playing sounds in PowerShell
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]How to play beep and other system sounds

#16 : How to play beep and other system sounds?

Below piece of code can be used to play a beep from a Powershell script. I have used in many of scripts where user interaction is required. Presenting a list of different sounds which can be played from a Powershell script.

Beep:
  1. #--Statement to play beep from Powershell Scripts --#    
  2. clear   
  3. [System.Media.SystemSounds]::Beep.Play()   
Hand:

  1. #--Statement to play hand from Powershell Scripts --#    
  2. clear   
  3. [System.Media.SystemSounds]::Hand.Play()   
Asterisk:
  1. #--Statement to play Asterisk from Powershell Scripts --#    
  2. clear   
  3. [System.Media.SystemSounds]::Asterisk.Play()   
    Exclamation:
    1. #--Statement to play Exclamation from Powershell Scripts --#    
    2. clear   
    3. [System.Media.SystemSounds]::Exclamation.Play()   
    Please test and let me know your response. Thanks.

    原文出處:Som's Powershell Tips: #16 : How to play beep and other system sounds?
    前一個主題 | 下一個主題 | 頁首 | | |


    
    Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|