Thursday, February 28, 2013

Facebook: Disable Notification Sound

Facebook recently implemented sounds when you receive a new notification.  It's akin to the Sonic the Hedgehog game checkpoint sound.  Click on the screenshot and follow the steps to disable this new sound.

Sunday, February 24, 2013

PowerShell: Get-Hash to filter file duplicates

You can find duplicate files and filter out one copy of each set into a new directory, leaving behind all the copies.  This is possible thanks to the PowerShell Community Extension modules PSCX get-hash cmdlet.

(Note: I've created a more extensive script utilizing get-hash and taglib.  Click here for a more comprehensive music filter technique)

1. Download the PSCX zipped folder and copy it's contents into your PowerShell modules folder ($env:psmodulepath).  I used version 3.0 to run in PowerShell 3.0.

2. Import-module pscx; careful using help.  I've had my shell lock up several times when asking for command help -- no matter the command. Run update-help to fix this problem.

3.  Go to a populated folder and type get-hash .\*.* to ensure the module and get-hash command are working.  You'll see path and hashstring objects for each file.

4.  Create a destination folder for your filtered files (i.e. e:\test\hash)

5. Run the following command from the root of the folder structure you wish to examine.

getchild-item -file -recurse|get-hash|sort hashstring -unique|%{move $_.path e:\test\path}

#BTW the -file option for GCI is new for PowerShell 3.0 otherwise you'd
#have to put in the |?{!$_.psiscontainer}| section after the gci to exclude
#subfolders

You now have one copy of each file in your new filtered folder.  All other copies are left in their original locations.

Obviously you can alter the command line to create subfolders, move based on various properties, etc., but basically your work is done.

If you have problems with duplicate file names when moving them to the new folder, you can have your filename duplicates create subfolders based on their hashstring, which will be unique, and be placed into them.

if ((gci -ea 0 e:\test\hash\($_.path('\')[-1]))){md -ea 0 e:\test\hash\$_.hashstring;move $_.path e:\test\hash\$_.hashstring}

PowerShell: Create contractions or input apostrophes

If you've ever needed to recreate contractions due to aprostrophe's being stripped from values or filenames, then read on:

Download my contractions.csv file (Thank you Enchanted Learning)

The contractions.csv has three columns to suit your needs.  Column one is the root words used to create the contraction.  Column two is the contraction without the apostrophe.  Column three is the resulting contraction.

$contractions = import-csv "c:\powershell\contractions.csv" -header ("Words","NoApostrophe","Contraction")

# I've assigned $file with a file name missing an apostrophe

$file = gci "it s a gas".txt

#Now we read each row of the CSV file searching for the word combination needed 
#to add an apostrophe to the name.  Once found, assign a new filename variable, save the file
#with the new name and break out of the loop


foreach($c in $contractions){if ($file.name -like ("*" + $c.noapostroph
e + "*")){
$newfile = $file.name.replace($c.noapostrophe,$c.contraction);ren $file $newfile;break}}


#to rename a variable string value:



$songtitle = "it s a gas"



foreach($c in $contractions){if ($songtitle -like ("*" + $c.noapostrophe + "*")){$songtitle = $songtitle -replace ($c.noapostrophe,$c.contraction);break}}


Seeding the Universe: Jason Silva

I am constantly amazed by this man's recollection of his readings. He quotes paragraphs and intertwines them with his visions epitomizing a euphoric video experience that shouts "Epiphany!" Brilliant man. The amount of tangents they must have talked about during this hike.

Gerontology: Aubrey de Grey discussing his latest research


PowerShell: ID3 Tag Editing via Taglib-sharp.dll and Discogs API


First of all, I've been searching for a good way to read, manipulate, then write values to the ID3 and other extended file information properties.  TagLib is perfect.  It integrates easily into scripts and doesn't have any problems other than trying to read a wrongly-named MP3 file.  It'll throw an error and I haven't found a way of suppressing or sending the error value to a variable other than to set $erroractionpreference = ignore, which sets the action for the whole script.

(Note: I've created a new script using taglib and get-hash to filter out duplicate files.  Click here)

Getting started:
Download the latest taglib: http://download.banshee.fm/taglib-sharp/















Download the most current version and look for the taglib-sharp.dll file in the libraries folder.















Windows 7+ has security protecting the system from "foreign" dlls.  Right-click on the taglib-sharp.dll file and click on the "unblock" button.  This will allow you to load the file into PowerShell.



#Create a variable for the tag-lib dll file

$taglib = "C:\PowerShell\taglib\libraries\taglib-sharp.dll"

#Load it into Powershell

[system.reflection.assembly]::loadfile($taglib)

#Find an MP3 and either assign it to a variable or put it's name into the create field


$media = [taglib.file]::create("e:\test\body.mp3")

#Now you can view, edit, and save ID3 information

PS E:\test> $media.properties
Codecs          : {TagLib.Mpeg.AudioHeader}
Duration        : 00:05:12.3120000
MediaTypes      : Audio
Description     : MPEG Version 1 Audio, Layer 3
AudioBitrate    : 128
AudioSampleRate : 44100
BitsPerSample   : 0
AudioChannels   : 2
VideoWidth      : 0
VideoHeight     : 0
PhotoWidth      : 0
PhotoHeight     : 0
PhotoQuality    : 0


Compare Taglib field entries to Windows Explorers Detail tab:
PS E:\test> $media.tag
StartTag                   : TagLib.NonContainer.StartTag
EndTag                     : TagLib.NonContainer.EndTag
TagTypes                   : Id3v1, Id3v2
Tags                       : {, }
Title                      : Body
Performers                 : {Bush}
PerformersSort             : {}
AlbumArtistsSort           : {}
AlbumArtists               : {Bush}
Composers                  : {Gavin Rossdale}
ComposersSort              : {}
TitleSort                  :
AlbumSort                  :
Album                      : Sixteen Stone
Comment                    :
Genres                     : {Alternative}
Year                       : 1994
Track                      : 6
TrackCount                 : 0
Disc                       : 1
DiscCount                  : 1
Lyrics                     :
Grouping                   :
BeatsPerMinute             : 0
Conductor                  :
Copyright                  :
MusicBrainzArtistId        :
MusicBrainzReleaseId       :
MusicBrainzReleaseArtistId :
MusicBrainzTrackId         :
MusicBrainzDiscId          :
MusicIpId                  :
AmazonId                   :
MusicBrainzReleaseStatus   :
MusicBrainzReleaseType     :
MusicBrainzReleaseCountry  :
Pictures                   : {}
IsEmpty                    : False
Artists                    : {Bush}
FirstArtist                : Bush
FirstAlbumArtist           : Bush
FirstAlbumArtistSort       :
FirstPerformer             : Bush
FirstPerformerSort         :
FirstComposerSort          :
FirstComposer              : Gavin Rossdale
FirstGenre                 : Alternative
JoinedArtists              : Bush
JoinedAlbumArtists         : Bush
JoinedPerformers           : Bush
JoinedPerformersSort       :
JoinedComposers            : Gavin Rossdale
JoinedGenres               : Alternative

PS E:\test> $media.properties.duration
Days              : 0
Hours             : 0
Minutes           : 5
Seconds           : 12
Milliseconds      : 312
Ticks             : 3123120000
TotalDays         : 0.00361472222222222
TotalHours        : 0.0867533333333333
TotalMinutes      : 5.2052
TotalSeconds      : 312.312
TotalMilliseconds : 312312

#An example of how I load variables from my music files

$filename = $file.basename
$fileextension = $file.name.split('.')[1]
$filetitle = $media.tag.title
$fileperformers = $media.tag.performers
$filealbumartists = $media.tag.albumartists
$filealbum = $media.tag.album
$filegenres = $media.tag.genres
$fileyear = $media.tag.year
$filetrack = $media.tag.track
$filetrackcount = $media.tag.trackcount
$fileaudiobitrate = $media.properties.audiobitrate
$fileconductor = $media.tag.conductor
$filecomposers = $media.tag.Composers
$fileBPM = $media.tag.BeatsPerMinute

$filedurationminutes = $media.properties.duration.minutes
$filedurationseconds = $media.properties.duration.seconds
$filedurationtotalseconds = $media.properties.duration.totalseconds


#Here's a way to clean the title tag. It cleans the annoying buggy brackets
#then it restricts all characters except the ranges inside the brackets
#Lifted clip explaining some regex expressions:


# Expression:
# ([0-9a-zA-Z\.]+) --> Gets all chars, numbers and '.'. Discard comma and others.


if ($filetitle){
$filetitle = $filetitle -replace ("\[","")
$filetitle = $filetitle -replace ("\]","")
$filetitle = $filetitle -replace ("[^0-9a-zA-Z-&\']"," ")}


#inserting Album Photo:
#(requires PowerShell 3.0 for this album photo catching section)
#To populate the picture tag, I call upon Discogs API via Invoke-Restmethod
#Please refer to the Discogs developer pages for information how to search for your music
#In this example, I used the weighted results and select the first hit for my album search

$filealbum = $media.tag.album
$apialbum = $filealbum -replace (' ','%20')
$albumfound = (invoke-restmethod http://api.discogs.com/database/search?title=$apialbum).results[0]
$thumb = $currentfolder + "\" + $albumfound.thumb.split('/')[-1]
invoke-webrequest -uri $albumfound.thumb -outfile $thumb
$pic = $pic + [taglib.picture]::createfrompath("$thumb")

#writing back to the file

$media.tag.title = [string]$filetitle
$media.tag.performers = [string]$fileperformers
$media.tag.albumartists = [string]$filealbumartists
$media.tag.album = [string]$filealbum
$media.tag.genres = [string]$filegenres
$media.tag.year = $fileyear
$media.tag.track = [string]$filetrack
$media.tag.trackcount = [string]$filetrackcount
$media.tag.conductor = [string]$fileconductor
$media.tag.composers = [string]$filecomposers
$media.tag.BeatsPerMinute = [string]$filebpm
$media.tag.pictures = $pic
$media.save()

References:
http://www.powershell.nu/2009/09/04/scripting-mp3-metadata-through-powershell/
http://vallery.net/2012/04/27/organizing-your-music-with-powershell/







Monday, February 18, 2013

PowerShell: Invoke-RestMethod, API's, and your music collection

Gathering ID3 tag information for your music files has become easier by embracing PowerShell 3's Invoke-RestMethod (IRM) command.  Many online databases use Application Programming Interfaces (API's) for developers to access their information.  These API's used to use XML but now have moved to the JSON language. IRM embraces JSON and allows us to create these API connections and query information about our music files.  In this article, I'm accessing Discogs Creative Commons open-source database.
Original Extended File Information
Updated after IRM and Discog's API

In my earlier article, I explained how to access shell.application in order to retrieve the extended file properties we use to rename and organize our files.  Combined with this method, we'll grab the extended file information and use it to find the correct file information and insert it into our files.  One point to make is that the API wants exact query information.  You'll receive no information if searching for an artist name of Quean instead of Queen.

 Syntax for Discogs allows us to search for song titles, artist names or IDs, album names and their release data plus much more.  The following demonstration script is fragile and incomplete.  It's to show how we embrace the power of PowerShell 3's Invoke-RestMethod.  Become familiar with IRM and API's to pull in online information into your scripts. It'll enhance the CURRENT information you're capable of accessing.  I'm really liking PS3's capabilities.

Disclaimer: Use this script at your own risk!  You'll be accessing Discogs live database and manipulating your computers music files.  This script uses the Windows 7 extended file information numbers.

##Start Script

$shell = new-object -com shell.application
$dirname = (get-item .\).fullname

#A quick and dirty way to find a song to use.  Run your script from your music folder

gi .\*.mp3
write "Type the exact (case-sensitive) song title you wish to query"
$fileinput read-host
$filename = gi .\$fileinput.mp3|?{(!($_.psiscontainer))}|foreach-object{$_.name}
$shellfolder = $shell.namespace($dirname).parsename($filename)

#now you can retrieve the extended file information and use those objects for your queries

$songtitle = $shell.namespace($dirname).getdetailsof($shellfolder,21)
$contribartist = $shell.namespace($dirname).getdetailsof($shellfolder,13)
$albumartist = $shell.namespace($dirname).getdetailsof($shellfolder,217)
$album = $shell.namespace($dirname).getdetailsof($shellfolder,14)
$genre = $shell.namespace($dirname).getdetailsof($shellfolder,16)
$year = $shell.namespace($dirname).getdetailsof($shellfolder,15)
$track = $shell.namespace($dirname).getdetailsof($shellfolder,26)
$length = $shell.namespace($dirname).getdetailsof($shellfolder,27)
$bitrate = $shell.namespace($dirname).getdetailsof($shellfolder,28)
$conductors = $shell.namespace($dirname).getdetailsof($shellfolder,17)
$bpm = $shell.namespace($dirname).getdetailsof($shellfolder,218)
$composers = $shell.namespace($dirname).getdetailsof($shellfolder,219)

#An example of how to replace spaces for your online queries. %20 is the variable used for space.

$apicontribartist = $contribartist -replace (' ','%20')
$apisongtitle = $songtitle -replace (' ','%20')

#Every artist in their database has an ID.  We query to find the ID.

$artistid = (irm http://api.discogs.com/artist/$apicontribartist).resp.artist.id

#We use the Artist ID to find the song's original release date.  Master is the keyword for original recording.

if ((invoke-restmethod http://api.discogs.com/database/search?artist=$artistid"&"type=master"&"title=$songtitle).results){
$origdate = ((invoke-restmethod http://api.discogs.com/database/search?artist=$artistid"&"type=master"&"title=$songtitle).results.resource_url|select -unique -first 1|%{invoke-restmethod $_}).year}else{
$origdate = ((invoke-restmethod http://api.discogs.com/database/search?artist=$apicontribartist"&"track=$songtitle).results.year)|sort|select -first 1}

#Locate Original Album hosting the audio track by using the 
#artist ID, releases, master, and date.

$master = ((irm http://api.discogs.com/artists/$artistid/releases).releases|?{$_.year -eq $origdate -and $_.type -eq "Master"})
$master = $master[0]
$origalbum = (irm $master.resource_url).title

#Now we can use the master album the song appeared on to find additional song information
#We find track number, released on Vinyl, 45, CD, etc., and I like to combine the styles and genres 
#& is a reserved PowerShell character.  We wrap it "&" so it will be passed to Discogs.

$origtracknumber = 1 + (((irm ((irm http://api.discogs.com/database/search?artist=$apicontribartist"&"title=$origalbum"&"track=$songtitle"&"year=$origdate"&"type=master).results).resource_url).tracklist|?{$_.title -eq $songtitle}).position.count)
$origformat = ((irm http://api.discogs.com/database/search?artist=$apicontribartist"&"title=$origalbum"&"track=$songtitle"&"year=$origdate"&"type=master).results)
$origformat = ((irm http://api.discogs.com/database/search?artist=$apicontribartist"&"title=$origalbum"&"track=$songtitle"&"year=$origdate"&"type=master).results).label|select -first 1
$origstyles = (irm ((irm http://api.discogs.com/database/search?artist=$apicontribartist"&"title=$origalbum"&"track=$songtitle"&"year=$origdate"&"type=master).results).resource_url).styles
$origgenres = (irm ((irm http://api.discogs.com/database/search?artist=$apicontribartist"&"title=$origalbum"&"track=$songtitle"&"year=$origdate"&"type=master).results).resource_url).genres

#I combined the styles and genre information so I can create better
#future genre searches.

$origgenre = ($origstyles + $origgenres)|sort

#write the updates back to the original file
#Try taglib for inserting tags back into your audio file.
#Here's a site which goes over some of its abilities.
#For Windows 7, when you download taglib, find the taglib-sharp.dll
#file, right-click it, select properties, and "unblock" it so you can load 
#the DLL into your ref.assembly command

$taglib = "c:\powershell\taglib\libraries\taglib-sharp.dll"
[system.reflection.assembly]::loadfile($taglib)
$tags = [taglib.file]::create($filename)

#type $tags to see the file properties
#type $tags.tag to see the extended file properties
#I'll let you take it from here :-)
##End Script




Friday, February 15, 2013

PowerShell: Clean & Manage Music Files

Disclaimer: USE THIS AT YOUR OWN RISK! This script is made for Windows 7!  There will be file renaming and moving so TEST, TEST, TEST.  You can test this script by copying one folder of music into an empty folder then run the script in that folder.  It will only process the files in that folder.  If all goes well, all files will move into a new folder structure named Music on your desktop.

To run this or any other PowerShell script, you'll have to read up on the Get-Executionpolicy and Set-Executionpolicy as PowerShell disables scripts by default.

My previous article explained how to clean the extra "stuff" added to music file names.  This article further cleans those file names, gets rid of the troublesome [brackets], then uses the Genre, Artist, and Album information to create a new folder structure on your desktop.  You can then move it to wherever you store your music.


Here is an example of some files my friend wanted help managing:

Notice the brackets, curly brackets, and other special characters in the file names and properties.


This is the same folder of music files after running the script:
I performed a search for all files to display the name changes.  Notice the subfolder structure that each file is located in.  The left side is the Genre structure created based on the file information.

Three more notes before going through the script:  

  1. The script automatically ignores subfolders.  I strongly recommend you perform this move one folder at a time so you can ensure it worked properly.
  2. The script will process any type of file.  You'll have to create an -exclude in the top $filename variable if you want it to ignore JPG's, AVI's, and etc.
  3. Do not save this script to the folder you are processing. It will be moved into the Music folder and could cause problems.
## Start of Script

#We call the Shell Object

$shell = new-object -com shell.application

#Organized Music Folder Location
#You can change the location. The Music Root Folder defaults to your desktop

$MusicFolder = ($home + '\desktop\music')

#remove brackets from filenames per PowerShells bug with brackets

gci .\* |?{(!($_.psiscontainer))}|foreach{move -literalpath $_ ($_.name -replace ('\[|]','~'))}

#Set directory and file variables.  Add -exclude to the Get-Item if you want 
#to ignore other filetypes
#For instance: (get-item .\* -exclude *.avi,*.jpg)

$dirname = (get-item .\).fullname
$filename = (get-item .\*)|?{(!($_.psiscontainer))}|foreach-object{$_.name}

foreach($file in $filename){

#original filename holder, then split it to capture the extension and name only.

$fileholder = $file
$ext = ('.' + ($file.split('.')[-1]))
$noext = $file.trimend($ext)

#get Title (item number 21) property from the file

$shellfolder = $shell.namespace($dirname).parsename($file)
$title = $shell.namespace($dirname).getdetailsof($shellfolder,21)

#PowerShell filename bug for brackets. 
#If there's a bracket in the title, $title will equal nul

if ($title -like "*``[*" -or $title -like "*``]*"){$title = $nul}

#if the title isn't empty, replace the filename with it

if ($title -gt 0){$file = ($title + $ext)}

#clean extraneous characters from the filename
#BTW, let me know if you have a better way to filter files
#for now, I borrowed this filter technique:

$file2 = $file -replace ('^[0-100]','')
$file3 = $file2 -replace ('.mp33','.mp3')
$file4 = $file3 -replace (' - ','-')
$file5 = $file4 -replace ('   ',' ')
$file6 = $file5 -replace ('  ',' ')
$file7 = $file6 -replace ('\(','')
$file8 = $file7 -replace (':','')
$file9 = $file8 -replace ('\)','')
$file10 = $file9 -replace ('\/','')
$file11 = $file10 -replace ('\>','')
$file12 = $file11 -replace ('\<','')

#Split the filename at the hyphen and only keep the last portion
#since that's normally the song name

$file13 = $file12.split('-')[-1]

$finalfilename = $file13

#rename the file after all the modifications

ren -erroraction silentlycontinue -path ($dirname + '\' + $fileholder) -newname $finalfilename

$shellfolder = $shell.namespace($dirname).parsename($finalfilename)

#Filtering for Artist - Items 13 and 217

$contribartist = $shell.namespace($dirname).getdetailsof($shellfolder,13)
$albumartist = $shell.namespace($dirname).getdetailsof($shellfolder,217)

#filtering the Album Name - Item 14

$album = $shell.namespace($dirname).getdetailsof($shellfolder,14)
$album1 = $album -replace ('^[0-100]','')
$album2 = $album1 -replace ('.mp33','.mp3')
$album3 = $album2 -replace (' - ','-')
$album4 = $album3 -replace ('   ',' ')
$album5 = $album4 -replace ('  ',' ')
$album6 = $album5 -replace ('\(','')
$album7 = $album6 -replace (':','')
$album8 = $album7 -replace ('\)','')
$album9 = $album8 -replace ('\/','')
$album10 = $album9 -replace ('\>','')
$album11 = $album10 -replace ('\<','')

$finalalbumname = $album11

#filtering the genre name - Item 16

$genre = $shell.namespace($dirname).getdetailsof($shellfolder,16)
$genre1 = $genre -replace ('^[0-100]','')
$genre2 = $genre1 -replace ('.mp33','.mp3')
$genre3 = $genre2 -replace (' - ','-')
$genre4 = $genre3 -replace ('   ',' ')
$genre5 = $genre4 -replace ('  ',' ')
$genre6 = $genre5 -replace ('\(','')
$genre7 = $genre6 -replace (':','')
$genre8 = $genre7 -replace ('\)','')
$genre9 = $genre8 -replace ('\/','')
$genre10 = $genre9 -replace ('\>','')
$genre11 = $genre10 -replace ('\<','')

$finalgenrename = $genre11

#Deciding between Album and Contributing artist then cleaning the name

$artist = if ($contribartist -gt 0){$contribartist}else{$albumartist}
$artist1 = $artist -replace ('^[0-100]','')
$artist2 = $artist1 -replace ('.mp33','.mp3')
$artist3 = $artist2 -replace (' - ','-')
$artist4 = $artist3 -replace ('   ',' ')
$artist5 = $artist4 -replace ('  ',' ')
$artist6 = $artist5 -replace ('\(','')
$artist7 = $artist6 -replace (':','')
$artist8 = $artist7 -replace ('\)','')
$artist9 = $artist8 -replace ('\/','')
$artist10 = $artist9 -replace ('\>','')
$artist11 = $artist10 -replace ('\<','')

$finalartistname = $artist11

$filetrim = $finalfilename.trimend($ext)
$filedupeinsert = ($filetrim + 'DUPE')
$filedupe = ($filedupeinsert + $ext)


#move file based on its Genre, Artist, and Album

#Replacing Brackets for tildes

if ($finalgenrename -like "*``[*" -or $finalgenrename -like "*``]*"){$finalgenrename -replace ('\[|]','~')}
if ($finalalbumname -like "*``[*" -or $finalalbumname -like "*``]*"){$finalalbumname -replace ('\[|]','~')}
if ($finalartistname -like "*``[*" -or $finalartistname -like "*``]*"){$finalartistname -replace ('\[|]','~')}

#moving songs to folders

#If there's no Genre entry, a default is used
#For the sake of endless genre possibilities, I used this filter to group
#the various types.  Manipulate as you see fit:

if ($finalgenrename -like "*Rock*"){$finalgenrename = "Rock"}
if ($finalgenrename -like "*Alt*"){$finalgenrename = "Alternative"}
if ($finalgenrename -like "*Metal*"){$finalgenrename = "Metal"}
if ($finalgenrename -like "*Pop*"){$finalgenrename = "Pop"}
if ($finalgenrename -like "*Rap*"){$finalgenrename = "Rap"}
if ($finalgenrename -like "*Hip*"){$finalgenrename = "Hip-Hop"}
if ($finalgenrename -like "*R&B*"){$finalgenrename = "R&B"}

if (!($finalgenrename -gt 0)){$finalgenrename = "Genre"}

#If there's no Album entry, a default is used

if (!($finalalbumname -gt 0)){$finalalbumname = "Album"}

#Creating the desktop\music folder structure
#and moving files to their new folders

if (!(dir $MusicFolder\$finalgenrename -erroraction silentlycontinue)){md $MusicFolder\$finalgenrename -force}
if (!(dir $MusicFolder\$finalgenrename\$finalartistname -erroraction silentlycontinue)){md $MusicFolder\$finalgenrename\$finalartistname -force}
if (!(dir $MusicFolder\$finalgenrename\$finalartistname\$finalalbumname -erroraction silentlycontinue)){md $MusicFolder\$finalgenrename\$finalartistname\$finalalbumname -force}
if (dir $MusicFolder\$finalgenrename\$finalartistname\$finalalbumname\$finalfilename -erroraction silentlycontinue){move -erroraction silentlycontinue ($dirname + '\' + $finalfilename) -destination $MusicFolder\$finalgenrename\$finalartistname\$finalalbumname\$filedupe}else{move -erroraction silentlycontinue ($dirname + '\' + $finalfilename) $MusicFolder\$finalgenrename\$finalartistname\$finalalbumname\$finalfilename -force}
}

## End of Script

Click Here to view the list of Windows 7 file properties you can access via shell.application.


Monday, February 11, 2013

PowerShell: Creating & Deleting Outlook 2003-2010 Appointments

I wanted to help my finance department set up meeting reminders that most staff need on their calendars.  We couldn't find a way to create an Outlook Recurring Event since the dates varied from month to month.  For lack of an easier way to automate the decimation of these meeting reminders via Outlook, I turned to a PowerShell solution.  I found several articles and two stood out: Richard Siddaway's Delete Items blog entry and the Hey, Scripting Guys Export Calendar blog.  Using bits from both as well as other locations, I created a simple way to add and delete Calendar Appointments.


Creating Outlook Calendar Appointments


#The first two lines connect to Outlook and prepare for the new item entries

$olAppointmentItem = 1 

$o = new-object -comobject outlook.application 

#Each new calendar appointment must have the CreateItem and Save lines

#--------One complete Calendar---------------
$a = $o.CreateItem($olAppointmentItem) 
  
$a.Start = "2/14/2013 8:00 AM" 
$a.Duration = 60 
$a.Subject = "Monthly Budget Meeting" 
$a.Body = "See Agenda Items at http://www.contoso.com" 
$a.Location = "Finance Section" 
$a.ReminderMinutesBeforeStart = 15 
$a.ReminderSet = $True 
  
$result = $a.Save() 
#--------Appointment Item Entry--------------

$a = $o.CreateItem($olAppointmentItem) 
  
$a.Start = "3/11/2013 8:00 AM" 
$a.Duration = 60 
$a.Subject = "Monthly Budget Meeting" 
$a.Body = "See Agenda Items at http://www.contoso.com" 
$a.Location = "Finance Section" 
$a.ReminderMinutesBeforeStart = 15 
$a.ReminderSet = $True 
  
$result = $a.Save() 

$a = $o.CreateItem($olAppointmentItem) 
  
$a.Start = "4/18/2013 8:00 AM" 
$a.Duration = 60 
$a.Subject = "Monthly Budget Meeting" 
$a.Body = "See Agenda Items at http://www.contoso.com" 
$a.Location = "Finance Section" 
$a.ReminderMinutesBeforeStart = 15 
$a.ReminderSet = $True 
  
$result = $a.Save() 

#End of Script, you've now added three appointments. 
#Expand to suit your needs and add/delete $a. fields as needed.


Deleting Outlook Calendar Appointments


#Connect to Outlooks Calendar Folder System

Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)

#Now you can search for Calendar Objects just like any other type
#In the example below, I'm searching for entries with a 
#subject of Monthly Budget Meeting that starts as of the 
#current date andd time

$folder.items |?{$_.Subject -like "*Monthly Budget Meeting*" -and $_.Start -gt (get-date)}|%{$_.delete()}

#End of script.  Now you know how to search and 
#delete multiple appointments

Sunday, February 10, 2013

PowerShell: Cleaning up Music File Names

I've been helping a friend manage his plethora of digital music.  Anyone who's acquired music has noticed the many ways filenames have been created.  Since Windows can display extra columns of information that coincide with MP3 tags, he decided the song names should be the filenames as the files would stay in the Artist/Album folders.

Here's a sample of the files original names:

After much research, I found the Title tag was usually a cleaner file name source than the actual file name.  I also noticed that hyphens were used a bunch and the section after the last hyphen was the song's name.  I used these two findings combined with character filters to come up with this:

PowerShell versions 1 and 2 balk at the [brackets] in a filename unless you perform some trickery.  PowerShell 3 has overcome this limitation -- but I'm using PS 2.0 for this script.  The script is not perfect but at least gets you in the general area.  The script does NOT -recurse (trickle down subfolders) since it's best to check your progress one folder at a time.

Here is the script if you'd like to give it a shot.  Place the .PS1 file with your other scripts and run it from the music folder to be affected.


$shell = new-object -com shell.application
$dirname = (get-item .\).fullname
$filename = (get-item .\*)|foreach-object{$_.name}

foreach($file in $filename){

#original filename holder

$fileholder = $file

#get the songs Title (item number 21 (if it exists)) property from the file

$shellfolder = $shell.namespace($dirname).parsename($file)
$title = $shell.namespace($dirname).getdetailsof($shellfolder,21)

#if the title isn't empty, use it for the filename.
#Note: This is specifically for MP3's; you'd have to add other lines for 
#other music file extensions

if ($title -gt 0){$file = ($title + '.mp3')}

#clean extraneous characters from the filename
#experiment by adding filters which serve you best

$file2 = $file -replace ('^[0-100]','')
$file3 = $file2 -replace ('.mp33','.mp3')
$file4 = $file3 -replace (' - ','-')
$file5 = $file4 -replace ('   ',' ')
$file6 = $file5 -replace ('  ',' ')
$file7 = $file6 -replace ('\(','')
$file8 = $file7 -replace ('\)','')

#Split the filename at the hyphen and only keep the last portion
#since that's normally the song name. -1 means get the last one.

$file9 = $file8.split('-')[-1]

#rename the file after all the modifications

rename-item -path ($dirname + '\' + $fileholder) -newname $file9
}

Friday, February 8, 2013

PowerShell: Post-SysPrep scripts to Rename & Add Computer to Domain (using alternate credentials)


I tasked myself with the mission of using Microsoft's Windows 8 ADK to create a bootable Windows PE 4.0 UFD, and save a sysprepped workstation hard drive image (.wim) to the UFD. The UFD's WinPE boot sequence would have a PE-hooked batch file in which the user presses one button, walks away for 20 - 30 minutes and returns to a new PC which is on the Domain, in the correct OU, and has all its drivers loaded and GPO's applied.  The renaming and Domain adding portion of this process involved PowerShell's access to the win32_computersystem rename command and Add-Computer.

On my first attempt, I tried to rename the computer and add it to the domain during one boot. It just didn't work.  I had to reopen the pre-sysprep image and change the unattend.xml sysprep file's administrator login count from 1 to 2.  So the first boot checks then changes the computers name and places a RunOnce registry entry to kick off the second half of the process.  The second boot checks the computers name and if it is changed, grabs the IP address and starts a Add-Computer command associated with the OU of the corresponding IP subnet.

The PowerShell script during the first boot will create the computer name, add a regkey reboot hook, rename the computer, and restart it (it becomes passive during the second boot due to the computer name checks):

$ComputerName = ("Domain" + (gwmi win32_systemenclosure).SMBIOSAssetTag)

#A hook into the post-sysprep start script so it can be reran on next boot:

if ((gwmi win32_computersystem).name -ne $ComputerName){reg.exe add HKLM\Software\Microsoft\Windows\CurrentVersion\Runonce /v Restart /t REG_SZ /d "c:\scripts\start.bat" /f}

#Renaming the computer and restarting:

if ((gwmi win32_computersystem).name -ne $ComputerName){(gwmi win32_computersystem).rename($ComputerName);restart-computer}

#The second boot PowerShell script encrypts a domain user/pass, 
#determines the PC's IP subnet, then executes Add-Computer and 
#places the PC in the correct OU. Upon restart, kicks off GPUpdate at first Domain login.

#Saving the alternate Active Directory credentials:

$Username = "domain\delegateuser" 
$Password = convertto-securestring "secret" -asplaintext -force 
$Cred = new-object system.management.automation.pscredential($Username,$Password)

#Use ADSI to search for the computer object on the domain.  
#If one is found, add-computer with no OU is used.  If not then the script 
#finds the IP subnet and assigns the computers object to the appropriate OU


$searcher = [adsisearcher][adsi]""
$searcher.filter ="(cn=$ComputerName)"
$searchparm = $searcher.FindOne()
if (!($searchparm)){

#Checking the computer name then finding its IP subnet:

$ComputerName = ("Domain" + (gwmi win32_systemenclosure).SMBIOSAssetTag)
if ((gwmi win32_computersystem).name -ne $ComputerName){write "something went wrong with renaming the computer"|out-gr

idview;cmd /c pause}
$IP = [system.net.dns]::GetHostAddresses($env:computername)|?{$_.IPAddresstostring -like '192.168.*'}

#We've captured IP addresses to two octets and now find the 
#Active Directory Site via the third IP octet and perform the 
#add-computer command combined with the saved AD credentials and correct OU location:

if ($ip -like '192.168.0.*'){add-computer -domain MyDomain -Credential $cred -OUPath "OU=HQ,OU=WORKSTATIONS,DC=MyDomain,DC=COM"}
if ($ip -like '192.168.1.*'){add-computer -domain MyDomain -Credential $cred -OUPath "OU=Site1,OU=WORKSTATIONS,DC=MyDomain,DC=COM"}
if ($ip -like '192.168.2.*'){add-computer -domain MyDomain -Credential $cred -OUPath "OU=Site2,OU=WORKSTATIONS,DC=MyDomain,DC=COM"}

#closing the if statement concerning finding a computer object or not.
#The Else adds the computer to the domain and reattaching it to it's existing
#computer object

#Update 2/21/14
#I found it more efficient to take away the else statement and instead just create a catch-all:   add-computer -domain MyDomain -Credential $cred
#Another problem was re-imaging existing domain computers.  AD would try
#creating another GUID and cause GPO and other problems.  
#To fix this, I added:
start-process powershell.exe -credential $cred -argumentlist 'if (!(test-computersecurechannel)){test-computersecurechannel -repair}

#so if the computer is in the domain but not in one of the above mentioned subnets, it'll still be added to the domain.

}else{add-computer -domain MyDomain -Credential $cred}

#The computer is now a member of the Domain.  Next add a 
#Registry RunOnce key entry so the next boot will start a 
#forced application of GPUpdate relevant to it's OU:

reg.exe add HKLM\Software\Microsoft\Windows\CurrentVersion\Runonce /v GPUpdate /t REG_SZ /d "gpupdate /force" /f

Lastly, delete the scripts with passwords in them and restart the computer so it can change its network affiliation from WORKGROUP to the new Domain assignment:

reg.exe add HKLM\Software\Microsoft\Windows\CurrentVersion\Runonce /v DelPS1 /t REG_SZ /d "del c:\scripts*.ps1 /q" /f
restart-computer

This process works and has a positive side-effect: It stops the computer from joining the domain if the Computer Object already exists (which stops any sort of SID problems or unintended OU movements).

It's been suggested by a fellow Redditor that I should create a switch instead of the If statements breaking down the IP subnets.  I'll update this post after I've tested that portion.  Let me know if you have any questions, comments, or suggestions.