Exploring iTunes Using PowerShell Part 3: Connected Devices

Continuing on with my article on PoweShell and iTunes, I am now going to show you how to find out what devices are currently connected to iTunes. By connected, I mean that the device is actually showing up as being connected to iTunes to be able to sync, update or ejecting the device from iTunes.

First thing is first: connect to the iTunes COM object.

 
$itunes = New-Object  -ComObject iTunes.Application

Now that we have accomplished that, I also need to connect my iPhone to my computer so it actually shows up in iTunes. Once that has been done, we can start digging for information on the iPhone.

The “source” of what we are looking for just happens to reside in a property call…wait for it…sources.

 
$Sources = @($itunes.Sources)
$Sources

image

Note: I have to cast the results as an array using @() when dealing with COM objects, otherwise it will be treated as a single item in the collection.

In this case, the iPhone is easily distinguished by its name. But what if the device had another name or there were more sources than just these 3? We need to look at the kind property as the means to filter our the sources. Fortunately, looking through the SDK allowed me to find the enum to map the integer to what kind of source I am dealing with.

enum
ITSourceKind {
ITSourceKindUnknown = 0,
ITSourceKindLibrary,
ITSourceKindIPod,
ITSourceKindAudioCD,
ITSourceKindMP3CD,
ITSourceKindDevice,
ITSourceKindRadioTuner,
ITSourceKindSharedLibrary
}

This can easily be made into a hash table.

 
$SourceKind = @{
    Unknown = 0
    Library = 1
    iPhone = 2
    CD = 3
    MP3 = 4
    Device = 5
    Radio = 6
    SharedLibrary = 7
}

Now I can filter out the sources to find my actualy iDevice.

 
#Need Kind -eq 2
$iPhone = $Sources | Where {
    $_.Kind -eq $SourceKind.iPhone
}

#View the actual iPhone device
$iPhone

image

Here I can see the size of the of device as well as how much free space is left. I can also locate the version of the iOS (8.1 in this case). So this is pretty neat, but I wonder what methods are available for me to use?

 
$iPhone | Get-Member -MemberType Method

image

Each of these does what you would expect them to:

  • EjectIpod ejects the iDevice from the computer
  • UpdateIpod attempts to update the software on the iDevice, assuming that is what you want to happen.

In this case, I won’t be demoing these two methods as they are pretty straightforward and require no additional parameters to the methods.

I can take a look at my playlists and see what I have.

 
@($iPhone.Playlists) | 
Select Name, Time, @{L='SizeGB';E={"{0:N}" -f ($_.Size/1GB)}} | 
Format-Table –AutoSize

image

I’m curious as to what I have for Podcasts on my iPhone currently. I know that under playlists, it is the third index in the collection.

 
@($iPhone.Playlists)[3]

image

Now let’s see what all is in here.

 
@(@($iPhone.Playlists)[3].Tracks) | 
Select-Object Name, Album, Artist, DateAdded, 
@{L='SizeGB';E={"{0:N}" -f ($_.Size/1GB)}}, Genre, Description, Time

image

Pretty cool! I won’t dive much more into playlists as I am going to spend more time with that at a later date. But here you can see what I have on my iPhone currently.

Unfortunately, there really isn’t a lot I can uncover using the COM object about my device, at least not publicly (the SDK doesn’t even mention anything else). But if I happen to find anything else out, I will be sure to write about it!

This entry was posted in powershell and tagged , , , . Bookmark the permalink.

1 Response to Exploring iTunes Using PowerShell Part 3: Connected Devices

  1. Ryan says:

    Hi, thanks for the interesting info.
    I’m currently using a simple VBS script to control scheduled iTunes startup and I would love it if you had any insight on setting independent volume levels for several Airplay speakers.
    The SoundVolume function works fine, but only as a global volume setting it would appear(?)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s