是否可以在 Powershell 多行命令中使用注释?
Is it possible to use comments in Powershell multiple line commands?
在 Powershell ISE 中调试和测试多行命令多年来一直困扰着我。我喜欢多行命令,因为它们易于阅读,但它们使事情更难调试。例如,我使用以下命令来获取早于 $days
的文件夹(顺便说一下,这是可行的)。
$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 `
| Where CreationTime -gt (Get-Date).AddDays(-1 * $days) `
| Sort-Object -Property LastWriteTime
我想将 AddDays
更改为 AddMinutes
以测试不同的结果集,但我想保留原始行以便我可以轻松地来回切换。下面我复制了我想保留的行并将其注释掉,并在新行上将 AddDays
更改为 AddMinutes
添加 #
会破坏多行功能。有没有一种简单的方法可以解决这个问题,我不必剪切复制的行并将其移动到命令的 "out" ?或者有没有办法 split/unsplit 一个命令进出多行?
$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 `
# | Where CreationTime -gt (Get-Date).AddDays(-1 * $days) `
| Where CreationTime -gt (Get-Date).AddMinutes(-1 * $days) `
| Sort-Object -Property LastWriteTime
(由于注释掉了行,以上内容不起作用)
使用多行注释语法代替#。
<# comment #>
这应该允许您在多行命令中注释文本。
但是,这仅在您使用 Powershell 2.0 时有效
试试这个,它可以作为多行注释示例包含在内
$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 `
<# | Where CreationTime -gt (Get-Date).AddDays(-1 * $days) #> ` | Where CreationTime -gt (Get-Date).AddMinutes(-1 * $days) `
| Sort-Object -Property LastWriteTime
你的问题是 [icky, nasty] 反引号。 [grin] powershell 知道 管道后面还有更多......所以如果你把管道放在被管道传输的段的末尾。像这样...
$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 |
# Where CreationTime -gt (Get-Date).AddDays(-1 * $days) |
Where CreationTime -gt (Get-Date).AddMinutes(-1 * $days) |
Sort-Object -Property LastWriteTime
因为 powershell 期望在 |
或 ,
之后继续
作为一行中的最后一个字符,您不需要反引号和
您可以使用不同的格式,然后在更长的管道中的单行注释仍然有效:
$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 |
# Where CreationTime -gt (Get-Date).AddDays(-1 * $days) |
Where CreationTime -gt (Get-Date).AddMinutes(-1 * $minutes) |
Sort-Object -Property LastWriteTime
在 Powershell ISE 中调试和测试多行命令多年来一直困扰着我。我喜欢多行命令,因为它们易于阅读,但它们使事情更难调试。例如,我使用以下命令来获取早于 $days
的文件夹(顺便说一下,这是可行的)。
$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 `
| Where CreationTime -gt (Get-Date).AddDays(-1 * $days) `
| Sort-Object -Property LastWriteTime
我想将 AddDays
更改为 AddMinutes
以测试不同的结果集,但我想保留原始行以便我可以轻松地来回切换。下面我复制了我想保留的行并将其注释掉,并在新行上将 AddDays
更改为 AddMinutes
添加 #
会破坏多行功能。有没有一种简单的方法可以解决这个问题,我不必剪切复制的行并将其移动到命令的 "out" ?或者有没有办法 split/unsplit 一个命令进出多行?
$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 `
# | Where CreationTime -gt (Get-Date).AddDays(-1 * $days) `
| Where CreationTime -gt (Get-Date).AddMinutes(-1 * $days) `
| Sort-Object -Property LastWriteTime
(由于注释掉了行,以上内容不起作用)
使用多行注释语法代替#。
<# comment #>
这应该允许您在多行命令中注释文本。
但是,这仅在您使用 Powershell 2.0 时有效
试试这个,它可以作为多行注释示例包含在内
$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 `
<# | Where CreationTime -gt (Get-Date).AddDays(-1 * $days) #> ` | Where CreationTime -gt (Get-Date).AddMinutes(-1 * $days) `
| Sort-Object -Property LastWriteTime
你的问题是 [icky, nasty] 反引号。 [grin] powershell 知道 管道后面还有更多......所以如果你把管道放在被管道传输的段的末尾。像这样...
$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 |
# Where CreationTime -gt (Get-Date).AddDays(-1 * $days) |
Where CreationTime -gt (Get-Date).AddMinutes(-1 * $days) |
Sort-Object -Property LastWriteTime
因为 powershell 期望在 |
或 ,
之后继续
作为一行中的最后一个字符,您不需要反引号和
您可以使用不同的格式,然后在更长的管道中的单行注释仍然有效:
$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 |
# Where CreationTime -gt (Get-Date).AddDays(-1 * $days) |
Where CreationTime -gt (Get-Date).AddMinutes(-1 * $minutes) |
Sort-Object -Property LastWriteTime