Sysadmins shortcuts

Sysadmins shortcuts

Dynamic Powershell Menu build on current files in directory

PowershellPosted by Thomas Petersen Mon, February 18, 2013 16:38:59
The main goal here is to automate patching of .wim files for Windows client/server installation, to save the initial updating after installing.

When updating your image, the installation will be running quite a lot faster, than if you would have to make the install, and then start patching - no rocket science here...

Usually there would be a few different images, so the menu will be based on the current .wim files located in the image directory.

Basically the files are read into an array, and an option for choosing all images and one for abotring is added.

So the menu part of the script looks like the following:

clear-host


#location of .wim files. Being read into the $wimfiles array

$wimfiles=Get-ChildItem c:\scripts *.wim


function ToArray

# typecasting single object to array

{

begin

{

$output = @();

}

process

{

$output += $_;

}

end

{

return ,$output;

}

}



#building menu

$i=0

Write-output "Please choose image(s) to update"

do {

write-host $i $wimfiles[$i].name

$i++

} while ($i -lt ($wimfiles.count))

write-host $i 'All images'

write-host ($i+1) 'Abort'


#read choice, and validate against number of files+2 (#files, All files, Abort)

do {$WimID = Read-host "Choice"}

while ((0..($wimfiles.count+1)) -notcontains $WimID)


if ($WimID -eq ($wimfiles.count+1)) {

write-output "Aborting..."

exit

}

elseif ($WimID -eq ($wimfiles.count)) {

# All wimfiles chosen, read files into $WimPatch array

$WimPatch = Get-ChildItem c:\scripts *.wim

}

Else {

# Single file chosen, read into $WimPatch and call function ToArray, to typecast the single file to an array

$WimPatch = Get-ChildItem c:\scripts $WimFiles[$WimID].Name | ToArray

}


write-host *******************

write-host "Image(s) to be updated:"

write-host

$i=0

do {

write-host $WimPatch[$i].Name

$i++

} while ($i -lt ($WimPatch.count))

#now we have chosen which file(s) to work on, and they are ready to be sent to the patch function

More to come later...

  • Comments(0)//www.ttp.dk/#post1

Debugging PowerShell

PowershellPosted by Thomas Petersen Mon, February 18, 2013 16:03:35
Ok, first entry smiley

When working with powershell, one of the first things you will need is to debug your script.

There is actually functionality for this build in right out of the box.

A very basic way of making this work could be like the following:

# Add the two lines, for basic debugging functionality

[CmdletBinding()]

param ($param)

# Ok, this is what is needed.

# When you run your script, you can add various debugging parameters, that make the following write snippets fire :

write-output "This is standard output"

write-debug "Debug mode enabled"

write-verbose "Verbose output enabled"

write-output "all done"

So to enable debug-mode, run your script with -debug as parameter, or you could use -verbose for, well, verbose output.

More powershell related stuff to be online shortly. Currently I am working on a setup to automate patching of .wim files for Windows installation.

  • Comments(0)//www.ttp.dk/#post0