I work with several large brown field PowerShell projects, which were developed by different peoples, both amateurs and pro in PS. I saw different, ugly and beautiful things.
The most confusing are one char syntax, that powershell is rich of. Some of this are common, some of this are rare and even redundant.
Here is short listing:
- # Explicit provider
- Get-Content -Path Function:prompt
- # Old good string format
- 'This is my post about {1} number {0}' -F 3, 'powershell'
- # Escaping
- "First line`r`nSecond one"
- # Explicit scope
- $Global:test = 'GlobalTest'
- # Hashtable
- $hash = @{ Name = 'Hashtable'; Description = 'Ordinary Collections.Hashtable'; }
- # Call operators
- & 'Get-Process'
- . 'Get-Process'
- # Use bool in switch argument
- $someBool = $true
- New-Item -Path Variable:SwitchTest -Value 1 -Verbose:$someBool
- # Simple scriptblock
- $sb = { Get-Process }
- # Foreach-Object and Where-Object aliases
- Get-Process | ?{ $_.PM -gt 1000 } | %{ $_.ProcessName }
Explicit provider
- Get-Content -Path Function:prompt
So you can iterate current variables and functions as well, try it:
- Get-ChildItem Function:*
- Get-ChildItem Variable:*
PS C:\Windows>
You can reassign it, to show some additional or interesting information.
PS — If you like to use your own prompt function by default, you can add it's assigning to Profile.ps1 file in powershell folder, usually C:\Windows\System32\WindowsPowerShell\v1.0\.
String format
- 'This is my post about {1} number {0}' -F 3, 'powershell'
Escaping
- "First line`r`nSecond one"
Explicit scopes
- $Global:test = 'GlobalTest'
- Global is like public
- Script is like internal
- Private is private
- Local is current stack level
- Numbered scopes are from 0 (same to Local) to 1..n where each step is up to stack level
- $test = 'Global Scope'
- Function Foo {
- $test = 'Function Scope'
- Write-Host $Global:test # Global Scope
- Write-Host $Local:test # Function Scope
- Write-Host (Get-Variable -Name test -ValueOnly -Scope 0) # Function Scope
- Write-Host (Get-Variable -Name test -ValueOnly -Scope 1) # Global Scope
- }
- Foo
Hashtables
- $hash = @{
- Name = 'Hashtable';
- Description = 'Ordinary Collections.Hashtable';
- }
- $hash.Description # Will return 'Ordinary Collections.Hashtable'
- $hash.NewField = 'Some new field'
Call operators
- & 'Get-Process'
- . 'Get-Process'
- $app = Read-Host "Input application name or exe path: "
- . $app
When you invoke powershell script file (*.ps1) by dot, it's content added to local scope, but, script invoked by ampersand will not be added to any scope.
- . C:\SomeScript.ps1 # Script functions and variables will be added to current (local) scope
- & C:\SomeOtherScript.ps1 # Script not added to scope at all
Bool to switch
- $someBool = $true
- New-Item -Path Variable:SwitchTest -Value 1 -Verbose:$someBool
- Function Write-HelloWorld {
- Param(
- [switch]$Elite
- )
- If ($Elite) {
- Write-Host 'h3110 w0r1d'
- } Else {
- Write-Host 'Hello world'
- }
- }
- Write-HelloWorld -Elite # Elite-like
- Write-HelloWorld # Simple
Scriptblock
- $sb = { Get-Process }
ScriptBlocks are base of PowerShell.
ForEach-Object and Where-Object
- Get-Process | ?{ $_.PM -gt 1000 } | %{ $_.ProcessName }
There are actually plenty of particulars like that to take into consideration. That may be a nice level to convey up. I provide the ideas above as normal inspiration however clearly there are questions just like the one you bring up the place crucial factor can be working in trustworthy good faith. I don?t know if finest practices have emerged round issues like that, however I'm positive that your job is clearly identified as a good game. Both girls and boys feel the affect of just a moment’s pleasure, for the remainder of their lives. online casino bonus
ReplyDelete