All posts tagged: PowerShell Scripting

Accessing Windows Imaging Application Platform Interface (WIMGAPI) in PowerShell

If you have used the Deployment Imaging Servicing Management (DISM) cmdlets in Windows 8 (Server or Client), you would have quickly realized that the functionality is limited only to certain basic tasks around image servicing and tasks such as capturing a WIM image and applying WIM image are still with dism.exe. So, we still need to use dism.exe when we need to perform some of the advanced tasks as I mentioned here. Now, it may not a very suitable solution when you want to do everything using PowerShell. Of course, you can wrap dism.exe in PowerShell but in my opinion, that is not the best way! I prefer everything PowerShell as long as possible.

Attaching scripts or tasks to Windows event log entries using PowerShell and WMI

During a few load test iterations on a SharePoint farm, I started seeing some SQL exceptions in the application log of SharePoint servers. If you are familiar with SharePoint platform, you may have seen these events such as event ID 3355. This event complains that the SharePoint server cannot connect to SQL server. This need not really mean that the DB server is offline. So, to find out the real reason behind these event logs, I needed to start some trace activities whenever event ID 3355 gets logged. Initially, I was looking for eventtriggers.exe which is meant for attaching a script or task to an event log. However, I could neither find this on Windows Server 2008 R2 nor an external download. So, I wanted to look at other options I had.  I found that there are multiple ways to achieve this. Attach to script or task to the event in Windows Event Viewer You can find this option in event viewer. This link is available in the actions pane of event viewer upon selecting …

Passing variables or arguments to an event action in PowerShell

Update: Based on feedback from @ShayLevy, we don’t have to send the background job object to Event action as a variable. It is already available as $event.Sender or just $sender. You can see that in the image below. I just picked up a wrong example to explain the -MessageData parameter. Although, the article originally showed a background job as an example, it was just to show how any object can be passed to event action. For example, if all you want to access is a background job object which you are monitoring using Register-ObjectEvent, you can access the job name using $event.Sender.Name or $sender.Name, job Id using $event.Sender.Id or $sender.Id, and so on. To avoid any confusion, I removed the initial example of background job and updated it with a more generic example. While working with @LaerteSQLDBA on a SQL SMO related script, I was asked a question on how to pass variables or arguments and access them within an event action script block. The solution is simple. We just use the -MessageData parameter of Register-ObjectEvent. …

PowerShell to watch TechEd 2011 sessions

Now that TechED North America is done, all the sessions are now available for offline viewing at https://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/RSS/. There were some great PowerShell sessions by Windows PowerShell team and MVPs. So, I quickly pulled this WPK script to display a simple UI to browse through the list and watch the session. Here is the code behind it: Import-Module wpk

I did not spend lot of time working on this. I could have added a play/pause/stop buttons as well. And, probably a few buttons to download the slides and PPT. 🙂 I added category dropdown at the top so that you can filter the videos by the category you’d like to watch. This one uses WPK module present in Windows 7 PowerShellPack.    

Watch-Process: PowerShell to monitor local or remote process creation or deletion

After answering this question on StackOverflow, I started writing a simple function, for my own use, to monitor remote process creation or termination. I do lot of SharePoint installs on remote machines in my lab environment. This involves installing prerequisite software and SharePoint 2010 bits. I need a way to wait for the remote process to terminate and then do something based on the exit code of the process. This function is very useful to me. I don’t have to worry about event registrations every time I want to monitor a remote process. For the sake of sharing with others, I added monitoring of remote process creation also. If you think you can do this using Wait-Process, go head and explore it yourself. So, here it is: PoshCode: http://poshcode.org/2560 Update1: removed if ($computerName -eq “.”) check. Thanks to @cjwarwickps for the quick feedback. Update2: This results in a blocking call. This means, if you embed a call to Watch-Process in your script, your script just waits for this to complete before proceeding. I have not …

Using DropBox to share PowerShell profile scripts across computers

I started using DropBox for several months ago and I love the way it syncs information across several computer I own. I put all the scripts under development in a DropBox folder and work on them from any PC I have access to. And, the changes are always there on all PCs. There is versioning too. This is simply awesome. Apart from the usual stuff like sharing documents, scripts, and photos, I also use DropBox to share my PowerShell profile scripts across all these computers I work on. All this is done automatically. I just need to set it up once and all my computers receive the changes to my profile script almost instantly. This is how I have been doing it: First, I have DropBox setup on all my computers. Second, I created a folder within DropBox folder and called it PowerShellProfiles. After this, I copied all my PowerShell profile scripts to the PowerShellProfiles folder on DropBox. BTW, I use two different profiles. One for PowerShell console/PowerGUI and another just for ISE. This is …

Monitoring file creation using WMI and PowerEvents module

There are several ways we can create a file monitoring script using PowerShell. There is also a cmdlet in PowerShellPack called Start-FileSystemWatcher to monitor file /folder changes. However, none of these methods survive a exit at the console or wherever the script is running. This is because all these methods create a temporary event consumer. As I’d mentioned in an earlier post, Trevor’s PowerEvents module makes it very easy to create permanent event consumers in PowerShell. In today’s post, we shall look at how we can do that. Before we dig into that, let us first see how we can create a file monitoring script using PowerShell. Many people use CIM_DirectoryContainsFile class and create an event listener. This is how we use do that class in PowerShell.

As you see in the above output, what we get as a part of event data is just that string contained in $Event.SourceEventArgs.NewEvent.TargetInstance.PartComponent. Of course, if you are RegEx lover, you’d just parse that and find the name (extension, etc) of the new file that just got …