我是PowerShell的新手.我在网上看了这个答案,但无济于事.也许我只是错误地表达了这个问题.
我经常发现自己处于一种情况,我必须在cmdlet之后将字符串与变量连接起来.例如,
New-Item $archive_path + "logfile.txt" -type file
Run Code Online (Sandbox Code Playgroud)
如果我尝试运行它,PowerShell会抛出以下错误:
New-Item:找不到接受参数'+'的位置参数.
我没有正确连接字符串吗?我不想在每次执行此操作的cmdlet之前声明另一个变量(例如$logfile = $archive_path + "logfile.txt",然后执行New-Item $logfile -type file).此外,我不会总是连接文件路径.
Mat*_*sen 17
您得到该错误是因为powershell解析器看到$archive_path,+并且"logfile.txt"作为三个单独的参数参数,而不是一个字符串.
将字符串连接括在parantheses中()以更改评估顺序:
New-Item ($archive_path + "logfile.txt") -Type file
Run Code Online (Sandbox Code Playgroud)
或将变量包含在子表达式中:
New-Item "$($archive_path)logfile.txt" -Type file
Run Code Online (Sandbox Code Playgroud)
您可以阅读有关参数模式解析的内容 Get-Help about_Parsing
| 归档时间: |
|
| 查看次数: |
35232 次 |
| 最近记录: |