Post from August, 2009

INI File PowerShell Functions

Friday, 7. August 2009 19:50

Though XML is around for years now, I still stumble upon those old school INI files. Inspired by a small piece of code that I found recently at stackoverflow.com I have built a small PowerShell library that provides the following functions:

  • Import-Ini - read an INI file’s entire content into memory, structured as hashtable
  • Export-Ini - write the hashtable back to file
  • Remove-IniCategory - remove a category from an INI file
  • Remove-IniKey - remove a key from an INI file
  • Get-IniKey - return value of an INI file key
  • Set-IniKey - set a key in an INI file
# $Date: 2009-10-04 08:48:59 +0200 (So, 04 Okt 2009) $
# $Rev: 1 $
# $Author: fpschultze $
# $URL: http://fpschultze.unfuddle.com/svn/fpschultze_ps-ini-file-functions/IniFileFunctions.ps1 $

Function Import-Ini (
	[string]$Path = $(Read-Host "Please supply a value for the Path parameter")
)
{
	$ini = @{}
	if (Test-Path -Path $Path)
	{
		switch -regex -file $Path
		{
			"^\[(.+)\]$"
			{
				$Category = $matches[1]
				$ini.$Category = @{}
			}
			"(.+)=(.+)"
			{
				$Key,$Value = $matches[1..2]
				$ini.$Category.$Key = $Value
			}
		}
	}
	else
	{
		Write-Host "File not found - $Path"
	}
	$ini
}

Function Export-Ini (
	[hashtable]$inputObject = $(Read-Host "Supply a value for the inputObject parameter"),
	[string]$Path = $(Read-Host "Supply a value for the Path parameter")
)
{
	$Content = @()
	ForEach ($Category in $inputObject.Keys)
	{
		$Content += "[$Category]"
		ForEach ($Key in $inputObject.$Category.Keys)
		{
			$Content += "$Key=$($inputObject.$Category.$Key)"
		}
	}
	$Content | Set-Content $Path -Force
}

Function Remove-IniCategory (
	[string]$Path = $(Read-Host -Prompt "Supply a value for the Path parameter"),
	[string]$Category = $(Read-Host -Prompt "Supply a value for the Category parameter")
)
{
	$ini = Import-Ini -Path $Path
	if ($ini.Count)
	{
		if ($ini.Contains($Category))
		{
			$ini.Remove($Category)
			Export-Ini -inputObject $ini -Path $Path
		}
		else
		{
			Write-Host "Category $Category does not exist in $Path"
		}
	}
}

Function Remove-IniKey (
	[string]$Path = $(Read-Host -Prompt "Supply a value for the Path parameter"),
	[string]$Category = $(Read-Host -Prompt "Supply a value for the Category parameter"),
	[string]$Key = $(Read-Host -Prompt "Supply a value for the Key parameter")
)
{
	$ini = Import-Ini -Path $Path
	if ($ini.Count)
	{
		if ($ini.Contains($Category))
		{
			if ($ini.$Category.Contains($Key))
			{
				$ini.$Category.Remove($Key)
				Export-Ini -inputObject $ini -Path $Path
			}
			else
			{
				Write-Host "Key $Key does not exist in $Path, category $Category"
			}
		}
		else
		{
			Write-Host "Category $Category does not exist in $Path"
		}
	}
}

Function Get-IniKey (
	[string]$Path = $(Read-Host -Prompt "Supply a value for the Path parameter"),
	[string]$Category = $(Read-Host -Prompt "Supply a value for the Category parameter"),
	[string]$Key = $(Read-Host -Prompt "Supply a value for the Key parameter")
)
{
	$ini = Import-Ini -Path $Path
	if ($ini.Count)
	{
		if ($ini.Contains($Category))
		{
			if ($ini.$Category.Contains($Key))
			{
				$ini.$Category.$Key
			}
			else
			{
				Write-Host "Key $Key does not exist in $Path, category $Category"
			}
		}
		else
		{
			Write-Host "Category $Category does not exist in $Path"
		}
	}
}

Function Set-IniKey (
	[string]$Path = $(Read-Host -Prompt "Supply a value for the Path parameter"),
	[string]$Category = $(Read-Host -Prompt "Supply a value for the Category parameter"),
	[string]$Key = $(Read-Host -Prompt "Supply a value for the Key parameter"),
	[string]$Value = $(Read-Host -Prompt "Supply a value for the Value parameter"),
	[switch]$Force
)
{
	$ini = Import-Ini -Path $Path
	if ($ini.Count)
	{
		if (!($ini.Contains($Category)))
		{
			if ($Force)
			{
				$ini.$Category = @{}
			}
			else
			{
				Write-Host "Category $Category does not exist in $Path"
				return
			}
		}
		if ($ini.$Category.Contains($Key))
		{
			if (!$Force)
			{
				Write-Host "Key $Key already exists in $Path, category $Category"
				return
			}
		}
		$ini.$Category.$Key = $Value
		Export-Ini -inputObject $ini -Path $Path
	}
}

Category:Scripting, Windows PowerShell | Comment (0) | Author: Frank-Peter

Small PowerShell function library for Word2007

Thursday, 6. August 2009 20:59

I created a small library of functions for creating word documents using PowerShell. The library contains 8 functions that allow you to create simple documents in Word2007 (maybe also Word 2003 but I haven’t been able to test it yet).

The functions are:

  1. WORD\Invoke-Word
  2. WORD\Add-Heading
  3. WORD\Add-Paragraph
  4. WORD\Add-Table
  5. WORD\Add-TableOfContents
  6. WORD\Add-PageBreak
  7. WORD\Add-TextBox
  8. WORD\Update-TablesOfContents

A small example script is included in the zip file which can be downloaded here.

A screenshot of one of the chapter generated by the example script:

 

word

Category:Office 2007, PowerShell | Comment (0) | Author: Dennis Damen

Issues with PowerShell 2 CTP(v3) and Word2007

Thursday, 6. August 2009 10:43

At Login Consultants I’m using a simple script to automatically document our scripting framework. After some reports of my colleagues about this script not working on Windows 7 I decided to start testing. Here’s what I found:

 

PowerShell v1.0:

posh1

 

PowerShell CTP 2.0 (V3):

posh2

Anyone got some idea what is going on?

Category:Gotcha, Office 2007, PowerShell | Comment (0) | Author: Dennis Damen