11 May 2010 @ 21:58 

Example #1 (From Lee Holmes’ Powershell cookbook)

“Compile C# code on the fly”


$code =@'
using System.Management.Automation; 

[Cmdlet("Write", "InputObject")]
public class MyWriteInputObjectCmdlet : Cmdlet
{
    [Parameter]
    public string Parameter1; 

    [Parameter(Mandatory = true, ValueFromPipeline=true)]
    public string InputObject; 

    protected override void ProcessRecord()
    {
        if (Parameter1 != null)
                WriteObject(Parameter1 +  ":" +  InputObject);
            else
                WriteObject(InputObject);
    }
}
'@

Add-Type -TypeDefinition $code  -OutputAssembly .\ExampleModule.dll 

Import-Module .\ExampleModule.dll

Example #2 (From Lee Holmes’ Powershell cookbook)
Showing how static functions are available directly

$source = @"
public class BasicTest
{
public static int Add(int a, int b)
{
return (a + b);
}

public int Multiply(int a, int b)
{
return (a * b);
}
}
"@

 Add-Type -TypeDefinition $source

[BasicTest]::Add(4, 3)

$basicTestObject = New-Object BasicTest
$basicTestObject.Multiply(5, 2)

Example #3
Access .Net function to move mouse, in order to prevent screensaver from kicking in.

Function Move-Mouse {

param($minutes = 60)

for ($i = 0; $i -lt $minutes; $i++) {
  Start-Sleep -Seconds 60
  $Pos = [System.Windows.Forms.Cursor]::Position
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1) , $Pos.Y)

}
}
Posted By: Xenophane
Last Edit: 11 May 2010 @ 21:58

EmailPermalink
Tags
Tags:
Categories: Everyday


 

Responses to this post » (One Total)

 
Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


 Last 50 Posts
 Back
Change Theme...
  • Users » 159
  • Posts/Pages » 98
  • Comments » 73
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Links



    No Child Pages.

Books



    No Child Pages.

Ønsker



    No Child Pages.

CV



    No Child Pages.