Learn about Windows PowerShell
Summary: Learn how to automatically add multiline comments in the Windows PowerShell ISE, how to work with text files in VBScript, and how to customize Microsoft Office Communicator.
Hey, Scripting Guy! How do I get my computer to play the Star Trek "Red Alert" sound when a chosen persons Microsoft Office Communicator status changes?
Hello MH, great idea! You have to find a .wav file from the Internet that contains the sound that you want to play. Then you go into Control Panel/Appearance and Personalization/Personalization/Sounds and select the particular event. Within Office Communicator, you click in the upper-left corner to show the menu, select Tools/Options/Alerts. Select Configure Sounds and then select the Alert. Note, this also brings you to the Personalization/Sounds menu. You can tag a person to notify you when his status changes. Unfortunately, I do not know of a way to cause it to play custom sounds for custom individuals. Personally, I only track a couple of people to alert me when their status changes. Therefore, it is not a problem. If you want to track many people, and want customizable sounds, then it would involve trapping the alert and then parsing the alert and then selecting the sound to play if the alert matches the user name MSDN does document an API for Office communicator, but I have not played around with it much. There is an OnContactStatusChange event that you should be able to use.
Hey, Scripting Guy! Is there a way to use a wildcard in a VBScript like the asterisk character (*) in a batch file? I am trying to email a file that I rename with a date stamp. I have tried using '%' & '@' & '*' which do not work? I did find some code where you have to do a search then do a FOR EACH statement but I was hoping to keep things clean.
Hello WM, if you are talking about using a wildcard with the getfile method, I do not think that is possible, because it requires either a path or a relative path. You would have to use files to return a files collection, and then use for each next to walk through the collection. Inside the collection, you could then parse the file name to see whether it matches.
However, using the WMI class Cim_DataFile you can use wildcard characters. Look at the Sesame Script articles where there are several articles that explore working with files. In addition to the Sesame Script articles, there are several example scripts on the Script Repository that talk about how to work with text files. Whereas many of them are talking about copying and deleting text files, you will also find scripts that explain using Cim_DataFile to search for files that have a wildcard name. With these two resources at your disposal, you should be able to accomplish what you want. If, after consulting the two previously mentioned resources, you need additional help, there are many Hey Scripting Guy! blog posts that talk about how to work with files within VBScript. In addition, if while writing your script, you get stuck, give a shout out to the Official Scripting Guys! Forum - just do not expect them to write your script for you. They are volunteers, not personal script monkeys.
Hey, Scripting Guy! I've seen many your Q&A blogs around TechNet and have a question I have not been able to find out myself.
Is it possible to encrypt a .ps1 file from a username and password, but instead of it extracting to another decrypted file when you run it (or decrypting to another file, then running that file), having it run straight away, decrypting on-the-fly as the script runs?
We are running a script which logs into several databases with administrative access. Therefore, if we accidentally leave the script on someone's computer, we want to be assured that our data would be safe.
Hello ZB. Because Windows PowerShell is a .NET Framework application, you can encrypt the contents of the file by using Windows encryption just as you would encrypt any other file. This relies on your EFS key. The problem is that if you are running the script on another machine, and impersonating THAT user, then you begin to have issues.
The best way to do these kinds of things is to supply the credentials to the script when it launches, and not to store the credentials at all. You can easily do this by using the Get-Credential Windows PowerShell cmdlet. If your databases are using Integrated Windows Authentication, then there is no issue at all with this approach.
If your database does not use Windows authentication, then the best approach is to have your script use a web service that will make the connection to the database.
Another approach would be to use the Windows Task Scheduler which will let you safely store credentials to make your database connection.
If you must supply credentials to the script, then you can use Get-Credential to obtain a credential object, and then you can store that credential object into a file. You could then read the file and obtain the credential object that is required. This is the least secure approach, and is not one that I personally recommend. It would have to be vetted by your security team also. Lee Holmes has a discussion of this technique on his website.
Hey Scripting Guy! Does the PowerShell ISE have a keyboard shortcut, or any support for that matter, for commenting code? I would love comment a single line or multiple lines similar to Visual Studio with a Ctrl+K, C for commenting or Ctrl+K, U for uncommenting.
Hello DP, not out of the box. However, it could be added. Windows PowerShell ISE is highly configurable and an API is exposed to enable you to customize it. Here is a link to some blogs I have written about Windows PowerShell ISE.
There is an ISE Pack Module in the Windows 7 Resource Kit PowerShell Pack that can be downloaded from MSDN Code Gallery. When you load the ISEPack module, it adds a toggle comments command to the Add-Ins menu. To use it, you highlight your code and then launch the command either through the menu structure, or by using the associated keyboard shortcut. This is shown in the following figure.
As you can see from the picture, the ISEPack adds many additional commands. The items above the ISEPack were added as a result of my posts on the Windows PowerShell ISE; those modules are available in the Scripting Guys Script Repository, and are linked from the appropriate posts.
Well, this concludes another edition of Quick Hits Friday. Join me tomorrow for the Weekend Scripter when Phil Braniff will be the guest blogger as he discusses a large Microsoft Exchange migration.
I would love you to follow me on Twitter or Facebook. If you have any questions, send email to me at firstname.lastname@example.org or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
<p>This is really cool. The blog looks good. Robson Eustaquio de Mesquita</p>
<p>Thanks for the great info Ed. To your Automatically Add Comments In The PowerShell ISE section, the PowerShellPack Toggle Comments command isn't very good as it is destructive to blank lines (and for other reasons). I explain more and share the code for my own solution on my blog at <a rel="nofollow" target="_new" href="http://blog.danskingdom.com/powershell-ise-multiline-comment-and-uncomment-done-right-and-other-ise-gui-must-haves/">blog.danskingdom.com/powershell-ise-multiline-comment-and-uncomment-done-right-and-other-ise-gui-must-haves</a> </p>