Tag: Cmdlets

OneGet PowerShell

Windows Management Framework V5 Preview

I know there is a lot of cool stuff going on right now and Microsoft announced a lot of new things. Like Windows Phone 8.1, Cortana, Windows 8.1 Update, Visual Studio 2013 Update 2, Windows Universal Apps, the Nokia Lumia 630, 635 and 930 and much much more. But there just was another announcement from Jeffrey Snover which is really interesting for us automation and management guys. Today Microsoft announced the Windows Management Framework 5.0 Preview, which brings some new stuff to PowerShell and PowerShell Desired State Configuration (DSC).

In the release 5.0 of the Windows Management Framework Microsoft did some work on DSC to make it more stable and reliable by addressing bug fixes, performance improvements and general optimizations. But there are coming the new feature to Windows PowerShell.

Windows PowerShell OneGet

OneGet is a new way to discover and install software packages from around the web. With OneGet, you can:
  • Manage a list of software repositories in which packages can be searched, acquired, and installed
  • Search and filter your repositories to find the packages you need
  • Seamlessly install and uninstall packages from one or more repositories with a single PowerShell command

Network Switches

Microsoft will offere a new PowerShell module which brings the possibility to manage Network Switches. In this release Microsoft added a set of L2 Layer NetworkSwitch management PowerShell cmdlets to manage Certified for Windows network switches. This effort was part of the Data Center Abstraction (DAL) vision which was led by Microsoft working closely with industry leaders in this space such as: Arista, Cisco and Huawei. Using Windows Server 2012 R2, network switches that pass the Certified for Windows program can now be managed natively by System Center Virtual Machine Manager 2012 R2 (SCVMM) without the need to write custom plugins.

I think this is not just a small announcement, this is a pretty big one to me.

Get more Information on the Windows Server Blog about the Windows Management Framework V5 Preview.



Replace Diskpart with Windows PowerShell – Basic Storage cmdlets

Windows Server 8

Last week I made a blog post about how you can create a USB drive for Windows To Go. In my post I used diskpart.exe to format the USB drive. Now we don’t live in the stone age anymore, so I did the same with the new version of Windows PowerShell coming in Windows 8 and Windows Server 8.

Now here some basic cmdlets to do some simple storage operations, like clean a disk, create a partition and so on.

Lets start simple

List all disks

Get-Disk

get-disk

Now get all partitions

Get-Partition

get-partition

Now get all partitions of disk 0

Get-Partition -DiskNumber 0

get-partitiondisk

Clear a Disk

Get-Disk 1 | Clear-Disk -RemoveData

clear-disk

Create a new partition

New-Partition -DiskNumber 1 -UseMaximumSize

new-partition

Format this volume

Get-Partition -DiskNumber 1 -PartitionNumber 1 | Format-Volume -FileSystem NTFS

format-volume

Create new partition and format it with the label “USB”:

New-Partition -DiskNumber 1 -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel USB

format-volume1

Change driveletter

Set-Partition -DriveLetter E -NewDriveLetter T

set-partition

Set a partition active

Set-Partition -DriveLetter T -IsActive $true

set-partition1

Remove a partition

Remove-Partition -DriveLetter T

remove-partition

Bring a disk online

 Set-Disk 1 -isOffline $false 

Remove Readonly flag

 Set-Disk 1 -isReadOnly $false

Initialize Disk with GPT

 Initialize-Disk 1 -PartitionStyle GPT

online disk

This is some basic knowledge about the storage module in PowerShell v3. Lets see how we can change the commands from using diskpart to Windows PowerShell.

Diskpart.exe

select disk 1
clean
create partition primary
format fs=ntfs quick
active
assign letter=e

PowerShell:

Get-Disk 1 | Clear-Disk -RemoveData
New-Partition -DiskNumber 1 -UseMaximumSize -IsActive -DriveLetter E | Format-Volume -FileSystem NTFS -NewFileSystemLabel USB

replace-diskpart



Powershell iTunes v1

Powershell iTunesIn the last days I worked with Powershell and ComObjects. It turned out that iTunes is a really good exmaple for using ComObjects with Powershell, thats why I wrote this little Script.
Powershell iTunes is a Powershell script which lets you control iTunes over Powershell.

Howto:

  1. Download Powershellitunes.zip
  2. Extract the Files where ever you wanna save the powershell script
  3. Run the script
    Powershell iTunes
  4. Now you can run the Powershell Cmdlets

Powershell iTunes Commands:

  • Start-iTunes
  • Close-iTunes
  • Play-Track
  • Pause-Track
  • Resume-Track
  • Back-Track
  • Next-Track
  • Mute-Track
  • Unmute-Track
  • Show-iTunesVersion
  • Set-VolumeDown
  • Set-VolumeUp
  • Set-Volume

The Script:

#
#*****************************************************************
#
# Powershell iTunes v1 $Rev: 754 $
# (c) 2004-2010 Thomas Maurer. All rights reserved.
# created by Thomas Maurer
#
# https://www.thomasmaurer.ch
# https://www.thomasmaurer.ch/projects 
#
# last Update by $Author: tmaurer $ on $Date: 2010-11-24 07:50:06 +0100 (Mi, 24 Nov 2010) $
#
# Version 1.0.1
#
#*****************************************************************
#
 
# Welcome													
Write-Host "                                                                  " -BackgroundColor Black -ForegroundColor White
Write-Host "    ____                               __         ____            " -BackgroundColor Black -ForegroundColor White
Write-Host "   / __ \____ _      _____  __________/ /_  ___  / / /            " -BackgroundColor Black -ForegroundColor White
Write-Host "  / /_/ / __ \ | /| / / _ \/ ___/ ___/ __ \/ _ \/ / /             " -BackgroundColor Black -ForegroundColor White
Write-Host " / ____/ /_/ / |/ |/ /  __/ /  (__  ) / / /  __/ / /              " -BackgroundColor Black -ForegroundColor White
Write-Host "/_/    \____/|__/|__/\___/_/  /____/_/ /_/\___/_/_/               " -BackgroundColor Black -ForegroundColor White
Write-Host "                                                                  " -BackgroundColor Black -ForegroundColor White                    
Write-Host "    _ ______                                                      " -BackgroundColor Black -ForegroundColor White
Write-Host "   (_)_  __/_  ______  ___  _____                                 " -BackgroundColor Black -ForegroundColor White
Write-Host "  / / / / / / / / __ \/ _ \/ ___/                                 " -BackgroundColor Black -ForegroundColor White
Write-Host " / / / / / /_/ / / / /  __(__  )                                  " -BackgroundColor Black -ForegroundColor White
Write-Host "/_/ /_/  \__,_/_/ /_/\___/____/                                   " -BackgroundColor Black -ForegroundColor White                                 
Write-Host "                                                                  " -BackgroundColor Black -ForegroundColor White
Write-Host "******************************************************************" -BackgroundColor Black -ForegroundColor White
Write-Host "                                                                  " -BackgroundColor Black -ForegroundColor White
Write-Host " Welcome to the Powershell iTunes                                 " -BackgroundColor Black -ForegroundColor White
Write-Host "                                                                  " -BackgroundColor Black -ForegroundColor White
Write-Host " (c) 2010 Thomas Maurer www.thomasmaurer.ch/projects              " -BackgroundColor Black -ForegroundColor White
Write-Host "                                                                  " -BackgroundColor Black -ForegroundColor White
Write-Host "******************************************************************" -BackgroundColor Black -ForegroundColor White
Write-Host "                                                                  " -BackgroundColor Black -ForegroundColor White
Write-Host " for Help use the Get-PSiTunesHelp                                " -BackgroundColor Black -ForegroundColor White
Write-Host "                                                                  " -BackgroundColor Black -ForegroundColor White
 
# Add ComObjects to $iTunes
$iTunes = New-Object -ComObject iTunes.Application
 
 
# Function to start itunes
function Start-iTunes { 
    process {
 
		$iTunesRunning = Get-Process | Where-Object {$_.ProcessName -eq "iTunes"}
		if(!$iTunesRunning) {
			$script:iTunes = New-Object -ComObject iTunes.Application
			Write-Host "(PS iTunes): iTunes started" -BackgroundColor Black -ForegroundColor green
		} 
		else {
			Write-Host "(PS iTunes): iTunes allready running" -BackgroundColor Black -ForegroundColor red
		}
	}
}
 
# Function to close itunes
function Close-iTunes { 
    process {
 
		$iTunesRunning = Get-Process | Where-Object {$_.ProcessName -eq "iTunes"}
		if(!$iTunesRunning) {
			Write-Host "(PS iTunes): iTunes not running" -BackgroundColor Black -ForegroundColor red
		} 
		else {
			Stop-Process $iTunesRunning.Id
			Write-Host "(PS iTunes): iTunes closed" -BackgroundColor Black -ForegroundColor red
		}
	}
}
 
# Function to play a song
function Play-Track { 
    process {
		if ($iTunes.Playerstate -eq "0"){
			$iTunes.Play()
			Write-Host "(PS iTunes): Now playing:" $iTunes.CurrentTrack.Name "from" $iTunes.CurrentTrack.Artist -BackgroundColor Black -ForegroundColor green
		} 
		else {
			Write-Host "(PS iTunes): Already playing Track:" $iTunes.CurrentTrack.Name "from" $iTunes.CurrentTrack.Artist -BackgroundColor Black -ForegroundColor red
		}
	}
}
 
# Function to stop a song
function Pause-Track { 
    process {
        if ($iTunes.Playerstate -eq "1"){
			$iTunes.Pause()	
			Write-Host "(PS iTunes): iTunes paused" -BackgroundColor Black -ForegroundColor Red
		}
		else {
			Write-Host "(PS iTunes): Not playing any track" -BackgroundColor Black -ForegroundColor red
		}
	}
}
 
# Function to resume a song
function Resume-Track { 
    process {
    	if ($iTunes.Playerstate -eq "0"){ 
			$iTunes.Play()
			Write-Host "(PS iTunes): Now playing:" $iTunes.CurrentTrack.Name "from" $iTunes.CurrentTrack.Artist -BackgroundColor Black -ForegroundColor green
		}
		else {
			Write-Host "(PS iTunes): Already playing Track:" $iTunes.CurrentTrack.Name "from" $iTunes.CurrentTrack.Artist -BackgroundColor Black -ForegroundColor red
		}
	}
}
 
# Function to go a song back
function Back-Track { 
    process { 
	$iTunes.BackTrack()
	Write-Host "(PS iTunes): Now playing:" $iTunes.CurrentTrack.Name "from" $iTunes.CurrentTrack.Artist -BackgroundColor Black -ForegroundColor green	
	}
}
 
# Function to go to the next
function Next-Track { 
    process {   
	$iTunes.NextTrack()
	Write-Host "(PS iTunes): Now playing:" $iTunes.CurrentTrack.Name "from" $iTunes.CurrentTrack.Artist -BackgroundColor Black -ForegroundColor green	
	}
}
 
# Function to get infos about the current track
function Get-CurrentTrack { 
    process {    
	Write-Host "(PS iTunes): Currently playing:" $iTunes.CurrentTrack.Name "from" $iTunes.CurrentTrack.Artist -BackgroundColor Black -ForegroundColor White
	}
}
 
# Function to mute itunes
function Mute-iTunes { 
    process {
        if($itunes.Mute -eq $false) {
			$itunes.Mute = $true
			Write-Host "(PS iTunes): iTunes muted" -BackgroundColor Black -ForegroundColor Red
		}
		else {
			Write-Host "(PS iTunes): iTunes already muted" -BackgroundColor Black -ForegroundColor red
		}
	}
}
 
# Function to unmute itunes
function Unmute-iTunes { 
    process {
    	if($itunes.Mute -eq $true) {
			$itunes.Mute = $false
			Write-Host "(PS iTunes): iTunes unmuted" -BackgroundColor Black -ForegroundColor green
		}
		else {
			Write-Host "(PS iTunes): iTunes already unmuted" -BackgroundColor Black -ForegroundColor red
		}
	}
}
 
# Function to set iTunes volume up
function Set-VolumeUP { 
    process {
		if($iTunes.SoundVolume -lt 100) {
			$iTunes.SoundVolume = $iTunes.SoundVolume + 2
			Write-Host "(PS iTunes): iTunes Volume" $iTunes.SoundVolume -BackgroundColor Black -ForegroundColor green	
		}	
		else {
			Write-Host "(PS iTunes): iTunes Volume is already 100%" -BackgroundColor Black -ForegroundColor red
		}	
	}
}
 
# Function to set iTunes volume down
function Set-VolumeDown { 
    process {
		if($iTunes.SoundVolume -gt 0) {
			$iTunes.SoundVolume = $iTunes.SoundVolume - 2
			Write-Host "(PS iTunes): iTunes Volume" $iTunes.SoundVolume -BackgroundColor Black -ForegroundColor red	
		}	
		else {
			Write-Host "(PS iTunes): iTunes Volume is already 0%" -BackgroundColor Black -ForegroundColor red
		}
	}
}
 
# Function to set iTunes
function Set-Volume { 
	param (
		[int]$Volume 
	)
    process {
		if($Volume -ge 0 -and $Volume -le 100) {
			$iTunes.SoundVolume = $Volume
			Write-Host "(PS iTunes): iTunes Volume" $iTunes.SoundVolume -BackgroundColor Black -ForegroundColor green	
		}	
		else {
			Write-Host "(PS iTunes): Volume has to be 0-100" -BackgroundColor Black -ForegroundColor red
		}
	}
}
 
# Function to show the Version of iTunes
function Show-iTunesVersion { 
    process {
	Write-Host "(PS iTunes): iTunes Version" $iTunes.version -BackgroundColor Black -ForegroundColor White	
	}
}
 
# Function to show the Version of iTunes
function get-PSiTunesHelp { 
    process {
	Write-Host " "
	Write-Host " Powershell iTunes Help"
	Write-Host " "	
	Write-Host "********************************************************"	
	Write-Host " "
	Write-Host " Commands"
	Write-Host " "
	Write-Host " Start-iTunes			Starts iTunes"
	Write-Host " Close-iTunes			Closes iTunes"
	Write-Host " Play-Track			Plays a iTunes Track"
	Write-Host " Pause-Track			Pauses a iTunes Track"
	Write-Host " Resume-Track			Resumes a iTunes Track"
	Write-Host " Back-Track			Go to the last iTunes Track"
	Write-Host " Next-Track			Plays next iTunes Track"
	Write-Host " Mute-Track			Mutes iTunes"
	Write-Host " Unmute-Track			Unmutes iTunes"	
	Write-Host " Show-iTunesVersion		Shows iTunes Version"
	Write-Host " Set-VolumeDown			Set Volume Down"
	Write-Host " Set-VolumeUp			Set Volume Up"
	Write-Host " Set-Volume			Set iTunes Volume (0-100)"
	}
}

I released this here: https://www.thomasmaurer.ch/projects/powershell-itunes/