How to call C# code in powershell

Powershell Command Add-Type

  1. usage of Add-Type
  2. we use Add-Type -TypeDefinition $code. About C# Code, we should wrap it with a class.we put our sub class, struct,method,in this class.
  3. Sample code
$Code = @'
using System.Runtime.InteropServices;
using System.Drawing; public class GetCursor
{
public struct POINT
{
public int X;
public int Y;
public static implicit operator Point(POINT point)
{
return new Point(point.X, point.Y);
}
};
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint); public static Point GetCursorPosition()
{
POINT lpPoint;
GetCursorPos(out lpPoint);
return lpPoint;
}
}
'@

we call it in powershell

    Add-Type -TypeDefinition $Code -ReferencedAssemblies System.Drawing

we call Add-Type by using parameter -memberdefinition

  1. if we use memberdefinition, parameter, we just expose the function to the powershell session, we don't need to create a class, powershell will auto generate a class for us. In this scenario, we usually, use this way to invoke native windows api. Detail info refer MSDN, Exapmle5, Here is a small code snip.
$CSharpCode=@"
[DllImport("user32.dll")]
public static extern bool BlockInput(bool fBlockIt);
"@
$CS=Add-Type -MemberDefinition $CSharpCode -Name "KeyBoardMouse" -namespace win32Func -PassThru
$res = $CS:BlockInput($true)

Call C# in powershell的更多相关文章

  1. 在PowerShell中使用curl(Invoke-WebRequest)

    前言 习惯了windows的界面模式就很难转去命令行,甚至以命令行发家的git也涌现出各种界面tool.然而命令行真的会比界面快的多,如果你是一个码农. situation:接到需求分析bug,需要访 ...

  2. Windows 7上执行Cake 报错原因是Powershell 版本问题

    在Windows 7 SP1 电脑上执行Cake的的例子 http://cakebuild.net/docs/tutorials/getting-started ,运行./Build.ps1 报下面的 ...

  3. <译>通过PowerShell工具跨多台服务器执行SQL脚本

    有时候,当我们并没有合适的第三方工具(大部分需要付费)去管理多台数据库服务器,那么如何做最省力.省心呢?!Powershell一个强大的工具,可以很方便帮到我们处理日常的数据库维护工作 .简单的几步搞 ...

  4. 利用PowerShell复制SQLServer账户的所有权限

    问题 对于DBA或者其他运维人员来说授权一个账户的相同权限给另一个账户是一个很普通的任务.但是随着服务器.数据库.应用.使用人员地增加就变得很枯燥乏味又耗时费力的工作.那么有什么容易的办法来实现这个任 ...

  5. PowerShell 数组以及XML操作

    PowerShell基础 PowerShell数组操作 将字符串拆分成数据的操作 cls #原始字符串 $str = "abc,def,ghi,mon" #数据定义 #$StrAr ...

  6. linux下mono,powershell安装教程

    1简介 简单来说pash就是bash+powershell 2官网 https://github.com/Pash-Project/Pash 3下载fedora20---lxde桌面---32位版. ...

  7. Windows下PowerShell监控Keepalived

    一.背景 某数据库服务器为CentOS,想要监控Keepalived的VIP是否有问题,通过邮件进行报警,但这台机器不能上外网,现在只能在Windows下通过PowerShell来完成发邮件预警. 二 ...

  8. 使用PowerShell收集多台服务器的性能计数器

    写在前面     当管理多台Windows Server服务器时(无论是DB.AD.WEB以及其他的应用服务器),当出现性能或其他问题后,参阅性能计数器都是一个非常好的维度从而推测出问题可能出现的原因 ...

  9. 野路子出身PowerShell 文件操作实用功能

    本文出处:http://www.cnblogs.com/wy123/p/6129498.html 因工作需要,处理一批文件,本想写C#来处理的,后来想想这个是PowerShell的天职,索性就网上各种 ...

  10. 使用PowerShell 监控运行时间和连接情况

    概念 Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境.你可以把它看成是命令行提示符cmd.exe的扩充,不对,应当是颠覆. powershell需要.N ...

随机推荐

  1. [SSH 3]以网上商城项目浅谈spring配置

    导读:在做ITOO项目的时候,就用到了容器+反射,从而运用了依赖注入和依赖查找.如果看过WCF端的配置文件,那么对于这个spring的配置就很容易理解.本篇博客,是对于自己做的一个小项目中所运用到的s ...

  2. 跨域请求之JSONP 三

    script请求返回JSON实际上是脚本注入.它虽然解决了跨域问题,但它不是万能的. 不能接受HTTP状态码 不能使用POST提交(默认GET) 不能发送和接受HTTP头 不能设置同步调用(默认异步) ...

  3. 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)

    消息队列处理后台任务带来的问题 项目中经常会有后台运行任务的需求,比如发送邮件时,因为要连接邮件服务器,往往需要5-10秒甚至更长时间,如果能先给用户一个成功的提示信息,然后在后台慢慢处理发送邮件的操 ...

  4. VC++2010下编译STLport,Boost

    VC++2010下编译STLport,Boost 最近在想向Boost转移,努力掌握Boost代码的过程中, STLport版本:5.2.1 Boost版本:1.4.6.1 (1.4.7.0也OK) ...

  5. leetcode 118

    118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  6. Composer -- PHP依赖管理的用法

    1:下载 1.1:方法一: 通过PHP来安装 cd /home/composer curl -sS https://getcomposer.org/installer | php  #这个命令会下载c ...

  7. C++ streambuf用法

    class LogStreamBuf : public std::streambuf { public: // REQUIREMENTS: "len" must be >= ...

  8. CSS中属性position位置详解功能讲解与实例分析

    position有五个值:static.relative.absolute.fixed.inherit. static 是默认值.就是按正常的布局流从上到下从左到右布局,平常我们做网页制作时,没有指定 ...

  9. 【PHP】金额数字转换成大写形式

    <?php /*将数字金额转成大写*/ function num_to_upper($num) { $d = array('零','壹','贰','叁','肆','伍','陆','柒','捌', ...

  10. Nginx下10个安全问题提示

    Nginx是当今最流行的Web服务器之一.它为世界上7%的web流量提供服务而且正在以惊人的速度增长.它是个让人惊奇的服务器,我愿意部署它 下面是一个常见安全陷阱和解决方案的列表,它可以辅助来确保你的 ...