::::::::: PowerShell :::::::::
Sunday, March 26, 2006
  [MSH] How to change a Process's Priority
There are a lot of posts about how to get processes' name and priorities but I haven't been able to find ways to change a process's priority easily.

Suppose that you are (un)zipping a large file and would like to change the priority of the operation set to "High" to finish the task quickly. One can simply just fire up a Task Manager and set the priority of the process manually with mouse clicks.

But why do so when there is a way to do it with few keystrokes...

If you want to change the process of zip operation you can simply do something like
#RealTime is generally not recommended...(NOTE: One-liner)
gps -p zip.exe | foreach { $_.PriorityClass = "RealTime" }

where valid values for "PriorityClass" can be retrieved through the following function
MSH>[Enum]::getNames("Diagnostics.ProcessPriorityClass")
Normal
Idle
High
RealTime
BelowNormal
AboveNormal


But the problem with using "foreach" statement in the next pipe is that, when pipe more than one process into the foreach statement, all of them will have their priority set to "RealTime". I don't like the following solution but just to be on the safe side, you can do the following

# Apply the process priority only to the first input object(NOTE: One-liner)
gps -processname "process_name" | &{ $input.moveNext(); $input.Current.PriorityClass = "RealTime" }

Basically you are setting the priority of the first passed pipe object(represented by $input)


You might as well create a function to change the priority more easily

function set-ProcessPriority { 
param($processName = $(throw "Enter process name"), $priority = "Normal")

get-process -processname $processname | foreach { $_.PriorityClass = $priority }
write-host "`"$($processName)`"'s priority is set to `"$($priority)`""
}



# When zip is running
MSH> set-ProcessPriority zip "High"
"zip"'s priority is set to "High"

# When zip operation is over,("Normal" is the default priority level)
MSH set-ProcessPriority zip
"zip"'s priority is set to "Normal"


To actually see what the priory level is the for given process just to make sure
MSH> gps zip | select name,priorityclass | ft -auto

Name PriorityClass
---- -------------
zip Normal


By the way, I am sure that there could be easier ways to change process priorities. I would love it if anyone can suggest other ways...


Tags :
 
Sunday, March 12, 2006
  [MSH] display and extract zip contents
In the previous post, I have talked about how to display zip contents of a file.
I have realized that ,while writing extraction script(out-ZipContent), it'd be better to convert the script into a snapin than using a simple script to support variety of features(such as extracting zip file contents into different folders) as well as using different Zip libraries(moreover, it's hard for me to use library functions Monadishly...)
So I will try to convert these scripts into Cmdlets later on and post the source on somewhere on GotDotNet...


For now, I have separated retrieving ZipStream object into another function called "Get-ZipStream", displaying logic into "Get-ZipContent" and extracting contents into "Out-ZipContent".

Well, I don't want to go too deep into usages and stuff but here is how cmdlets can be used.

To Display zip contents
MSH>get-zipstream .\zipFile.zip | get-zipContent

To Extract zip contents
MSH>get-zipstream .\zipFile.zip | out-zipContent


Get-ZipStream: Returns ZipStream

function Get-ZipStream {
param($zipFileName = $(throw "Enter Zip File Name"))

# Load J# library
# used "out-null" instead of "[void]" since "out-null" would not display
# loaded assembly even if there was an exception thrown
[System.Reflection.Assembly]::LoadWithPartialName("vjslib") | out-null

# 1) Open "java.io.FileInputStream" for a zip file
$fis = new-object java.io.FileInputStream($zipFileName)
# 2) Pipe File Input stream object to "java.io.ZipInputStream"
$zis = new-object java.util.zip.ZipInputStream($fis)

trap {
if ($zis -ne $null) { $zis.close() }
if ($fis -ne $null) { $fis.close() }
break
}

# Return the zip stream
$zis
}






Get-ZipContent: Displays Zip contents

function Get-ZipContent {
if ($args[0] -eq $null) {
# Retrive only the first value
$input.moveNext() | out-null
$zipStream = $input.get_Current()
}

# Load J# library
# used "out-null" instead of "[void]" since "out-null" would not display
# loaded assembly even if there was an exception thrown
[System.Reflection.Assembly]::LoadWithPartialName("vjslib") | out-null

trap { break; }

while (($ze = $zipStream.getNextEntry()) -ne $null) {
$ze.toString()
}

# Close "ZipInputStream" first and then "FileInputStream"
$zipStream.close()
remove-variable zipStream
}





Out-ZipContent: Extracts zip file content

function Out-ZipContent {
if ($args[0] -eq $null) {
$input.MoveNext()
$zis = $input.get_Current()
} else {
$zis = $args[0]
}

trap { break; }

[System.Reflection.Assembly]::LoadWithPartialName("vjslib") | out-null

while (($ze = $zis.getNextEntry()) -ne $null) {
$fn = $ze.getName()
$idx = $fn.LastIndexOf("/")
Write-Host "`$fn = $($fn)"

# we need create a directory
if ($idx -gt 1) {
$dir = $fn.SubString(0, $idx)
$dir = "$((pwd).path)\$($dir)"

# Since the directory doesn't exist, we now create it
if ( !(test-path $dir -type container) ) {
new-item -path $dir -type directory
Write-host "Created $($dir)"
}
}

$file = "$((pwd).path)\$($fn)"
# Write only for leaf files
if ( !(test-path $file -type container) ) {
[sbyte[]]$buf = [sbyte[]](@(0..127) * 8)
$fos = new-object Java.io.fileOutputStream($file)

while ( ($len = $zis.read($buf)) -ge 0 ) {
$fos.Write($buf, 0, $len)
}; $fos.close()
Write-Host "Extracted $($file)"
}
}
}



Tags :
 
Let's get lazy with PowerShell!

Name:
Location: Flushing, NY, United States

Experimenting with a different format of blogs...

Links
ARCHIVES
10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /


Powered by Blogger