Monday, May 14, 2012

Quick way to generate RSA key with LinqPad

To generate private RSA key, we can use LinqPad Expression Executing below expression, will generate RSA key in result window.

new System.Security.Cryptography.RSACryptoServiceProvider (1024).ToXmlString (true) 

if needed, press F4 to add reference, and add reference to System.Security.dll

In Powershell
$rsa = New-Object  System.Security.Cryptography.RSACryptoServiceProvider (1024);
public key
$rsa.ToXmlString($false)
private key
$rsa.ToXmlString($true)

Monday, April 9, 2012

Powershell Find large images

Save the below code in red to .psm1 file eg. ImageFilter.psm1 and then issue following command in powershell console: Import-Module .\ImageFilter.psm1

then you can call the GetFilteredImages function, all params are optional

eg. GetFilteredImages -path c:\temp -exportFile test.txt -width 600 -height 600


*********************************************

Add-Type -Assembly System.Drawing


Function GetFilteredImages
{
Param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[String]
$path = (Get-Location -PSProvider FileSystem).ProviderPath
,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[String]
$exportFile = "$path\result.csv"
,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[Int]
$width = 1000
,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[Int]
$height = 500
)
Process
{
Try
{
Get-ChildItem -Path $path -Recurse -Include *.jpg, *.png, *.gif |
ForEach-Object {
$Image = [System.Drawing.Image]::FromFile($_.FullName)
New-Object -TypeName System.Management.Automation.PSObject -Property @{
Name = $_.Name
Length = $_.Length
Width = $Image.Width
Height = $Image.Height
Fullname = $_.Fullname
}| Where-Object { $_.Width -gt $width -and $_.Height -gt $height}
$Image.Dispose()
} | Select-Object Name, Height, Width, Length, FullName| Export-Csv -NoTypeInformation -Path $exportFile
}
Catch
{
write-warning "Error : $($_.Exception.Message)"
}
}
}