PowerShell Function Labs Code
PowerShell Function Labs Code
function Get-MLOVersion
{
$PSVersionTable.PSVersion.Major
}
Function Welcome
{
[CmdletBinding(SupportsShouldProcess)]
param ($name)
Write-host "Welcome Mr. $Name"
}
(Get-Command -Name Welcome).Parameters.Keys
Function Write-Welcome
{
[CmdletBinding(SupportsShouldProcess)]
param (
[ValidateNotNullOrEmpty()]
[string[]]$name = "Raymond"
)
Write-host "Welcome Mr. $Name"
}
.DESCRIPTION
Write-Welcome is a function that welcomes User
.PARAMETER Name
Defines the Name of User
.PARAMETER Place
.EXAMPLE
Write-Welcome -Name "Raymond"
.EXAMPLE
Example2
.EXAMPLE
Example3
.INPUTS
String
.OUTPUTS
PSCustomObject
.NOTES
Author: luxmi narayaan
Website: Test Function
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[ValidateNotNullOrEmpty()]
[string[]]$name = "Raymond"
)
Write-Verbose -Message "Welcoming Our Guest"
Write-host "Welcome Mr $Name"
}
[ValidateNotNullOrEmpty()]
[string[]]$Place = "India"
)
Write-Verbose -Message "Welcoming Our Guest"
Write-host "Welcome Mr. $Name to $place"
}
######## Ensure that parameter Value is Selected out of Some predefined Values Only
#####
Function Write-Welcome
{
[CmdletBinding(SupportsShouldProcess)]
param (
#[Parameter(Mandatory)]
[ValidateSet("Services","Process","Events")]
[string[]]$Item = ("Services","Process","Events")
)
Switch($item)
{
Services{
Write-Host "Listing first 5 Services" -ForegroundColor Green
Get-Service|select -First 5
}
Process{
Write-Host "Listing first 5 Processes" -ForegroundColor Green
Get-Process|select -First 5
}
events{
Write-Host "Listing first 5 Events" -ForegroundColor Green
Get-EventLog -LogName Application|select -First 5}
}
}