lab*_*nth 913 syntax powershell commenting powershell-2.0
你如何在PowerShell(1.0或2.0)中注释掉代码?
JPB*_*anc 1221
在PowerShell V1中,只有#在评论后才能生成文本.
# This is a comment in Powershell
Run Code Online (Sandbox Code Playgroud)
在PowerShell V2 <# #>中,可以用于块注释,更具体地说,用于帮助注释.
#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 (jean-paul_blanc@silogix-fr.com)
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
{}
Run Code Online (Sandbox Code Playgroud)
有关详细信息.SYNOPSIS,.*请参阅about_Comment_Based_Help.
备注:这些功能的意见是由使用的Get-Helpcmdlet,并可以把关键字之前Function,或内部{}之前或代码本身之后.
ada*_*ich 97
你使用这样的哈希标记
# This is a comment in Powershell
Run Code Online (Sandbox Code Playgroud)
维基百科有一个很好的页面,可以跟踪如何用几种流行语言进行评论
http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(syntax)#Comments
Ale*_*der 33
单行注释以井号开头,#将忽略右侧的所有内容:
# Comment Here
Run Code Online (Sandbox Code Playgroud)
在PowerShell 2.0及更高版本中,可以使用多行块注释:
<#
Multi
Line
#>
Run Code Online (Sandbox Code Playgroud)
您可以使用块注释在命令中嵌入注释文本:
Get-Content -Path <# configuration file #> C:\config.ini
Run Code Online (Sandbox Code Playgroud)
注意:由于PowerShell支持Tab完成,因此Space + TAB在注释之前需要注意复制和粘贴.
Vic*_*Vic 15
这里
# Single line comment in Powershell
<#
--------------------------------------
Multi-line comment in PowerShell V2+
--------------------------------------
#>
Run Code Online (Sandbox Code Playgroud)
为此,请使用主题标签,后跟空格(!):
# Comment here
Run Code Online (Sandbox Code Playgroud)
不要忘记这里的空白!否则它可能会干扰内部命令。
例如,这不是评论:
#requires -runasadmin
Run Code Online (Sandbox Code Playgroud)
我参加这个聚会有点晚了,但似乎没有人真正编写所有用例。所以...
目前(2020 年秋季及以后)仅支持的 PowerShell 版本是:
您不想也不应该使用不同版本的 PowerShell。
这两个版本(或者您可能在某些过时的工作站上出现的 WPS 3.0-5.0、PS Core 6.xx 周围的任何其他版本)共享相同的评论功能。
# 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
Run Code Online (Sandbox Code Playgroud)
<#
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.
#>
Run Code Online (Sandbox Code Playgroud)
<#
Nope, these are not allowed in PowerShell.
<# This will break your first multiline comment block... #>
...and this will throw a syntax error.
#>
Run Code Online (Sandbox Code Playgroud)
<#
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
Run Code Online (Sandbox Code Playgroud)
# 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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
788038 次 |
| 最近记录: |