Internet-radio built in powershell

SRLast weekend I had an hour or two over after my kids gone to sleep, to do some scripting at home. I got an idea of building an internet radio in powershell with the four nationwide Swedish public service channels.  I used the .NET framework class System.Windows.Media.MediaPlayer to play the streams and then some variables to feed it. First of you could only choose channel when you ran the function and the only option was quit, but I built in a meny so you will be able to change channel without quiting the function. When I proudly showed the internet radio player to my girlfriend she told me it looked like crap and asked why I just don´t go to the sr.se site…

function SR-Play{
 param 
 ([parameter(mandatory)]
 [ValidateSet("P1","P2","P3","P4")]
 [String]$Radiostation)

 $P1 = "http://sverigesradio.se/topsy/direkt/132-hi-mp3.m3u"
 $P2 = "http://sverigesradio.se/topsy/direkt/2562-hi-mp3.m3u"
 $P3 = "http://sverigesradio.se/topsy/direkt/164-hi-mp3.m3u"
 $P4 = "http://sverigesradio.se/topsy/direkt/179-hi-mp3.m3u"
 
$MediaPlayer = New-Object System.Windows.Media.MediaPlayer 

switch ($Radiostation)
{
    "P1" {$MediaPlayer.Open([uri]$p1)}
    "P2" {$MediaPlayer.Open([uri]$p2)}
    "P3" {$MediaPlayer.Open([uri]$p3)}
    "P4" {$MediaPlayer.Open([uri]$p4)}
}
$MediaPlayer.play()
Start-Sleep -Seconds 1

    do {
    [int]$meny = 0

    while ( $meny -lt 1 -or $meny -gt 9) {
    write-host "Kanalmeny, välj kanal eller avsluta."
    Write-Host "1. P1"
    Write-Host "2. P2"
    Write-Host "3. P3"
    Write-Host "4. P4"
    Write-Host "9. Avsluta"

    [int]$meny = Read-Host "Välj en kanal"

    switch ($meny) {
      1{Write-Host "P1"
      $MediaPlayer.Open([uri]$p1)
$MediaPlayer.play()}
      2{Write-Host "p2"
      $MediaPlayer.Open([uri]$p2)
$MediaPlayer.play()}
      3{Write-Host "p3"
      $MediaPlayer.Open([uri]$p3)
$MediaPlayer.play()}
      4{Write-host "p4"
      $MediaPlayer.Open([uri]$p4)
$MediaPlayer.play()}
      9{Write-Host "Hejdå"}
      default {Write-Host "Fel val"}
    }
  }
  
    }
until ("9" -ccontains $meny
)
 if ($meny -eq "9")
    {$MediaPlayer.stop()
    $MediaPlayer.close()
     }
}

List and count users that never logged on to domain

neverloggedin

I got an urgent case from the boss, we had to compare a list of users from our meta-catalog with users in our AD and list and count which users who never logged on to domain. I imported the csv in an variable and then used an foreach-loop to check if the users had logged in or not. We also wanted to list and count all users that where included in the csv-file but not in the active driectory, I used the ErrorVariable to do that and the appended each entry to a text-file with the out-file append switch. As you can see I used two different techniques to export multiple data from the foreach-loop. The first one when I exported the users that hadn´t logged on, where I first created an variable with an empty array  “$users = @()” , then instead of running the command to get the users I used the + to fill the array with result “$users +=”, on the second I used the out-file as I explained above.  When the lists where done I used the count and length property.

 

#--------------------------------------------------------------------
#NAME:UsersNeverLogon.ps1
#AUTHOR: Viktor Lindström
#
#COMMENTS: List and count users accounts that never loged on.
#It also lists and counts users that exist in our meta-catalog but
#not in our Active directory
#--------------------------------------------------------------------

#Import from CSV that contains usesrs samaccountname under header accountname.
$import = Import-Csv "C:\temp\acc.txt"

#Creates an empty array
$users = @()

#foreach loop to check all users.
foreach ($anv in $import)
{
#Checks if user dont have the lastLogonTimestamp-attribute set, and if it isn´t set it appends users samaccountname in the array. If the user dosn´t exist in the AD it puts one error code-line in an variable.
$users += get-aduser $anv.accountName -ErrorAction SilentlyContinue -ErrorVariable err | Get-ADObject -Properties samaccountname,"lastLogonTimestamp" | where "lastLogonTimestamp" -eq $null | select samaccountname 
$err | Out-File -FilePath "C:\temp\users_not_in_AD.txt" -Append
}
#Export users to CSV and counts the users.
$users | export-csv -Path c:\temp\users_never_logged_in.txt -NoTypeInformation
Write-Host Number of users who have never logged in: $users.count
$dontexist = Get-Content C:\temp\users_not_in_AD.txt
Write-Host Number of users that are not in active directory $dontexist.Length