将带空格的字符串作为参数传递给 PowerShell 函数

Jos*_*arl 3 powershell

我有一个非常简单的 PowerShell 函数,用于给自己做笔记:

New-Alias Note CreateNote    
function CreateNote ( [string]$note )
{
    $message = "`n" + $note
    Add-Content C:\notes.txt $message 
    Write-Host "Saved note."
}
Run Code Online (Sandbox Code Playgroud)

只要我用带引号的字符串调用它,这就会很好用:

PS > Note "This is a note to myself."
Run Code Online (Sandbox Code Playgroud)

我真的很想能够省略引号,类似于如何Write-Host工作:

PS > Write-Host This is a note to myself.
This is a note to myself.
Run Code Online (Sandbox Code Playgroud)

如果我将参数作为数组处理并在将它们附加到文本文件之前将它们连接起来,这似乎是可行的。有一个更好的方法吗?

jon*_*n Z 5

你可以用它$args来完成这个:

function CreateNote
{
    $message = "`n" + $args
    Add-Content D:\notes.txt $message 
    Write-Host "Saved note."
}

CreateNote this is a test
Run Code Online (Sandbox Code Playgroud)