summaryrefslogtreecommitdiffhomepage
path: root/powershell.html.markdown
diff options
context:
space:
mode:
authorAndrew Ryan Davis <AndrewDavis1191@gmail.com>2020-08-16 21:14:26 -0700
committerAndrew Ryan Davis <AndrewDavis1191@gmail.com>2020-08-20 12:30:32 -0700
commite6a53387bee66c4315c22354cd5a3edbbb0c6018 (patch)
tree0d5a6bf872f9d1ccae02bf2c99702d3b4b05c236 /powershell.html.markdown
parent5c79b4288c9c1de5db6e1eb3856191f9381f7d4f (diff)
Addressing comments
1. Clarifying '[]' and null-assigned expressions. 2. Removing "-contains" on switch example from Wouter, it does not appear to be true anymore 3. Adding comments and adjusting some func examples a bit
Diffstat (limited to 'powershell.html.markdown')
-rw-r--r--powershell.html.markdown44
1 files changed, 36 insertions, 8 deletions
diff --git a/powershell.html.markdown b/powershell.html.markdown
index 7c54dc44..2d4c65b7 100644
--- a/powershell.html.markdown
+++ b/powershell.html.markdown
@@ -151,23 +151,47 @@ $null # => None
# $null, 0, and empty strings and arrays all evaluate to False.
# All other values are True
+# $null, 0, and empty strings and arrays all evaluate to False.
+# All other values are True
function test ($value) {
- if ($value) {Write-Output 'True'}
- else {Write-Output 'False'}
+ if ($value) {
+ Write-Output 'True'
+ }
+ else {
+ Write-Output 'False'
+ }
}
+
test ($null) # => False
test (0) # => False
test ("") # => False
-test [] # => True
+test [] # => True *[] Not valid in Powershell, creates null-valued expression
test ({}) # => True
test @() # => False
+<#
+IsPublic IsSerial Name BaseType
+-------- -------- ---- --------
+True True Int32 System.ValueType
+True True Int32 System.ValueType
+True True String System.Object
+You cannot call a method on a null-valued expression.
+At line:4 char:1
++ $test3.getType()
++ ~~~~~~~~~~~~~~~~
+ + CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ + FullyQualifiedErrorId : InvokeMethodOnNull
+
+True True ScriptBlock System.Object
+True True Object[] System.Array
+#>
+
####################################################
## 2. Variables and Collections
####################################################
# Powershell uses the "Write-Output" function to print
-Write-Output "I'm Powershell. Nice to meet you!" # => I'm Powershell. Nice to meet you!
+Write-Output "I'm Posh. Nice to meet you!" # => I'm Posh. Nice to meet you!
# Simple way to get input data from console
$userInput = Read-Host "Enter some data: " # Returns the data as a string
@@ -367,7 +391,6 @@ switch($val) {
{ $_ -like 's*' } { "Case insensitive"; break }
{ $_ -clike 's*'} { "clike, ceq, cne for case sensitive"; break }
{ $_ -notmatch '^.*$'} { "Regex matching. cnotmatch, cnotlike, ..."; break }
- { 'x' -contains 'x'} { "FALSE! -contains is for lists!"; break }
default { "Others" }
}
@@ -384,7 +407,6 @@ finally {
}
-
# Writing to a file
$contents = @{"aa"= 12
"bb"= 21}
@@ -532,6 +554,7 @@ True False Guitar Instrument
#>
+
####################################################
## 7. Advanced
####################################################
@@ -582,7 +605,7 @@ $Area
This is a silly one
You may one day be asked to create a func that could take $start and $end
and reverse anything in an array within the given range
- based on an arbitrary array
+ based on an arbitrary array without mutating the original array
Let's see one way to do that and introduce another data structure
#>
@@ -599,7 +622,7 @@ function Format-Range ($start, $end) {
elseif ($index -ge $start -and $index -le $end) {
$stack.Push($targetArray[$index])
}
- elseif ($index -gt $end) {
+ else {
$secondSectionArray.Add($targetArray[$index]) > $null
}
}
@@ -607,6 +630,8 @@ function Format-Range ($start, $end) {
Write-Output $returnArray
}
+Format-Range 2 6 # => 'a','b','g','f','e','d','c','h','i','j','k','l','m','n'
+
# The previous method works, but it uses extra memory by allocating new arrays
# It's also kind of lengthy
# Let's see how we can do this without allocating a new array
@@ -623,10 +648,13 @@ function Format-Range ($start, $end) {
}
return $targetArray
}
+
+Format-Range 2 6 # => 'a','b','g','f','e','d','c','h','i','j','k','l','m','n'
```
Powershell as a Tool:
Getting Help:
+
```Powershell
# Find commands
Get-Command about_* # alias: gcm