From b9a35cef9701a6b6f8431f4bb3387212065c5382 Mon Sep 17 00:00:00 2001 From: sharpchen Date: Sun, 17 Nov 2024 17:03:37 +0800 Subject: [PATCH] grrr --- docs/document/Powershell/docs/1.Overview.md | 1 + docs/document/Powershell/docs/Alias.md | 3 + .../Array.md => Environment/1.Overview.md} | 0 .../docs/File System/2.Working Directory.md | 4 +- .../docs/File System/3.Special Folders.md | 19 ++++++ .../{3.Create Item.md => 4.Create Item.md} | 0 .../docs/File System/5.Write to File.md | 13 ++++ .../docs/File System/6.Serialization.md | 31 +++++++++ .../Comment.md => Language/Array.md} | 0 .../Powershell/docs/Language/Comment.md | 6 ++ .../Evaluation.md | 0 .../Function.md | 0 .../Operators.md | 0 .../Powershell/docs/Language/String.md | 9 +++ .../String.md => Modulization/1.Overview.md} | 0 .../docs/Modulization/Command Conflicts.md | 36 +++++++++++ .../docs/Modulization/Load Modules.md | 63 +++++++++++++++++++ .../Powershell/docs/Modulization/Modules.md | 42 +++++++++++++ .../docs/Object Manipulation/1.Overview.md | 9 +++ .../Object Manipulation/Compare Objects.md | 49 +++++++++++++++ .../Object Manipulation/Object Members.md | 8 +++ .../docs/Object Manipulation/Select.md | 1 + .../Powershell/docs/Registry/1.Overview.md | 0 23 files changed, 292 insertions(+), 2 deletions(-) rename docs/document/Powershell/docs/{Powershell as a Language/Array.md => Environment/1.Overview.md} (100%) create mode 100644 docs/document/Powershell/docs/File System/3.Special Folders.md rename docs/document/Powershell/docs/File System/{3.Create Item.md => 4.Create Item.md} (100%) create mode 100644 docs/document/Powershell/docs/File System/5.Write to File.md create mode 100644 docs/document/Powershell/docs/File System/6.Serialization.md rename docs/document/Powershell/docs/{Powershell as a Language/Comment.md => Language/Array.md} (100%) create mode 100644 docs/document/Powershell/docs/Language/Comment.md rename docs/document/Powershell/docs/{Powershell as a Language => Language}/Evaluation.md (100%) rename docs/document/Powershell/docs/{Powershell as a Language => Language}/Function.md (100%) rename docs/document/Powershell/docs/{Powershell as a Language => Language}/Operators.md (100%) create mode 100644 docs/document/Powershell/docs/Language/String.md rename docs/document/Powershell/docs/{Powershell as a Language/String.md => Modulization/1.Overview.md} (100%) create mode 100644 docs/document/Powershell/docs/Modulization/Command Conflicts.md create mode 100644 docs/document/Powershell/docs/Modulization/Load Modules.md create mode 100644 docs/document/Powershell/docs/Modulization/Modules.md create mode 100644 docs/document/Powershell/docs/Object Manipulation/1.Overview.md create mode 100644 docs/document/Powershell/docs/Object Manipulation/Compare Objects.md create mode 100644 docs/document/Powershell/docs/Object Manipulation/Object Members.md create mode 100644 docs/document/Powershell/docs/Object Manipulation/Select.md create mode 100644 docs/document/Powershell/docs/Registry/1.Overview.md diff --git a/docs/document/Powershell/docs/1.Overview.md b/docs/document/Powershell/docs/1.Overview.md index aa98f901..1eb5d3c5 100644 --- a/docs/document/Powershell/docs/1.Overview.md +++ b/docs/document/Powershell/docs/1.Overview.md @@ -2,3 +2,4 @@ - Dynamic typing - Case insensitive +- Everything is object, more than plain text in shell. diff --git a/docs/document/Powershell/docs/Alias.md b/docs/document/Powershell/docs/Alias.md index ea817b3c..db4d2e96 100644 --- a/docs/document/Powershell/docs/Alias.md +++ b/docs/document/Powershell/docs/Alias.md @@ -46,6 +46,9 @@ Get-Alias | Where-Object { $_.Options -match 'ReadOnly|Constant' } > [!note] > Do not use custom aliases for public repository. +> [!CAUTION] +> Not all builtin aliases are available in all system. See [removed aliases](https://learn.microsoft.com/en-us/powershell/scripting/whats-new/unix-support?view=powershell-7.4#aliases-not-available-on-linux-or-macos) + ## Differ from Bash Alias in powershell is only a name alias for a command, it can never take any predetermined parameters or flags like in bash. diff --git a/docs/document/Powershell/docs/Powershell as a Language/Array.md b/docs/document/Powershell/docs/Environment/1.Overview.md similarity index 100% rename from docs/document/Powershell/docs/Powershell as a Language/Array.md rename to docs/document/Powershell/docs/Environment/1.Overview.md diff --git a/docs/document/Powershell/docs/File System/2.Working Directory.md b/docs/document/Powershell/docs/File System/2.Working Directory.md index b5c9731b..326bf580 100644 --- a/docs/document/Powershell/docs/File System/2.Working Directory.md +++ b/docs/document/Powershell/docs/File System/2.Working Directory.md @@ -22,12 +22,12 @@ Alias sl -> Set-Location There's two ways to get current working directory in Powershell. -- `$PWD` builtin string variable. Refreshes on each time location changed. +- `$PWD` builtin `PathInfo` variable. Refreshes on each time location changed. - `Get-Location` cmdlet. Returns a `PathInfo` object. - `pwd` is an builtin alias for `Get-Location`. ```ps1 -(Get-Location).Path # equivelant to $pwd +(Get-Location).Path # equivalent to $pwd ``` ```console diff --git a/docs/document/Powershell/docs/File System/3.Special Folders.md b/docs/document/Powershell/docs/File System/3.Special Folders.md new file mode 100644 index 00000000..f8cadd0b --- /dev/null +++ b/docs/document/Powershell/docs/File System/3.Special Folders.md @@ -0,0 +1,19 @@ +# Special Folders + +## Home Directory + +- `$HOME` +- `$env:USERPROFILE` + +## Temp Folder + +- `$env:TMP` +- `$env:TEMP` + +## APPDATA + +- `$env:APPDATA` + +## LOCALAPPDATA + +- `$env:LOCALAPPDATA` diff --git a/docs/document/Powershell/docs/File System/3.Create Item.md b/docs/document/Powershell/docs/File System/4.Create Item.md similarity index 100% rename from docs/document/Powershell/docs/File System/3.Create Item.md rename to docs/document/Powershell/docs/File System/4.Create Item.md diff --git a/docs/document/Powershell/docs/File System/5.Write to File.md b/docs/document/Powershell/docs/File System/5.Write to File.md new file mode 100644 index 00000000..c6e17f65 --- /dev/null +++ b/docs/document/Powershell/docs/File System/5.Write to File.md @@ -0,0 +1,13 @@ +# Write to File + + + +## Redirection Operator + +Redirection operator in powershell is a shorthand for `Out-File`, to mimic the same thing in bash. + +```ps1 +ls > foo.txt +# equivalent to +ls | Out-File foo.txt +``` diff --git a/docs/document/Powershell/docs/File System/6.Serialization.md b/docs/document/Powershell/docs/File System/6.Serialization.md new file mode 100644 index 00000000..0bd58b5d --- /dev/null +++ b/docs/document/Powershell/docs/File System/6.Serialization.md @@ -0,0 +1,31 @@ +# Serialization + +> [!TIP] +> For deserialization, see `gcm -Verb Import` + +## JSON + +```ps1 +ls | ConverTo-JSON +ls | ConverTo-JSON > foo.json +``` + +## XML + +Powershell has a special xml format for itself called `Clixml`, this is specific to powershell. + +```ps1 +ls | Export-Clixml foo.xml +``` + + +## CSV + +```ps1 +ls | ConverTo-Csv > foo.csv +ls | Export-Csv foo.csv +``` + +## HTML + +## TXT diff --git a/docs/document/Powershell/docs/Powershell as a Language/Comment.md b/docs/document/Powershell/docs/Language/Array.md similarity index 100% rename from docs/document/Powershell/docs/Powershell as a Language/Comment.md rename to docs/document/Powershell/docs/Language/Array.md diff --git a/docs/document/Powershell/docs/Language/Comment.md b/docs/document/Powershell/docs/Language/Comment.md new file mode 100644 index 00000000..c8dad1b8 --- /dev/null +++ b/docs/document/Powershell/docs/Language/Comment.md @@ -0,0 +1,6 @@ +# Comment + +## Single Line + +## Multiple Lines + diff --git a/docs/document/Powershell/docs/Powershell as a Language/Evaluation.md b/docs/document/Powershell/docs/Language/Evaluation.md similarity index 100% rename from docs/document/Powershell/docs/Powershell as a Language/Evaluation.md rename to docs/document/Powershell/docs/Language/Evaluation.md diff --git a/docs/document/Powershell/docs/Powershell as a Language/Function.md b/docs/document/Powershell/docs/Language/Function.md similarity index 100% rename from docs/document/Powershell/docs/Powershell as a Language/Function.md rename to docs/document/Powershell/docs/Language/Function.md diff --git a/docs/document/Powershell/docs/Powershell as a Language/Operators.md b/docs/document/Powershell/docs/Language/Operators.md similarity index 100% rename from docs/document/Powershell/docs/Powershell as a Language/Operators.md rename to docs/document/Powershell/docs/Language/Operators.md diff --git a/docs/document/Powershell/docs/Language/String.md b/docs/document/Powershell/docs/Language/String.md new file mode 100644 index 00000000..95f69e6b --- /dev/null +++ b/docs/document/Powershell/docs/Language/String.md @@ -0,0 +1,9 @@ +# String + +## String Interpolation + +## Raw String + +## String Concatenation + +## Character Escaping diff --git a/docs/document/Powershell/docs/Powershell as a Language/String.md b/docs/document/Powershell/docs/Modulization/1.Overview.md similarity index 100% rename from docs/document/Powershell/docs/Powershell as a Language/String.md rename to docs/document/Powershell/docs/Modulization/1.Overview.md diff --git a/docs/document/Powershell/docs/Modulization/Command Conflicts.md b/docs/document/Powershell/docs/Modulization/Command Conflicts.md new file mode 100644 index 00000000..7947c528 --- /dev/null +++ b/docs/document/Powershell/docs/Modulization/Command Conflicts.md @@ -0,0 +1,36 @@ +# Command Conflicts + +Commands from modules usually ships with a prefixed name to distinguish name conflicts. +`Az` module for example, it always prefix its commands with `Az`. + +```console +$ gcm Get*Az* | select -First 5 + +CommandType Name Version Source +----------- ---- ------- ------ +Alias Get-AzADServicePrincipalCredential 7.6.0 Az.Resources +Alias Get-AzAksClusterUpgradeProfile 6.0.4 Az.Aks +Alias Get-AzApplicationGatewayAvailableSslOptions 7.10.0 Az.Network +Alias Get-AzApplicationGatewayAvailableWafRuleSets 7.10.0 Az.Network +Alias Get-AzApplicationGatewayBackendHttpSettings 7.10.0 Az.Network +``` + +## Command Fullname + +One solution is specifying the fullname of the command. + +```ps1 +ModuleName\Get-Foo # use `\` as separator +``` + +## Add Custom Prefix + +If a module doesn't have a prefix for its cmdlet, you can customize prefix for it on import. + +```ps1 +ipmo '' -Prefix Foo +# assuming `Get-Bar` is from `` +# `Get-Bar` -> `Get-FooBar` +Get-FooBar +``` + diff --git a/docs/document/Powershell/docs/Modulization/Load Modules.md b/docs/document/Powershell/docs/Modulization/Load Modules.md new file mode 100644 index 00000000..3c09ee56 --- /dev/null +++ b/docs/document/Powershell/docs/Modulization/Load Modules.md @@ -0,0 +1,63 @@ +# Load Modules + +## Custom Module Path + +Powershell automatically scans modules at certain locations. +Inspect existing module path by: + +:::code-group +```ps1[Windows] +$env:PSModulePath -split ';' # windows +``` +```ps1[Linux/Mac] +$env:PSModulePath -split ':' # linux/mac +``` +::: + +You can add your module location inside your profile file. + +```ps1 +$env:PSModulePath += [IO.Path]::PathSeparator + '' +``` + +## Lazy Loading + +Powershell lazily loads a module when member from it is called. + +## Loaded Modules + +Inspect all loaded modules in current session: + +```ps1 +Get-Module +``` + +Check whether certain module name is loaded: + +```ps1 +Get-Module -Name '' +``` + +> [!TIP] +> Use `gmo` alias for `Get-Module`. + +## Import Modules + +Import a module in current session. + +```ps1 +Import-Module -Name '' +``` +> [!TIP] +> Use `ipmo` alias for `Import-Module`. + +## Unload Modules + +Unload a module from current session. + +```ps1 +Remove-Module '' +``` + +> [!TIP] +> Use `rmo` alias for `Remove-Module`. diff --git a/docs/document/Powershell/docs/Modulization/Modules.md b/docs/document/Powershell/docs/Modulization/Modules.md new file mode 100644 index 00000000..60dae560 --- /dev/null +++ b/docs/document/Powershell/docs/Modulization/Modules.md @@ -0,0 +1,42 @@ +# Modules + +## Add Module Repository + +Module repository is a registry of powershell modules. +It can be public or privately hosted by yourself. + +```ps1 +Register-PSRepository +``` + +## Install a Module + +```ps1 +Install-Module +``` + +## Search Modules + +Search module to be installed in registered repositories. + +```ps1 +Find-Module +``` + +## Custom Module Path + +Powershell automatically scans modules at certain locations. +Inspect existing module path by: + +:::code-group +```ps1[Windows] +$env:PSModulePath -split ';' # windows +``` +```ps1[Linux/Mac] +$env:PSModulePath -split ':' # linux/mac +``` +::: + +```ps1 +$env:PSModulePath += [IO.Path]::PathSeparator + '' +``` diff --git a/docs/document/Powershell/docs/Object Manipulation/1.Overview.md b/docs/document/Powershell/docs/Object Manipulation/1.Overview.md new file mode 100644 index 00000000..606e1763 --- /dev/null +++ b/docs/document/Powershell/docs/Object Manipulation/1.Overview.md @@ -0,0 +1,9 @@ +# Overview + +Powershell is a Object-Oriented shell language, so inspection and manipulation on objects is the fundamental capability to work with. + +This section focuses on how to work with objects, including: + +- LINQ like transformation over objects. +- Object comparison. +- Serialization and Deserialization. diff --git a/docs/document/Powershell/docs/Object Manipulation/Compare Objects.md b/docs/document/Powershell/docs/Object Manipulation/Compare Objects.md new file mode 100644 index 00000000..09a44daf --- /dev/null +++ b/docs/document/Powershell/docs/Object Manipulation/Compare Objects.md @@ -0,0 +1,49 @@ +# Compare Objects + +> [!TIP] +> Use `diff` or `compare` alias for `Compare-Object` + +`Compare-Object` is a cmdlet to compare two sources of objects and returns a indicator implying the difference between the two. + +`-ReferenceObject` is the base object to compare, `-DifferenceObject` is the right hand side. + +```ps1 +Compare-Object -ReferenceObject @('abc', 'dfg', 4) -DifferenceObject @('fg', 'abc', 1) +``` + +```console +InputObject SideIndicator +----------- ------------- +fg => +1 => +dfg <= +4 <= +``` + +- `<=` implies the difference content came from the `-ReferenceObject` +- `=>` implies the difference content came from the `-DifferenceObject` +- `==` implies the content appeared in both sides. + +> [!NOTE] +> `Compare-Object` hides `==` case by default. Use `-IncludeEqual` to display it. And `-ExcludeDifferent` to to hide different cases. +```ps1 +diff -ref @('abc', 'dfg', 4) -diff @('fg', 'abc', 1) -IncludeEqual +``` + +## Compare by Property + +You can specify which property to compare. + +```ps1 +diff @('abc', 'dfg') @('fg', 'abc') -Property Length # compare on string.Length +``` + +## Comparison Solution + +`Compare-Object` will try finding a method to do the comparison, falls back as following: + +- `IComparable` interface +- `ToString()` and compare on string + +> [!TIP] +> If the object to be compared doesn't implement `IComparable`, you should use `-Property`. diff --git a/docs/document/Powershell/docs/Object Manipulation/Object Members.md b/docs/document/Powershell/docs/Object Manipulation/Object Members.md new file mode 100644 index 00000000..bab37c82 --- /dev/null +++ b/docs/document/Powershell/docs/Object Manipulation/Object Members.md @@ -0,0 +1,8 @@ +# Object Members + +> [!TIP] +> Use `gm` alias for `Get-Member`. + +```ps1 +ls | Get-Member +``` diff --git a/docs/document/Powershell/docs/Object Manipulation/Select.md b/docs/document/Powershell/docs/Object Manipulation/Select.md new file mode 100644 index 00000000..9dfe3fd8 --- /dev/null +++ b/docs/document/Powershell/docs/Object Manipulation/Select.md @@ -0,0 +1 @@ +# Select diff --git a/docs/document/Powershell/docs/Registry/1.Overview.md b/docs/document/Powershell/docs/Registry/1.Overview.md new file mode 100644 index 00000000..e69de29b