1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
#requires -version 2.0
###########################
# Comments
###########################
# Normal Comment
<#
multi
line
comment
#>
###########################
## Strings
###########################
"String that accepts $variables, and substitutions"
"String with escaped `$variable"
'Literal string $($PSScriptRoot)'
###########################
# Heredoc strings
###########################
@"
Heredoc with $variables
"@
@'
Heredoc with $literal strings
'@
###########################
# Variables
###########################
$var1 = 5
$var2 = $true
$var3 = get-host | Select-Object -First 1
${this-is_a%valid*variable@name} = "OMG"
Write-Output ${this-is_a%valid*variable@name}
$global:ErrorActionPreference
###########################
# Collections
###########################
$my_hash = @{
owner = "my"
type = "hash"
thing = "table"
}
$my_complex_hash = @{
# comment
foo = {
if ($var1 -eq $var2)
{
return $true
}
}
}
$my_array = @("my" "array")
$myobject = [PSCustomObject]@{
Name = 'Alice';
Age = 42;
}
$myarray = @(
[PSCustomObject]@{
Name = 'Bob';
Age = 31;
},
[PSCustomObject]@{
Name = 'Charlie';
Age = 41;
}
)
###########################
# Commands
###########################
type "is a reserved word"
alias foo bar
Write-output "Hello" # End-of-line comment
Get-Process | Tee-Object -FilePath "C:\Test1\testfile2.txt" # Unapproved verb
cd C:\Test1\
C:\Test1\test2.cmd input.txt
& "C:\Test1\test2.exe"
\command.exe something
command.exe arg --user ".\ENTER-YOUR-USERNAME" --password "ENTER-YOUR-PASSWORD"
###########################
# Declarations
###########################
function Verb-Noun
{
<#
.SYNOPSIS
Tells you what it does
.DESCRIPTION
Tells you what it does with more detail.
#>
param ([string]$Name, [string]$Extension = "txt", [string]$foo="bar")
$name = $name + "." + $extension
$name
}
function global:Verb-Noun {
something
}
process {
"This is a script block"
}
###########################
# Control structures
###########################
if ($var1 -eq $var2)
{
return $true
}
try {
write-output "something"
} catch {
write-output "something else"
}
Foreach ($Thing in $Things ) {
Do-Something
}
###########################
# Classes
###########################
class Child : Parent, Relatives {
[int] hidden $var = 8
Child ([string]$a, [string]$b, [int]$capacity) {
$this.var = $a
}
[string]toString() {
return "A string"
}
}
$my_obj = [Child]::new()
###########################
# Challenges
###########################
[System.Collections.Generic.List[string]](1..100)
[System.Collections.Generic.Dictionary[string, int]](1..100|%{@{N=$_}})
Function Get-IPv4Scopes
<#
.SYNOPSIS
Read IPv4Scopes from an array of servers
.PARAMETER Servers
Specifies an array of servers
.EXAMPLE
Get-IPv4Scopes
Will prompt for all inputs
#>
{
[CmdletBinding()]
Param(
# 1
[parameter(
Mandatory=$true,
Position=0,
ValueFromPipelineByPropertyName=$true,
HelpMessage="Server List"
)]
[string[]]$Servers,
#2
[parameter(Mandatory=$false,ValueFromPipeline=$false)]
[bool]$Unique=$false
) #EndParam
Begin {}
Process {
$arrayJobs=@()
foreach ($server in $Servers) {
$arrayJobs+=Invoke-Command -ComputerName $server -scriptblock {Get-DhcpServerv4Scope} -AsJob
}
$complete=$false
while (-not $complete) {
$arrayJobsInProgress= $arrayJobs | Where-Object { $_.State -match 'running' }
if (-not $arrayJobsInProgress) { $complete=$true }
}
$Scopes=$arrayJobs|Receive-Job
$UniqueScopes=$Scopes|Sort-Object -Property ScopeId -Unique
}
End {
if ($Unique) { return $UniqueScopes }
else { return $Scopes }
}
} #end function
Write-Output "Updating: $($file.FullName)"
Write-Output "PowerShell PID: $(Get-Process powershell | Select -ExpandProperty Id) and Notepad PID: $(Get-Process notepad | Select -ExpandProperty Id)"
# $content = Get-Content $file.FullName
# Without Error
$gitUserName = "$($userAdObject.Properties['sn']), $($userAdObject.Properties['givenName'])"
# Grave escaping
$gitExeString = "$gitExeFolder\git.exe" &("$gitExeString") config --add --global user.name `"$gitUserName`"
Get-ChildItem -Include *.txt `
-Recurse
Write-Output `$hello
# Where-Object alias
Get-process | ? {$_.workingset -gt 25000*1024}
# Handle subexpressions nested within strings
function Write-Log {
Param(
$Message,
$Path = "L:\IISSiteAudit.txt"
)
function TS { return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date) }
"$(TS) $Message" | Tee-Object -FilePath $Path -Append | Write-Verbose
}
function NewVMSnapshot {
param(
[Parameter(Mandatory = $true)]
[string[]]$VM,
[string[]]$Name = (Get-Date -Format 'yyyy-MM-dd_HH:mm:ss') + "_$VM",
[string[]]$Description = "`Snapshot taken via $(Split-Path $PSCommandPath -leaf) by $global:vsphereuser."
)
}
# Handle line continuation in string interpolation
Get-CimInstance -ComputerName localhost win32_logicaldisk `
| where caption -eq "C:" `
| foreach-object {write " $($_.caption) $('{0:N2}' `
-f ($_.Size/1gb)) GB total, $('{0:N2}' `
-f ($_.FreeSpace/1gb)) GB free "}
|