이번에는 PowerShell에서 사용되는 cmdlet에 대해서 알아 보도록 하겠습니다.

PowerShell에서 명령어를 사용하기 위해서 Get-Command를 입력하면 아래와 같이 많은 명령어들이 출력되는 것을 볼 수 있습니다.

PS C:\Users\k3y6reak> Get-Command

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           Add-ProvisionedAppxPackage                         3.0        Dism
Alias           Apply-WindowsUnattend                              3.0        Dism
Alias           Disable-PhysicalDiskIndication               Storage
Alias           Disable-StorageDiagnosticLog                 Storage
Alias           Enable-PhysicalDiskIndication                Storage
Alias           Enable-StorageDiagnosticLog                  Storage
Alias           Flush-Volume                                 Storage
Alias           Get-DiskSNV                                  Storage
Alias           Get-PhysicalDiskSNV                          Storage
Alias           Get-ProvisionedAppxPackage                         3.0        Dism
Alias           Get-StorageEnclosureSNV                      Storage
Alias           Initialize-Volume                            Storage
Alias           Move-SmbClient                               SmbWitness
Alias           Remove-ProvisionedAppxPackage                      3.0        Dism
Alias           Write-FileSystemCache                        Storage
Function        A:
Function        Add-BCDataCacheExtension                     BranchCache
Function        Add-BitLockerKeyProtector                    BitLocker
Function        Add-DnsClientNrptRule                        DnsClient
Function        Add-DtcClusterTMMapping                      MsDtc
Function        Add-EtwTraceProvider                         EventTracingManagement
Function        Add-InitiatorIdToMaskingSet                  Storage

이 중에서 알아볼 명령어는 cmdlet에 대한 명령어인데 해당 명령어만 출력하도록 해보겠습니다. Get-Command -CommandType cmdlet을 실행합니다.

PS C:\Users\k3y6reak> Get-Command -CommandType cmdlet

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Add-AppvClientConnectionGroup                AppvClient
Cmdlet          Add-AppvClientPackage                        AppvClient
Cmdlet          Add-AppvPublishingServer                     AppvClient
Cmdlet          Add-AppxPackage                              Appx
Cmdlet          Add-AppxProvisionedPackage                         3.0        Dism
Cmdlet          Add-AppxVolume                               Appx
Cmdlet          Add-BitsFile                                 BitsTransfer
Cmdlet          Add-CertificateEnrollmentPolicyServer        PKI
Cmdlet          Add-Computer                                 Microsoft.PowerShell.Management
Cmdlet          Add-Content                                  Microsoft.PowerShell.Management
Cmdlet          Add-History                                  Microsoft.PowerShell.Core
Cmdlet          Add-JobTrigger                               PSScheduledJob
Cmdlet          Add-KdsRootKey                               Kds
Cmdlet          Add-LocalGroupMember                         Microsoft.PowerShell.LocalAccounts
Cmdlet          Add-Member                                   Microsoft.PowerShell.Utility
Cmdlet          Add-PSSnapin                                 Microsoft.PowerShell.Core
Cmdlet          Add-SignerRule                                     1.0        ConfigCI
Cmdlet          Add-Type                                     Microsoft.PowerShell.Utility

cmdlet에 대한 명령어 목록이 출력된 것을 확인할 수 있습니다. 이 중에서 process와 관련된 명령어를 찾고자 한다면 Get-Command -CommandType cmdlet -Name *process 을 입력합니다.

PS C:\Users\k3y6reak> Get-Command -CommandType cmdlet -Name *process

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Debug-Process                                Microsoft.PowerShell.Management
Cmdlet          Enter-PSHostProcess                          Microsoft.PowerShell.Core
Cmdlet          Exit-PSHostProcess                           Microsoft.PowerShell.Core
Cmdlet          Get-Process                                  Microsoft.PowerShell.Management
Cmdlet          Start-Process                                Microsoft.PowerShell.Management
Cmdlet          Stop-Process                                 Microsoft.PowerShell.Management
Cmdlet          Wait-Process                                 Microsoft.PowerShell.Management

여기서 Start-Process를 이용하여 notepad.exe를 실행시켜 보도록 하겠습니다. 그 전에 Start-Process를 어떻게 사용해야 하는지 알고 싶다면 Get-Help Start-Process -example를 입력합니다.

PS C:\Users\k3y6reak> Get-Help Start-Process -example


    Starts one or more processes on the local computer.

    Example 1: Start a process that uses default values

    PS C:\>Start-Process -FilePath "sort.exe"

    This command starts a process that uses the Sort.exe file in the current folder. The command uses all of the default values, including the default window style, working folder, and credentials.
    Example 2: Print a text file

    PS C:\>Start-Process -FilePath "myfile.txt" -WorkingDirectory "C:\PS-Test" -Verb Print

    This command starts a process that prints the C:\PS-Test\MyFile.txt file.
    Example 3: Start a process to sort items to a new file

    PS C:\>Start-Process -FilePath "Sort.exe" -RedirectStandardInput "Testsort.txt" -RedirectStandardOutput "Sorted.txt" -RedirectStandardError "SortError.txt" -UseNewEnvironment

    This command starts a process that sorts items in the Testsort.txt file and returns the sorted items in the Sorted.txt files. Any errors are written to the SortError.txt file.

    The UseNewEnvironment parameter specifies that the process runs with its own environment variables.
    Example 4: Start a process in a maximized window

    PS C:\>Start-Process -FilePath "notepad" -Wait -WindowStyle Maximized

    This command starts the Notepad process. It maximizes the window and retains the window until the process completes.
    Example 5: Start Windows Powershell as an administrator

    PS C:\>Start-Process -FilePath "powershell" -Verb runAs

    This command starts Windows PowerShell by using the Run as administrator option.
    Example 6: Using different verbs to start a process

    PS C:\>$startExe = New-Object System.Diagnostics.ProcessStartInfo -Args PowerShell.exe
    PS C:\> $startExe.verbs

    # Starts a PowerShell process in a new console window.

    PS C:\> Start-Process -FilePath "powershell.exe" -Verb open

    # Starts a PowerShell process with "Run as Administrator" permissions.

    PS C:\> Start-Process -FilePath "powershell.exe" -Verb runas

    These commands show how to find the verbs that can be used when starting a process, and the effect of using the verbs to start the process.

    The available verbs are determined by the file name extension of the file that runs in the process. To find the verbs for a process, create a System.Diagnostics.ProcessStartInfo object for the process file
     and look in the Verbs property of the object. This example uses the PowerShell.exe file that runs in the PowerShell process.

    The first command uses New-Object to create a System.Diagnostics.ProcessStartInfo object for PowerShell.exe, the file that runs in the PowerShell process. The command saves the ProcessStartInfo object in t
    he $startExe variable.

    The second command displays the values in the Verbs property of the ProcessStartInfo object in the $startExe variable. The results show that you can use the Open and Runas verbs with PowerShell.exe, or wit
    h any process that runs a .exe file.

    The third command starts a PowerShell process with the Open verb. The Open verb starts the process in a new console window.

    The fourth command starts a PowerShell process with the RunAs verb. The RunAs verb starts the process with permissions of a member of the Administrators group on the computer. This is the same as starting
    Windows PowerShell by using the Run as administrator option.

Start-Process notepad.exe를 입력하면 아래와 같이 notepad.exe가 실행된 것을 알 수 있습니다.


반대로 Stop-Process notepad.exe를 입력하면 아래와 같이 에러가 발생합니다.

PS C:\Users\k3y6reak> Stop-Process notepad.exe
Stop-Process : 'InputObject' 매개 변수를 바인딩할  없습니다. "System.String" 유형의 "notepad.exe" 값을 "System.Diagnostics.Process" 유형으로 변환할  없습니다.
위치 :1 문자:14
+ Stop-Process notepad.exe
+              ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Stop-Process], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.StopProcessCommand

Get-Help를 이용해서 Stop-Proces에 대한 예시를 살펴봅니다.

PS C:\Users\k3y6reak> Get-Help Stop-Process -example


    Stops one or more running processes.

    Example 1: Stop all instances of a process

    PS C:\>Stop-Process -Name "notepad"

위와 같이 -Name이라는 옵션을 이용해야 합니다. 직접 실행해 보면 notepad가 종료된 것을 알 수 있습니다.

© 2017. by k3y6reak

Powered by k3y6reak