Hoe becommentarieer je code in PowerShell?

Hoe becommentarieer je code in PowerShell(1.0 of 2.0)?


Antwoord 1, autoriteit 100%

In PowerShell V1 is er alleen #om van de tekst erna een opmerking te maken.

# This is a comment in Powershell

In PowerShell V2 kan <# #>worden gebruikt voor blokopmerkingen en meer specifiek voor hulpopmerkingen.

#REQUIRES -Version 2.0
<#
.SYNOPSIS
    A brief description of the function or script. This keyword can be used
    only once in each topic.
.DESCRIPTION
    A detailed description of the function or script. This keyword can be
    used only once in each topic.
.NOTES
    File Name      : xxxx.ps1
    Author         : J.P. Blanc ([email protected])
    Prerequisite   : PowerShell V2 over Vista and upper.
    Copyright 2011 - Jean Paul Blanc/Silogix
.LINK
    Script posted over:
    http://silogix.fr
.EXAMPLE
    Example 1
.EXAMPLE
    Example 2
#>
Function blabla
{}

Voor meer uitleg over .SYNOPSISen .*zie about_Comment_Based_Help.

Opmerking: deze functieopmerkingen worden gebruikt door de Get-HelpCmdLet en kunnen voor het trefwoord Functionworden geplaatst, of binnen de {}voor of na de code zelf.


Antwoord 2, autoriteit 8%

Je gebruikt het hekje als volgt

# This is a comment in Powershell

Wikipedia heeft een goede pagina om bij te houden hoe u opmerkingen kunt maken in verschillende populaire talen

http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(syntax)# Opmerkingen


Antwoord 3, autoriteit 3%

Eenregelige opmerkingen beginnen met een hekje, alles rechts van de #wordt genegeerd:

# Comment Here

In PowerShell 2.0 en hoger kunnen meerregelige blokopmerkingen worden gebruikt:

<# 
  Multi 
  Line 
#> 

Je zou blokopmerkingen kunnen gebruiken om commentaartekst in een commando in te sluiten:

Get-Content -Path <# configuration file #> C:\config.ini

Opmerking:omdat PowerShell Tab-voltooiingondersteunt, moet u voorzichtig zijn met kopiëren en plak Space + TABvoor opmerkingen.


Antwoord 4, autoriteit 3%

Het is de #.

Zie PowerShell – Speciale tekens en tokensvoor speciale tekens.


Antwoord 5

Hier

# Single line comment in Powershell
<# 
--------------------------------------
Multi-line comment in PowerShell V2+ 
-------------------------------------- 
#>

Antwoord 6

Binnen PowerShell ISE U kunt CTRL + J drukken om de te openen Start Snipping MENU en selecteer Commentaarblok :


Antwoord 7

Gebruik een hashtag gevolgd door een witte ruimte (!) Hiervoor:

# Comment here

Vergeet de witte ruimte hier niet! Anders kan het interne opdrachten verstoren.

E.g., Dit is niet een opmerking:

#requires -runasadmin

Antwoord 8

u kunt maken:

(Some basic code) # Use "#" after a line and use:
 <#
    for more lines
    ...
    ...
    ...
    ..
    .
 #>

Antwoord 9

Er is een speciale manier om opmerkingen in te voegen Voeg het einde van het script toe:

....
exit 
Hi
Hello
We are comments
And not executed 

Alles na exitwordt niet uitgevoerd en gedraagt ​​zich vrij van opmerkingen.


Antwoord 10

Ik ben een beetje laat op dit feest, maar lijkt erop dat niemand eigenlijk alle gebruiksgevallen schreef. Dus ...

Alleen ondersteunde versie van PowerShell tegenwoordig (daling van 2020 en meer dan ) zijn:

  • Windows PowerShell 5.1.x
  • POWERSHELL 7.0.X.

U wilt niet of u moet niet werken met verschillende versies van PowerShell.

Beide versies(of een andere versie die je zou kunnen hebben rond WPS 3.0-5.0, PS Core 6.xx op sommige verouderde stations) delen hetzelfde reactie functionaliteit.

Eén regel commentaar

# Get all Windows Service processes <-- one line comment, it starts with '#'
Get-Process -Name *host*
Get-Process -Name *host* ## You could put as many ### as you want, it does not matter
Get-Process -Name *host* # | Stop-Service # Everything from the first # until end of the line is treated as comment
Stop-Service -DisplayName Windows*Update # -WhatIf # You can use it to comment out cmdlet switches

Opmerkingen met meerdere regels

<#
Everyting between '< #' and '# >' is 
treated as a comment. A typical use case is for help, see below.
# You could also have a single line comment inside the multi line comment block.
# Or two... :)
#>
<#
.SYNOPSIS
    A brief description of the function or script.
    This keyword can be used only once in each topic.
.DESCRIPTION
    A detailed description of the function or script.
    This keyword can be used only once in each topic.
.NOTES
    Some additional notes. This keyword can be used only once in each topic.
    This keyword can be used only once in each topic.
.LINK
    A link used when Get-Help with a switch -OnLine is used.
    This keyword can be used only once in each topic.
.EXAMPLE
    Example 1
    You can use this keyword as many as you want.
.EXAMPLE
    Example 2
    You can use this keyword as many as you want.
#>

Geneste opmerkingen van meerdere regels

<#
Nope, these are not allowed in PowerShell.
<# This will break your first multiline comment block... #>
...and this will throw a syntax error.
#>

In code geneste opmerkingen met meerdere regels

<# 
The multi line comment opening/close
can be also used to comment some nested code
or as an explanation for multi chained operations..
#>
Get-Service | <# Step explanation #>
Where-Object { $_.Status -eq [ServiceProcess.ServiceControllerStatus]::Stopped } | 
<# Format-Table -Property DisplayName, Status -AutoSize |#>
Out-File -FilePath Services.txt -Encoding Unicode

Edge case-scenario

# Some well written script
exit
Writing something after exit is possible but not recommended.
It isn't a comment.
Especially in Visual Studio Code, these words baffle PSScriptAnalyzer.
You could actively break your session in VS Code.

Other episodes