Friday, October 2, 2009

Some cool PowerShell scripts

So I don't have that much to post for today. I've been developing some more PowerShell script to work with my OSD tasks, but they aren't finished yet, so I'd rather not put them up for the world to see yet.

What I do have however are 3 scripts that I'm working on idea's of how to use with SCCM, and then one that I've used throughout my TS already.

The one that I got working and is now entirely integrated into my TS is a PowerShell script that modifies Windows registry keys. Here's an example script. This script copies the i386 directory from my Windows source files that are on the Y: drive to the local C: drive. It then access the registry to redirect the i386 path to C:\. If you want to poke at the registry yourself, you can open PowerShell, then type "cd registry::" and then view the child items. If all you need to do is modify certain registry keys and you don't care to poke, the script it below. Should be fairly simple to figure out what you'll need to change.

Copy and Redirect i386
#Copies the I386 directory from the XP Source files to C
$local1 = "Y:\i386"
$local2 = "C:\"
copy-item $local1 $local2 -recurse

# redirect the I386 location
$i386 = "registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion"
cd $i386
set-itemproperty $i386 -name "SourcePath" -value "C:\I386"

The next couple scripts are sample ones. I'm still working on some idea's on how I can use them. They are adding a network printer, create an event in the event log, and retrieve information from the event log, all of which using PowerShell of course. I'm thinking somehow I can have my TS add items to the event log, and then at the end retrieve information to include in the email it sends the desktop technician making the technician aware of any problems that might have poped up. That way instead of making the task sequence fail all together because a certain task failed, or have it continue on error and then the tech no notice it missed something I can have it continue on error but put something in the event log, and then note that in the email it sends at the end. There's potential.

Add a network printer
$PrinterPath = "\\server\printer"

$net = new-Object -com WScript.Network
$net.AddWindowsPrinterConnection($PrinterPath)

Add to the event log
 # Writing an event
$EventLog = New-Object System.Diagnostics.EventLog('Application')
$EventLog.MachineName = "."
$EventLog.Source = "SCCM OSD"
$EventLog.WriteEntry("Script Failed","Error", $EventID)

 

# viewing overloads
($myevent.WriteEntry).OverloadDefinitions

 

# Listing enums
#[enum]::getvalues([system.diagnostics.eventlogentrytype])
# unpound above line to get additional values

Retrieve information from the event log
# PowerShell script to list the event logs.
get-Eventlog -list


# PowerShell script to find Error messages in the System eventlog.
get-EventLog system -newest 2000 | where {$_.entryType -match "Error"}

# Cmdlet to find latest 2000 errors in the System eventlog
$SysEvent = get-EventLog -logname system -newest 2000
$SysError = $SysEvent |where {$_.entryType -match "Error"}
$SysError | sort eventid | `
Format-Table EventID, Source, TimeWritten, Message -auto

Note that the retrieving from the event log script hasn't been tested. I haven't had time to mess with that yet, and can't  justify spending much time on it until I have an idea on some way of using this ability. 

This week I should be heading up to another company I administer to work on some stuff. One of the things on the "to-do" list is to get the SCCM environment up there to where they can re-image computer by themselves. The big thing there is that they have to be able to do it themselves since I'm only up there about once every three months, and that's normally on weekends. I'll also be installing .NET 3.5 and PowerShell v2 on all the machines so my current and future scripts will work. There will prolly be plenty posted on here after this weekend if I make it up there. 

I think I'll end with the following link. I haven't tried this (because I wouldn't want to) but this download claims to block Expert-Exchage search results from coming up when you search using Google in FireFox. For me, this would be stupid to do since I have an enterprise account with Experts Exchange, but I can understand why someone without an account might want to block them. 

Enjoy 



No comments: