이 샘플에서는 로컬 컴퓨터에서 프로세스를 검색하는 cmdlet을 구현하는 방법을 보여 줍니다. 이 cmdlet은 Windows PowerShell 2.0에서 제공하는 Get-Process
cmdlet의 간소화된 버전입니다.
Visual Studio를 사용하여 샘플을 빌드하는 방법
Windows PowerShell 2.0 SDK가 설치된 상태에서 GetProcessSample01 폴더로 이동합니다. 기본 위치는
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01
입니다.솔루션(.sln) 파일의 아이콘을 두 번 클릭합니다. 그러면 Microsoft Visual Studio에서 샘플 프로젝트가 열립니다.
빌드 메뉴에서 빌드 솔루션 선택하여 기본
\bin
또는\bin\debug
폴더에서 샘플에 대한 라이브러리를 빌드합니다.
샘플을 실행하는 방법
명령 프롬프트 창을 엽니다.
샘플 .dll 파일이 포함된 디렉터리로 이동합니다.
installutil "GetProcessSample01.dll"
를 실행합니다.Windows PowerShell을 시작합니다.
다음 명령을 실행하여 셸에 스냅인을 추가합니다.
Add-PSSnapin GetProcPSSnapIn01
다음 명령을 입력하여 cmdlet을 실행합니다.
Get-Proc
Get-Proc
다음 단계를 수행하여 발생하는 샘플 출력입니다.
Id Name State HasMoreData Location Command -- ---- ----- ----------- -------- ------- 1 26932870-d3b... NotStarted False Write-Host "A f...
Set-Content $Env:TEMP\test.txt "This is a test file"
A file was created in the TEMP directory
요구 사항
이 샘플에는 Windows PowerShell 1.0 이상이 필요합니다.
입증합니다
이 샘플에서는 다음을 보여 줍니다.
기본 샘플 cmdlet 만들기
Cmdlet 특성을 사용하여 cmdlet 클래스를 정의합니다.
Windows PowerShell 1.0 및 Windows PowerShell 2.0 모두에서 작동하는 스냅인을 만듭니다. 후속 샘플에서는 스냅인 대신 모듈을 사용하므로 Windows PowerShell 2.0이 필요합니다.
예시
이 샘플에서는 간단한 cmdlet 및 해당 스냅인을 만드는 방법을 보여줍니다.
using System;
using System.Diagnostics;
using System.Management.Automation; //Windows PowerShell namespace
using System.ComponentModel;
namespace Microsoft.Samples.PowerShell.Commands
{
#region GetProcCommand
/// <summary>
/// This class implements the Get-Proc cmdlet.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : Cmdlet
{
#region Cmdlet Overrides
/// <summary>
/// The ProcessRecord method calls the Process.GetProcesses
/// method to retrieve the processes of the local computer.
/// Then, the WriteObject method writes the associated processes
/// to the pipeline.
/// </summary>
protected override void ProcessRecord()
{
// Retrieve the current processes.
Process[] processes = Process.GetProcesses();
// Write the processes to the pipeline to make them available
// to the next cmdlet. The second argument (true) tells Windows
// PowerShell to enumerate the array and to send one process
// object at a time to the pipeline.
WriteObject(processes, true);
}
#endregion Overrides
} //GetProcCommand
#endregion GetProcCommand
#region PowerShell snap-in
/// <summary>
/// Create this sample as a PowerShell snap-in
/// </summary>
[RunInstaller(true)]
public class GetProcPSSnapIn01 : PSSnapIn
{
/// <summary>
/// Create an instance of the GetProcPSSnapIn01
/// </summary>
public GetProcPSSnapIn01()
: base()
{
}
/// <summary>
/// Get a name for this PowerShell snap-in. This name will be used in registering
/// this PowerShell snap-in.
/// </summary>
public override string Name
{
get
{
return "GetProcPSSnapIn01";
}
}
/// <summary>
/// Vendor information for this PowerShell snap-in.
/// </summary>
public override string Vendor
{
get
{
return "Microsoft";
}
}
/// <summary>
/// Gets resource information for vendor. This is a string of format:
/// resourceBaseName,resourceName.
/// </summary>
public override string VendorResource
{
get
{
return "GetProcPSSnapIn01,Microsoft";
}
}
/// <summary>
/// Description of this PowerShell snap-in.
/// </summary>
public override string Description
{
get
{
return "This is a PowerShell snap-in that includes the Get-Proc cmdlet.";
}
}
}
#endregion PowerShell snap-in
}
또한 참조하십시오
PowerShell