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. 使用UI Automation实现自动化测试 --微软提供的控件Pattern

    微软提供的控件Pattern System.Windows.Automation 命名空间 System.Windows.Automation.BasePattern 为控件模式类提供基实现 Syst ...

  2. .Net性能优化时应该关注的数据

    解决性能问题的时候,我往往会让客户添加下面一些计数器进行性能收集. Process object下的所有计数器: Processor object下的所有计数器: System object下的所有计 ...

  3. 新写的c++日志库:log4K

    网是开源的c/c++日志库也不少,但用起来总觉得不方便,于是动手写了一个C++日志框架Log4K. 测试代码: #include "log4k.h" #pragma comment ...

  4. 配置server.xml后,启动tomcat 失败(Unable to start cluster)及解决方法

    在配置负载均衡环境过程中修改server.xml  后重启tomcat报错,报错信息如下 [root@Alice bin]# ./catalina.sh runUsing CATALINA_BASE: ...

  5. 显示或隐藏一个Grid

    The Rowset class contains two methods that can be used to show and hide all rows: ShowAllRows() Hide ...

  6. 已经导入了具有相同的简单名称“Interop.DSOFramer, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null”的程序集。

    错误  : 已经导入了具有相同的简单名称“Interop.DSOFramer, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null”的程序集. ...

  7. c# TextReader/TextWriter 的类

    TextReader以及TextWriter这两个类,非常有用,很多方法都接受它们作为参数. TextReader有两个子类: StringReader/StringWriter 用于读取字符串: S ...

  8. HTML5 对于手机页面长按会粘贴复制的禁用 (解决方案)

    解决方案: 直接在CSS 文件中添加下面的代码,就可以实现了在手机端禁止粘贴复制的功能: *{    -webkit-touch-callout:none;  /*系统默认菜单被禁用*/    -we ...

  9. mariadb数据库备份学习笔记

    备份类型: 完全备份 部分备份:仅备份其中的一张表或多张表 增量备份:仅备份从上次完全备份或增量备份之后变化的数据部分 热备份:在线备份,读写操作不受影响 温备份:在线备份,读操作可继续进行,但写操作 ...

  10. ubuntu上搭建vsftpd且通过mysql来管理FTP账号

    参考文章:http://wiki.ubuntu.org.cn/Vsftpd%E5%92%8Cmysql%E9%85%8D%E7%BD%AE 请各位先按照这篇文章一步一步操作,我这里是记录一些其间遇到的 ...