# Author: DBMwSIt is still not complete since, it does not list a zip file inside a given zip file name(no recursive listing)
# Date: 02/27/2006 @ 04:13 PM EST
# Description: Retrieve a Zip file's content
# (either display only files inside the zip file or return ZipEntry object)
# Note: This function requires J# Library, "vjslib"
# Usage: get-ZipContent file_name [[System.Bool] -toString]
# Reference: http://codeproject.com/csharp/VmEasyZipUnZip.asp
# Params
# 1) $zipFileName: Path of zip file
# 2) $toString: return name of zip content, otherwise return ZipEntry
function Get-ZipContent {
param($zipFileName = $(throw "Enter Zip File Name"), [bool]$toString = $true)
# 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)
# 3) While there exists a content in a zip input stream, get a content of type "ZipEntry"
while (($ze = $zis.getNextEntry()) -ne $null) {
if ($toString) {
# Pass string value to pipe
Write-Object $ze.ToString()
} else {
# Pass actualy ZipEntry object to pipe
$ze
}
}
# 4) Close "ZipInputStream" first and then "FileInputStream"
$zis.close()
$fis.close()
trap {
if ($zis -ne $null) { $zis.close() }
if ($fis -ne $null) { $fis.close() }
break
}
}
MSH>ls | where {!$_.MshIsContainer -and $_.name -like "*.zip"} | foreach { get-zipcontent $_.fullname } | where { $_ -like "*.jpg" }
$xml = [xml]"<xml><title>some title</title><name>some name</name></xml>"
$xml
From here on, a variable named $arr is visible in "parse-file.msh".Runspace rs = RunspaceFactory.CreateRunspace();
rs.SessionStateProxy.SetVariable("arr", new ArrayList());
/*** snip ***/
string cmd = ". .\\parse-file.msh";
Pipeline pl = rs.CreatePipeline(cmd);
MSH>function bar {
>> $arr = new-object System.Collections.ArrayList
>> $arr.Add(1)
>> $arr.Add(2)
>> $arr.Add(3)
>>
>> return $arr
>> }
>>
MSH>bar
0
1
2
1
2
3
MSH>function multiple-returns {
>> $arr = new-object System.Collections.ArrayList
>> $arr.Add(5)
>> $arr.Add(6)
>> $arr.Add(7)
>> return $arr}
>>
MSH>multiple-returns
0
1
2
5
6
7
MSH>$a, $b, $c, $d, $e = multiple-returns
MSH>$a
0
MSH>$b
1
MSH>$c
2
MSH>$d
5
6
7
MSH>$e
MSH>$d.getType().Name
ArrayList
MSH>$e.getType().Name
You cannot call a method on a null-valued expression.
At line:1 char:11
+ $e.getType( <<<< ).Name
MSH>[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
GAC Version Location
--- ------- --------
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e08...
MSH>[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
MSH>[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
MSH>[System.Reflection.Assembly]::LoadWithPartialName("system.windows.forms") >$null
MSH>
MSH>function bar {
>> $arr = new-object System.Collections.ArrayList
>> [void]$arr.Add(1)
>> [void]$arr.Add(2)
>> [void]$arr.Add(3)
>>
>> return $arr
>> }
>>
MSH>bar
1
2
3MSH>function bar {
>> $arr = new-object System.Collections.ArrayList
>> $arr.Add(1) | out-null
>> $arr.Add(2) | out-null
>> $arr.Add(3) | out-null
>>
>> return $arr
>> }
>>
MSH>bar
1
2
3
Where,
This command also supports only one ubiquitous parameter: -Verbose(-vb)
print-image [-FileName|-fn] System.String [[-Printer|-p|-pn] System.String] [[-Landscape|-l|-ls] System.Boolean]
MSH>[reflection.assembly]::loadwithpartialname("system.drawing")
GAC Version Location
--- ------- --------
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\Syst...
MSH>[Drawing.Printing.PrinterSettings]::InstalledPrinters
Microsoft Office Document Image Writer
HP LaserJet 8150 PCL 5e
HP LaserJet 5000 Series PCL 5e
HP Color LaserJet 5500 PCL6(Color)
Adobe PDF
MSH>
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.ComponentModel;
using System.Management.Automation;
using System.Collections.Generic;
using System.Text;
namespace D2D.Snapins
{
/// <summary>
/// Prints image files
/// </summary>
/// <remarks>
/// @TODO: Display Preview form
/// </remarks>
[RunInstaller(true)]
public class PrintImageSnapin : MshSnapIn
{
public override string Description
{
get { return "Prints an image file"; }
}
public override string Name
{
get { return "D2D_print-image"; }
}
public override string Vendor
{
get { return "DontBotherMeWithSpam"; }
}
}
[Cmdlet("print", "image")]
public class PrintImageCommand : MshCmdlet
{
#region Private Member Variables
/// <summary>
/// Fully qualified image file name to print
/// </summary>
private string fileName;
/// <summary>
/// Name of printer to use
/// </summary>
private string printer;
/// <summary>
/// Should we use landscape mode or not
/// </summary>
private bool landscape;
/// <summary>
/// Responsible for printing image document
/// </summary>
private PrintDocument doc;
/// <summary>
/// Image to constructed from input file name
/// </summary>
private Bitmap img;
/// <summary>
/// Holds exception thrown in functions other than
/// "BeginProcessing", "EndProcessing", "ProcessRecord"
/// </summary>
private Exception ex;
#endregion
#region MSH Parameters
/// <summary>
/// Fully qualified image file name to print
/// </summary>
[Alias("fn")]
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true,
HelpMessage = "Fully qualified image file name to print")]
public string FileName
{
get { return this.fileName; }
set { this.fileName = value; }
}
/// <summary>
/// Name of printer to use
/// </summary>
[Alias("p", "pn")]
[Parameter(Mandatory = false, Position = 1, ValueFromPipeline = true,
HelpMessage = "Name of printer to print image")]
public string Printer
{
get { return this.printer; }
set { this.printer = value; }
}
[Alias("l", "ls")]
[Parameter(Mandatory = false, Position = 2, ValueFromPipeline = false,
HelpMessage = "Toggle Landscape mode")]
[DefaultValue(false)]
public bool Landscape
{
get { return this.landscape; }
set { this.landscape = value; }
}
#endregion
protected override void EndProcessing()
{
this.doc = new PrintDocument();
this.doc.BeginPrint += new PrintEventHandler(doc_BeginPrint);
this.doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
this.doc.EndPrint += new PrintEventHandler(doc_EndPrint);
this.doc.Print();
}
#region PrintDocument Event Handlers
/// <summary>
/// Construct image to print
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void doc_BeginPrint(object sender, PrintEventArgs e)
{
// Construct image to print
try
{
this.img = new Bitmap(this.FileName);
this.doc.DocumentName = Path.GetFileName(this.FileName);
this.doc.DefaultPageSettings.Landscape = this.Landscape;
if (this.Printer != "")
{
this.doc.PrinterSettings.PrinterName = this.Printer;
}
}
catch (Exception ex)
{
// We have failed create an image from the given file name
// No need to process any further, actually...
/// How do i abort the script at this point?
ErrorRecord err = new ErrorRecord(ex, "doc_BeginPrintError",
ErrorCategory.InvalidData, this.fileName);
WriteError(err);
e.Cancel = true; // abort printing job
}
//e.PrintAction = this.PrintAction;
e.Cancel = false;
WriteVerbose(string.Format(
"==================== {0} ====================",
this.doc.DocumentName));
}
/// <summary>
/// Print constructed image according to PrintAction
/// </summary>
private void doc_PrintPage(object sender, PrintPageEventArgs e)
{
WriteVerbose(string.Format("Printing {0}",
this.doc.DocumentName));
Graphics g = e.Graphics;
Rectangle paperBounds = e.MarginBounds;
SizeF adjSize = AdjustImageSize(this.img.Size, paperBounds);
if (this.ex == null)
{
// Calculate source and destination sizes
RectangleF destRec = new RectangleF(paperBounds.Location, adjSize);
RectangleF srcRec = new RectangleF(0, 0, img.Width, img.Height);
// Print to according to "PrintAction"
g.DrawImage(this.img, destRec, srcRec, GraphicsUnit.Pixel);
}
else
{
ErrorRecord err = new ErrorRecord(this.ex, "AdjustImageSizeError",
ErrorCategory.NotSpecified, null);
WriteError(err);
}
// Nothing else to print...
e.HasMorePages = false;
}
/// <summary>
/// Clean up used resources
/// </summary>
private void doc_EndPrint(object sender, PrintEventArgs e)
{
// Free Bitmap resource
if (this.img != null)
{
this.img.Dispose();
this.img = null;
}
WriteVerbose(string.Format(
"xxxxxxxxxxxxxxxxxxxx {0} xxxxxxxxxxxxxxxxxxxx",
this.doc.DocumentName));
}
#endregion
/// <summary>
/// Adjust image size to fit to the paper size
/// </summary>
/// <param name="imgSize">Size of image to adjust to fit to paper size</param>
/// <param name="paperBound">Bounding area of paper</param>
/// <returns>Adjusted size of image that fits on the paper</returns>
/// <remarks>If image's width is bigger than its height, try to fit width, else fit height</remarks>
private SizeF AdjustImageSize(Size imgSize, RectangleF paperBound)
{
bool fitWidth = (imgSize.Width > imgSize.Height); // Fit width or the height?
double ratio = 1;
SizeF adjSize = new SizeF(imgSize.Width, imgSize.Height); // adjusted size to return
try
{
// Image size is smaller than actual paper size to print on,
// so we use the original image's size
if ((imgSize.Width > paperBound.Width) &&
(imgSize.Height > paperBound.Height))
{
// If width is longer than the height of its image,
// we get the ratio of the width, else get the ratio of the height
if ((fitWidth == true))
{
ratio = (double)(paperBound.Width / imgSize.Height);
}
else
{
ratio = (double)(paperBound.Height / imgSize.Height);
}
adjSize = new SizeF(
// Flip the width of the image in Landscape mode
(this.Landscape ? paperBound.Height : paperBound.Width),
(float)(imgSize.Height * ratio));
}
}
catch (Exception ex)
{
this.ex = ex;
}
return adjSize;
}
}
}
MSH>$bitmap = new-object System.Drawing.Bitmap c:\test.tif
MSH>out-printer -printer "Adobe PDF" -in $bitmap
Tag :
PhysicalDimension : {Width=14454, Height=21957}
Size : {Width=14454, Height=21957}
Width : 14454
Height : 21957
HorizontalResolution : 1016
VerticalResolution : 1016
Flags : 77888
RawFormat : [ImageFormat: b96b3cb1-0728-11d3-9d7b-0000f81ef32e]
PixelFormat : Format1bppIndexed
Palette : System.Drawing.Imaging.ColorPalette
FrameDimensionsList : {7462dc86-6180-4c7e-8e3f-ee7333a7a483}
PropertyIdList : {256, 257, 258, 259, 262, 270, 273, 277, 278, 279, 282, 283, 32781}
PropertyItems : {256, 257, 258, 259, 262, 270, 273, 277, 278, 279, 282, 283, 32781}
print-image "image name" [printer name] [fit image to paper]
MSH> ls *.tif | foreach { print-image $_ }
function print-image { |
Experimenting with a different format of blogs...