Trevor Sullivan's Tech Room

Minding the gap between administration and development

Posts Tagged ‘development’

PowerShell: PowerEvents Module Update to 0.3 Alpha

Posted by Trevor Sullivan on 2012/01/11

imageIf you haven’t already checked it out, I wrote and published a PowerShell module on CodePlex a little over a year ago. It’s called PowerEvents for Windows PowerShell, and allows you to easily work with permanent WMI (Windows Management Instrumentation) event subscriptions. Some folks may not be aware that I’ve also written comprehensive documentation on the theory behind WMI events and why they’re useful. This ~30-page PDF document is included in the PowerEvents download, and is useful even if you do not want to use the PowerEvents module.

As a bonus, the PowerEvents module was mentioned just recently in the PowerScripting Podcast (listen around 1h19m)!!

Listen to my interview with the PowerScripting Podcast back in December 2010!

PowerEvents Download Link:


Posted in powershell, scripting, tools, wmi | Tagged: , , , , , , , , | 1 Comment »

Uploading a Video to Youtube via PowerShell

Posted by Trevor Sullivan on 2010/03/09

Hey guys! I know it’s been a long time since I have posted anything useful (that is, assuming anything I post is useful! haha). While sitting at Starbucks today, I suddenly came up with an idea for a new post though, and after doing a quick bit of research, figured I would write about it! The purpose of today’s post is to show an example of using PowerShell to upload a video to Youtube! This could be useful in a variety of circumstances, but I’ll leave finding the use cases up to each of you individually 🙂 A couple of examples I can think of would be:

  • Batch uploading a folder of videos
  • Automating the upload of videos that may be somehow generated into a folder automatically
  • …. ???

In any event, in my search to find a solution, knowing that PowerShell is based on the Microsoft .NET Framework, I searched for “C# upload youtube”. One of the first results that came up was a blog called “Trails in the Sand,” and more specifically, a post entitled “Programmatically Uploading Videos to Youtube.” This blog article goes into detail about uploading videos to Youtube using C#/.NET, but at the bottom of the post, the author was kind enough to include the source code for a library he had written! As a PowerShell scripter, you’ve probably learned to be very thankful for other people who write and share .NET libraries to do “cool stuff!”

After downloading the “Trails in the Sand Youtube Library,” I opened it up in Visual C# 2008 Express Edition (free, fully-functional version of Visual Studio), and converted the project to Visual Studio 2008 format, using the wizard that pops up. The conversion was successful, so I proceeded to create a developer key for Youtube. You can generate your own developer key at the following URL:

After pasting my developer key into the project under the “devCode” string constant, I compiled the library by hitting “F6.” This created my library under the “bin\release” folder of the project. Next, I opened Quest’s PowerGUI (a great, free PowerShell editor), and started a new script. First things first, using the System.Reflection.Assembly .NET class, I loaded the newly created .NET Youtube library into PowerShell:


Next, I instantiated the Trails.YouTube object:

$Yt = New-Object Trails.YouTube

Once I had the YouTube object, I defined a string variable to use as an “out” parameter, to capture any resulting error (if you don’t pre-define it, you’ll get an error):

$UploadError = “”

Next, we call the Authorize() method, passing our username and password:

$Yt.Authorize(“username”, “password”)

And then finally, we upload the video using the Upload() method! Parameters are as follows:

  • Title
  • Description
  • Category (library has a spelling error: “catagory”)
  • Video keywords
  • Path to local video file
  • Error variable (pre-defined variable, used as an “out” parameter)

$result = $Yt.Upload(“Test upload”, “test upload”, [Trails.YouTube+Catagory]::Howto, “keyword1”, “C:\Users\trevor.sullivan\Videos\SMS_BootImagePackage.wmv”, [ref] $UploadError)

Optionally, you can print out any resulting error message:


The entire script put together looks like this:

$LibYt = [System.Reflection.Assembly]::LoadFile(“c:\Users\trevor.sullivan\Documents\\YouTube\bin\Release\YouTube.dll”)

$Yt = New-Object Trails.YouTube

$UploadError = “”

$Yt.Authorize($(Read-Host -Prompt “Please enter username”), $(Read-Host -Prompt “Please enter password”))

$result = $Yt.Upload(“Test upload”, “test upload”, [Trails.YouTube+Catagory]::Howto, “keyword1”, “C:\Users\trevor.sullivan\Videos\SMS_BootImagePackage.wmv”, [ref] $UploadError)


If we wanted to “variable-ize” (yes, I made that up just now) this script a bit more, we could do something like this:

### Define YouTube library path

$YtDll = “c:\Users\trevor.sullivan\Documents\\YouTube\bin\Release\YouTube.dll”

### Get YouTube username and password

$YtUser = $(Read-Host -Prompt “Please enter username”)

$YtPass = $(Read-Host -Prompt “Please enter password”)

######## Set up video variables ########

$vTitle = “Video Title”

$vDesc = “Video Description”

$vCat = [Trails.YouTube+Catagory]::Howto

$vKeyword = “keyword1”

$vPath = “C:\Users\trevor.sullivan\Videos\SMS_BootImagePackage.wmv”

$vErr = “”

### Load YouTube library, login, upload video, and echo error

$LibYt = [System.Reflection.Assembly]::LoadFile($YtDll)

$Yt = New-Object Trails.YouTube

$Yt.Authorize($YtUser, $YtPass)

$result = $Yt.Upload($vTitle, $vDesc, $vCat, $vKeyword, $vPath, [ref] $vErr)


Hope this post helps!


Posted in powershell, scripting, tools | Tagged: , , , , , , , , , , , , , | 5 Comments »

Code Snippet: Process Time as Percentage of Total CPU Time

Posted by Trevor Sullivan on 2009/11/09

A couple of weeks ago, a colleague of mine asked me how I would determine a process’ CPU utilization as a percentage of total CPU time. I came up with this PowerShell code, but it’s not quite complete yet, because it doesn’t dynamically determine the number of cores that a system has.

During the investigation of this problem, I came across a few key learnings:

  1. The system’s last boot-up time must be converted from WMI DateTime format to a .NET System.DateTime struct so that one can be properly subtracted from the other using the System.DateTime’s subtraction operator overload
  2. Retrieving the number of cores from a system depends on the operating system. For XP/2003, you would use Win32_ComputerSystem.NumberOfProcessors, but on Vista / 7 / 2008 / 2008 R2 you would use the Win32_ComputerSystem.NumberOfLogicalProcessors property. This is important in calculating total [virtual] system uptime. It’s simply <RealTime> * <NumberOfCores> as best I can tell
  3. If a process is closed and restarted, you will not get an accurate percentage … perhaps you could use the CreationDate property of the Win32_Process WMI class to determine how long a process has been running, versus total system uptime

The PowerShell code below retrieves the percentage of CPU time that the System Idle Process has consumed on a dual-core (or single core, hyper-threaded) system.

Please note that modifications are required for other hardware configurations; I have not yet had a chance to revise the code appropriately, but I have addressed it above.

$proc = Get-WmiObject -Query "select * from Win32_Process where name = 'System Idle Process'"
$proccputime = [TimeSpan]::FromSeconds(($proc.UserModeTime + $proc.KernelModeTime) / 10000000) #virtual CPU time, not real
Write-Host "Process seconds: $($proccputime.TotalSeconds)"
$virtuptime = [TimeSpan]([DateTime]::Now - [System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime))
# This next line assumes a 2nd core by adding the TimeSpan to itself
$virtuptime += $virtuptime
Write-Host "Total system uptime (seconds): $($virtuptime.TotalSeconds)"
$percentage = ($proccputime.TotalSeconds / $virtuptime.TotalSeconds) * 100
Write-Host "Process $($proc.Name) has used $percentage% of CPU time"

Posted in scripting, wmi | Tagged: , , , , , , , , , | Leave a Comment »