Skip to content

Instantly share code, notes, and snippets.

@rikwatson
Created April 1, 2019 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikwatson/91da714c5d63bc8a635fc6dde079fa7c to your computer and use it in GitHub Desktop.
Save rikwatson/91da714c5d63bc8a635fc6dde079fa7c to your computer and use it in GitHub Desktop.
Powershell 101
#requires -version 4
get-childitem variable:
get-module -ListAvailable
get-module psake
<#
Powershell 101
Just a dumping ground for some techniques.
TODO: I need to get some best practices / coding standards & boiler plate sorted.
#>
## Version
$psversiontable
# or (Less good)
if ($false)
{
$hostVersionInfo = (get-host).Version.Major
if ( $hostVersionInfo -eq "2" )
{
# For PowerShell v2
write "We appear to be using PS version $hostVersionInfo... That's okay, but this script processes faster with PS v3!"
write ""
$containers = Get-ChildItem -path $MoLoPath\$CurrentParent -recurse | ? {$_.psIscontainer -eq $true}
}
elseif ( $hostVersionInfo -eq "3" )
{
# For PowerShell v3
write "We appear to be using PS version $hostVersionInfo... Good!"
write ""
$containers = Get-ChildItem -Directory -path $MoLoPath -recurse
}
else
{
write-host "Unknown/unapproved version of PowerShell. Exiting!"
exit
}
}
## Alias's
Get-Alias
<#
% and ? are the only flow control ones defined.
% ForEach-Object
? Where
`...der | ? { $_ } | % { $syncExclusi….
`
NOTE: ForEach (item In collection) {ScriptBlock} is DIFFERENT to foreach-object
TODO: I must understand this.
#>
Get-Process | ForEach-Object {Write-Host $_.name -foregroundcolor cyan}
Get-Process | % {Write-Host $_.name -foregroundcolor cyan}
## Grid View
For example:
```
Get-Command -PSSnapin Microsoft.PowerShell.Utility |sort-object PSSnapIn, noun | get-help -full | Out-GridView
```
## Arrays
$animal = @()
$animal
$animal = "Cat", "Dog"
$animal
$animal.count
$animal += "Hampster"
$animal
$animal[0]
$animal[$animal.count-1]
$animal -ne "Cat"
$animal -like '*a*'
[array]::Reverse($animal)
$animal | % { Write-Output ">>" $_}
$animal.GetType()
"$animal" # So all on one line
## Hash tables
$table = @{}
$table.count
$table.GetType()
# functions
#
# Always use the attribute [cmdletbindings()], even if there are no paramaters
#
# If you ned vriable no of parameters then use `$args` (Not sure if you can use cmdletbindings() in that
# case
#
function my-function
{
[cmdletbinding()]
Param()
Write-Verbose "Hi Rik"
}
my-function 1291212
function ex_01
{
#(param)
Write-Output "Fn 01"
}
ex_01
function ConvertTo-html
{
<#
get-alias | convertto-html > .\aliases.htm
Invoke-Item .\aliases.htm
get-eventlog -logname "Windows PowerShell" | convertto-html > pslog.htm
Invoke-Item .\pslog.htm
get-process | convertto-html -property Name, Path, Company -title "Process Information" > proc.htm; ii proc.htm
get-service A* | ConvertTo-Html -title "Windows Services: Server01" `
-body (get-date) `
-pre "<P>Generated by Corporate IT</P>" `
-post "For details, contact Corporate IT." > services.htm; ii services.htm
Write-output $variable
#>
}
##################
<#
Create a pipe-able function
#>
function sqrt {
param(
[Parameter(ValueFromPipeline)]
$p
)
Process{[Math]::Sqrt($p)}
}
<# Then #>
sqrt 25
25 | sqrt
6..10 | sqrt
#############
function my-function-with-no-parameters
{
[cmdletbinding()]
Param()
Write-Output "Hello from my-function-with-no-parameters"
}
my-function-with-no-parameters
my-function-with-no-parameters 1291212
if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{Write-Output 'Running as Administrator!'}
else
{Write-Output 'Running Limited!'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment