Search

Function

[- Disclaimer -] 아래 내용은 정보보안 공부 목적으로 작성된 것이나, 이를 토대로 허가되지 않은 대상에 실습을 진행할 경우 해킹 시도로 간주하여 법적 처벌을 받을 수 있음을 알려 드립니다.
함수 문법
function print_msg { "hello World!" }
Plain Text
복사
function print_msg { $args }
Plain Text
복사
function add { $args[0] + $args[1] }
Plain Text
복사
function addint { $args[0] + $args[1] }
Plain Text
복사
function defaultval ($a = 10, $b) { }
Plain Text
복사
사용자 정의 함수 목록 확인
PS C:\> Get-ChildItem function:\ CommandType Name Version Source ----------- ---- ------- ------ Function A: Filter add Function B: Function C: Function cd.. Function cd\ Function Clear-Host Function ConvertFrom-SddlString 3.1.0.0 Microsoft.PowerShell.Utility Function D: Function E: Function F: Function Format-Hex 3.1.0.0 Microsoft.PowerShell.Utility Function G: Function Get-FileHash 3.1.0.0 Microsoft.PowerShell.Utility Function Get-Verb Function H: Function help Function I: Function Import-PowerShellDataFile 3.1.0.0 Microsoft.PowerShell.Utility Function ImportSystemModules Function J: Function K: Function L: Function M: Function mkdir Function more Function N: Function New-Guid 3.1.0.0 Microsoft.PowerShell.Utility Function New-TemporaryFile 3.1.0.0 Microsoft.PowerShell.Utility Function O: Function oss Function P: Function Pause Function print_msg Function prompt Function PSConsoleHostReadLine 2.0.0 PSReadline Function Q: Function R: Function S: Function T: Function TabExpansion2 Function U: Function V: Function W: Function X: Function Y: Function Z: PS C:\>
Plain Text
복사
사용자 정의 함수 제거
Remove-Item function:\sumFunction
Plain Text
복사
Ex)
PS C:\> function print_msg { "Hello World!" } PS C:\> print_msg Hello World! PS C:\> Get-Help print_msg 이름 print_msg 구문 print_msg 별칭 없음 설명 없음 PS C:\> Get-Command *msg CommandType Name Version Source ----------- ---- ------- ------ Function print_msg PS C:\>
Plain Text
복사
PS C:\> function add($a, $b) {$a + $b} PS C:\> add 1 2 // 사용자 정의 함수라 괄호로 안 감싸줌 3 PS C:\>
Plain Text
복사
PS C:\> add -a 1 -b 1 2 PS C:\>
Plain Text
복사
PS C:\> function add ($a = 10, $b) {$a + $b} PS C:\> add -b 1 11 PS C:\> add 1 1 PS C:\>
Plain Text
복사
Ex) - 2
function addint([int]$a, [int]$b) { $out = $a + $b Write-Host $out } addint 1 2
Plain Text
복사
. ./add-int.ps1 //닷소싱을 해줘야 출력 부분 실행 됨. Session에 함수 가져오는기라...?
Plain Text
복사
function adv_function { param( [Parameter()] $a, [Parameter()] $b ) $out = $a + $b Write-Host $out }
Plain Text
복사
PS C:\Users\aapsemiordoenus\Documents> adv_function 1 2 3
Plain Text
복사
function adv_function { param( [Parameter(Mandatory = $true)] $a, [Parameter()] $b ) $out = $a + $b Write-Host $out }
Plain Text
복사
PS C:\Users\aapsemiordoenus\Documents> adv_function -a 1 -b 2 3
Plain Text
복사
function adv_function { [CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory = $true)] $a, [Parameter()] $b ) $out = $a + $b Write-Verbose "Adding Numbers" if($PSCmdlet.ShouldProcess("Adding 2 numbers")) { $out = $a + $b } Write-Host $out }
Plain Text
복사
PS C:\Users\aapsemiordoenus\Documents> adv_function 1 2 -verbose VERBOSE: Adding Numbers VERBOSE: Performing the operation "adv_function" on target "Adding 2 numbers". 3
Plain Text
복사
PS C:\Users\aapsemiordoenus\Documents> adv_function 1 2 -whatif What if: Performing the operation "adv_function" on target "Adding 2 numbers". 3
Plain Text
복사
PS C:\Users\aapsemiordoenus\Documents> adv_function 1 2 -confirm
Plain Text
복사