PowerShell - Deep Dive
PowerShell - Deep Dive
Function
# Creating Simple Function
Write-Host "I have Dell Laptops with Windows OS"
Function Global:Get-MachineInfo
{
Write-Host "I have Dell Laptops with Windows OS"
}
Get-MachineInfo
Get-ChildItem Function:\Get-MachineInfo |fl *
# specify parameters of a function
Function Get-MachineInfo
{ param ($make,$OperatingSystem)
Write-Host "I have $make Laptops with $OperatingSystem OS"
}
Get-MachineInfo -make "Lenovo"
Get-MachineInfo -make "IBM" -OperatingSystem "Linux"
## Creating Advanced Function
(Get-command -Name Get-Service).parameters.keys
(Get-command -Name Get-MachineInfo).parameters.keys
Function Get-MachineInfo
{
[CmdLetBinding()]
param ($make,$OperatingSystem)
Write-Verbose "Sharing Information about my Laptop Stock"
Write-Host "I have $make Laptops with $OperatingSystem OS"
}
(Get-command -Name Get-MachineInfo).parameters.keys
Get-MachineInfo -Verbose
# Adding WhatIf & Confirm Parameter
(Get-command -Name Get-MachineInfo).parameters.keys
Function Get-MachineInfo
{
[CmdLetBinding(SupportsShouldProcess)]
param ($make,$OperatingSystem)
Write-Verbose "Sharing Information about my Laptop Stock"
Write-Host "I have $make Laptops with $OperatingSystem OS"
}
(Get-command -Name Get-MachineInfo).parameters.keys
# Declaring Madatory Parameter
Function Get-MachineInfo
{
[CmdLetBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory)]
$make,$OperatingSystem)
Write-Verbose "Sharing Information about my Laptop Stock"
Write-Host "I have $make Laptops with $OperatingSystem OS"
}
Get-MachineInfo
## ensuring single Value against a parameter
Function Get-MachineInfo
{
[CmdLetBinding(SupportsShouldProcess)]
param (
[String]$make,$OperatingSystem)
Write-Verbose "Sharing Information about my Laptop Stock"
Write-Host "I have $make Laptops with $OperatingSystem OS"
}
Get-MachineInfo -make "Dell", "Lenovo"
Function Get-MachineInfo
{
[CmdLetBinding(SupportsShouldProcess)]
param (
[String[]]$make,$OperatingSystem)
Write-Verbose "Sharing Information about my Laptop Stock"
Write-Host "I have $make Laptops with $OperatingSystem OS"
}
Get-MachineInfo -make "Dell", "Lenovo"
## Defining default value of a parameter
Function Get-MachineInfo
{
[CmdLetBinding(SupportsShouldProcess)]
param (
[String[]]$make = "IBM",$OperatingSystem)
Write-Verbose "Sharing Information about my Laptop Stock"
Write-Host "I have $make Laptops with $OperatingSystem OS"
}
Get-MachineInfo
Get-MachineInfo -make "Dell"
Get-Service
# Writing Comment based help for a function
Help get-service -ShowWindow
Help Get-MachineInfo -ShowWindow
Get-MachineInfo
Function Get-MachineInfo
{
<#
.SYNOPSIS
Sharing Machine Info
.DESCRIPTION
This is being used to display about my laptop stock
.PARAMETER make
This parameter is used to provide brand of laptop
.PARAMETER OperatingSystem
This tell OS of my llaptops
.EXAMPLE
Get-MachineInfo
.EXAMPLE
Get-MachineInfo -make "Dell"
.NOTES
Author:
Website:
#>
[CmdLetBinding(SupportsShouldProcess)]
param (
[String[]]$make = "IBM",$OperatingSystem)
Write-Verbose "Sharing Information about my Laptop Stock"
Write-Host "I have $make Laptops with $OperatingSystem OS"
}
Help Get-MachineInfo -ShowWindow
# Creating menu of Possible values of a parameter
Function Get-MachineInfo
{
[CmdLetBinding(SupportsShouldProcess)]
param (
[ValidateSet ("Dell","IBM","Lenovo")]
[String[]]$make = "IBM",$OperatingSystem)
Write-Verbose "Sharing Information about my Laptop Stock"
Write-Host "I have $make Laptops with $OperatingSystem OS"
}
Get-MachineInfo -make
相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。