Wednesday, August 12, 2009

2009 Summer Scripting Games – Beginner Event 3

What a pleasant surprise! Came into work this morning and saw that @makovec sent me a note indicating that this mornings Scripting Guys Blog referenced one of my submissions posted during the 2009 Summer Scripting Games!

The posting sited the use of the “undocumented” parameter –delimiter.

Their post is here.

It isn’t very often that Stahler and elegant are used in the same sentence…

Wednesday, August 5, 2009

Removing Header/Footer lines from CSV files

I was recently tasked with scheduling a script to read in a TSM.out file and create a usable CSV file from it. The TSM *.out file has at least 9 header lines that are not relevant to the end file as well as some extra footer lines. I also noticed that the column headers are not descriptive either. So, to automate the file transformation, we look to PowerShell!
function Create-CSV {
param( [int]$HeaderLines,
[int]$FooterLines,
[string]$SourceFilePath,
[string]$DestinationFilePath,
[string]$NewColumnNames )

if(Test-Path -literalPath $SourceFilePath) {
$a = Get-Content -path $SourceFilePath
if ($NewColumnNames -ne $null) {
Add-Content -path $DestinationFilePath `
-value $NewColumnNames
}
# Grab only the lines we need
$a[$HeaderLines..($a.count - $FooterLines)] | `
foreach{Add-Content -path $DestinationFilePath -value $_}
}
else {
Write-Error "$SourceFilePath does not exist."
}
} # End of function

Create-CSV -HeaderLines 9 `
-FooterLines 4 `
-SourceFilePath 'c:\temp\test.out' `
-DestinationFilePath 'c:\temp\test.csv' `
-NewColumnNames "Nodename,hostname,tcpipaddress"

Enjoy!

Monday, July 27, 2009

Central Ohio PowerShell Users Group

Please join us for our first Central Ohio PowerShell Users Group on Thursday, July 30th, 2009 at the Ohio State University Medical Center. Jeffery Hicks, Microsoft MVP will be joining us! For more information, visit the Central Ohio PowerShell Users Group site.

Thursday, June 4, 2009

Searching for files remotely

We are looking to possibly centralize the management of our OPT files. Thought maybe a quick script in PowerShell would do the trick. Ran the following line:

Get-childitem \\server1-vp01\c$ -Include *.opt -Recurse

and waited, waited, waited...... 6 minutes later the console displayed my results.
Knowing a little bit about WMI, I decided to approach this from a different angle:
Get-WmiObject `
-class CIM_DATAFile `
-computername 'server-vp01' `
-filter "extension='opt' and drive='c:'"
This gave me back the results in 2.73 seconds!

Comparing the times generated the following:
measure-command {`
get-childitem \\server-vp01\c$ `
-Include *.opt `
-Recurse}
Days              : 0
Hours :
0
Minutes :
6
Seconds :
3
Milliseconds :
476
Ticks :
3634762131
TotalDays : 0.00420690061458333
TotalHours : 0.10096561475
TotalMinutes : 6.057936885
TotalSeconds : 363.4762131
TotalMilliseconds : 363476.2131
measure-command {`
Get-WmiObject `
-class CIM_DATAFile `
-computername 'server-vp01' `
-filter "extension='opt' and drive='c:'"}

Days :
0
Hours :
0
Minutes :
0
Seconds :
2
Milliseconds :
739
Ticks :
27390035
TotalDays : 3.17014293981481E
-05
TotalHours : 0.000760834305555556
TotalMinutes : 0.0456500583333333
TotalSeconds : 2.7390035
TotalMilliseconds : 2739.0035
Pretty obvious which method to use.
Once again, PowerShell and WMI save the day.

Wednesday, June 3, 2009

PowerShell Formatting Error

I was looking throught the PowerGUI forum yesterday when I saw a post similar to this:
$processes = Get-Process -name m*
$drives = Get-Wmiobject `
-class win32_logicaldisk `
-Filter "DriveType=3"

$processes
$driveinfo | Format-Table -autosize

When you run this code in an editor, you are likely to get the following error:
out-lineoutput : Object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not legal or not in the correct sequence. This is likely caused by a user-specified "format-tabl e" command which is conflicting with the default formatting.

After a few minutes of research, I see that this has been registered on Microsoft Connect as a bug. A quick, easy fix is to pass the $processes object down the pipline to Out-Default. ( $processes | Out-Default )

Hope this helps!

Sunday, May 31, 2009

TechED 2009 (Pics)

Had a great time at TechED. I met a lot of great folks including quite a few from the PowerScript community. Following are a few pics from TechED - specifically the Birds of a Feather Session that Hal and Steven hosted (Practical PowerShell: Best practices from the field - Check out the recorded podcast of the session!).


Hal Rottenberg, PowerShell MVP & Steven Murawski hosting the BOF.


Hal fielding some questions.


Kirk Munro, PowerShell MVP (http://poshoholic.com/)


From left to right, Steven Murawski, the back of Ed Wilson (1/2 of the Scripting Guys) and John Merrill (IT content evangelist and publishing manager in the Windows Server and Solutions Division User Assistance group).


Steven Murawski getting ready to work the PowerShell booth.


Ed Wilson evangelizing the merits of PowerShell.


Had a great time at the TechED, especially at the BOF and at the PowerShell Dinner.

Thursday, May 28, 2009

File Migration with PowerShell

Was recently tasked with assisting our file migration project. Until the following script (assumes you are using Quest AD Commandlets), this was a manual process.
$Users = get-content -path c:\users.txt
foreach ($User in $Users) {
$SourceFolder = "\\HumanResources\vol10\Users\$User"
$NewFolderName = "$SourceFolder-Migrated"

$homeDir = "\\personal-p01\users$\" + $User.substring(0,1) + "\$User"
Copy-Item $SourceFolder -Destination $homeDir -Recurse

Set-QADUser
$User -ObjectAttributes @{'HomeDirectory'=$homeDir; 'HomeDrive'= 'P:'}
$rule=new-object System.Security.AccessControl.FileSystemAccessRule("OSUMC\$User","FullControl","Allow")

foreach ($file in $(Get-ChildItem $homeDir -recurse)) {
$acl=get-acl $file.FullName
$acl.SetAccessRule($rule)
set-acl $File.Fullname $acl
}
# set the acl on the root folder
set-acl $homeDir $acl

# Rename-item doesn't work, so copy and delete
Copy-item $SourceFolder -Destination "$SourceFolder-migrated" -Recurse
Remove-Item $SourceFolder -Recurse -Force
}
Enjoy!