从PowerShell脚本调用DLL的方法
PowerShell 基于.NET Framework 构建,它和C# 共享一些语法功能和关键字。对于调用DLL 也是容易的。
$PSVersionTable.PSVersion
Major Minor Build Revision ----- ----- ----- -------- 5 1 18362 145
创建一个Class,这个类包含了构造方法,静态方法和普通方法
using System; namespace Cosmos.StudentForFrameWork { public class Students { public string Name { get; set; } public int Age { get; private set; } public Students(string name, int age) { Name = name; Age = age; } public static string StaticStudent(string name) { return "Hello:" + name; } public String GetInfo(string message) { return message + Name + Age; } public static string GetName { get { return "Do u want get my Name!"; } } } }
把它编译成dll.
导入你的dll所在的地方
调用 dll 的构造方法 使用 ::new()
调用dll 的静态方法使用::MethodName()
Import-Module "$PSScriptRoot\Cosmos.StudentForFrameWork.dll";
$Stuent=[Cosmos.StudentForFrameWork.Students]::new($name,$age); $StudentInfo=$Stuent.GetInfo();
$StaticStu=[Cosmos.StudentForFrameWork.Students]::StaticStudent($name);

浙公网安备 33010602011771号