Trevor Sullivan's Tech Room

Minding the gap between administration and development

Posts Tagged ‘framework’

PowerShell: Shortening Web Service Type Names with a Hashtable

Posted by Trevor Sullivan on 2011/07/13

When you use the New-WebServiceProxy class, you probably have noticed that PowerShell dynamically generates some really ugly type names. For example, if we get a reference to the Bing web service (you’ll need to get an API key first):

$BingSearch = New-WebServiceProxy -Class BingSearch -Uri "$ApiKey"

… and examine the types contained within it:

$BingSearch.GetType().Assembly.GetExportedTypes() | select FullName

… you’ll notice some ridiculously long type names based on your API key, such as:


Read the rest of this entry »


Posted in powershell, scripting | Tagged: , , , , , , , , , , , , , , , , | 2 Comments »

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 »