- 编写 snap-in
Youneed to add reference:
System.Management.Automation;
System.Configuration.Install;
- Csc /target:library /reference:.\System.management.Automation.dll PSBook.Chapter2.cs
生成PSBook.Chapter2.dll
- Installutil PSBook.Chapter2.dll
注册snap-in, 可以在path:HKLM\Software\Microsoft\PowerShell\1\PowerShellSnapIns 下检查是否注册成功
或者: get-pssnapin -registered
- Add-pssnapin -Name Wiley.PSProfessional.Chapter2
如果运行失败, 需要先运行
reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR/t REG_DWORD /d 1
reg add hklm\software\wow6432node\microsoft\.netframework /vOnlyUseLatestCLR /t REG_DWORD /d 1
- Get-pssnapin 就可以看到自己编写的snap-in
- Get-Command write-h* 就可以看到自己编写的cmdlet
Source Code(Copy from Poweshell高级编程)
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Management.Automation;
using
System.ComponentModel;
namespace
PowershellTest001