Learn to build powershell script (for busy programmers)
Single line comment
#
Multi-line comment
<# …. #>
Print message in console
Write-Host “hello world”
Variables
#variable of type String
$name=""
#variable of type Int32 (Integer)
$id=10
#variable of type Hashtable
$map = @{name=”trees”;color=”green”}
#variable of type array
$array = "lion","tiger","wolf"
Print value of a variable in console
Access a property from hashtable
Add a property to an hashtable
Remove a property from hashtable
List all keys in an Hashtable
Iterate a Hashtable
Iterate an array
How to find the datatype of variables? using GetType() function. Below is an example on how to use GetType()
Type-conversion of variable
#convert String to Dictionary or Json
$responseBody = $httpResponse | ConvertFrom-Json
#convert ,(comma) separated String to Array of Strings
$stringArray = $commaString.Split(",")
#convert String to Hashtable
$hashTable = Invoke-Expression $hashString
Invoke a restApi endpoint with Httpmethod Get
# $httpResponse will be of type String
$httpResponse = Invoke-WebRequest -Uri https://domain.com/api/users -Method Get
#convert String to Dictionary or Json
$responseBody = $httpResponse | ConvertFrom-Json
#print the json object i.e $responseBody
Write-Host "responseBody $responseBody"
#fetch a country name from JSON object
$countryName = $responseBody."Country Name"
#print all the keys and values from the JSON object
foreach($key in $responseBody.PsObject.Properties.name){
Write-Host "key:: $key value:: " $responseBody.$key
}
Pass Header along with the `Get` request
$httpResponse = Invoke-WebRequest -Uri https://cricscore -Headers @{ 'app-name' ='cocofruit'}
User-defined function without arguments
function MyFun{
return "pluto"
}
# $retval will hold "pluto"
$retval = MyFun
User-defined function with single argument
function MyFun {
param (
$osName
)
return $osName
}
$retval = MyFun("ubuntu")
User-defined function with multiple arguments
function MyFun{
param(
$country,
$state,
$city
)
Write-Host "country: $country :state: $state :city: $city "
}
MyFun "India" "Tamilnadu" "Madurai"
Add a newline character with-in a string
$sentence="curious the `r`n lazy frog"
Send email
$fromEmail = "admin@gmail.com"
$toEmail = "divine@gmail.com","fish@gmail.com","seal@gmail.com"
$attachmentFile = "filename.html"
New-Item $attachmentFile -ItemType File
Add-Content $attachmentFile "Dummy content within attachment"
Send-MailMessage -To $toEmail -From $fromEmail -Subject "REPORT" -Body "emailBody" -BodyAsHtml -Priority High -SmtpServer "mailhost3" -Attachments $attachmentFile
Array
$data = @(
'Zero'
'One'
'Two'
'Three'
)
$data += 'four'
Write-Host "$data”
Write-Host $data.count
foreach ( $node in $data )
{
"Item: [$node]"
}
Array of objects
$data = @(
[pscustomobject]@{FirstName='Kevin';LastName='Marquette'}
[pscustomobject]@{FirstName='John'; LastName='Doe'}
)
#$data = @()
#Write-Host $data[0]
#Write-Host $data[0].FirstName
$data += [pscustomobject]@{FirstName='Tiger1'; LastName='cream'}
$data += [pscustomobject]@{FirstName='ice'; LastName='blood'}
foreach($person in $data)
{
Write-Host $person.FirstName
}
Sort the above array of objects by Firstname
$sorted = $data | Sort-Object -Property FirstName
foreach($sor in $sorted)
{
Write-Host $sor.FirstName
}
This blog will be updated …
Related links…