PowerShell新项目位置参数异常

PowerShell新项目位置参数异常,powershell,Powershell,鉴于PowerShell的Microsoft文档,我看不出为什么以下代码会因给定错误而失败。同样,当脚本太长时,PowerShell可能会失败。所有路径都是双引号字符串 ##### ALGORITHM Take in keystore path, make a backup in an adjacent directory $ksPath = $java_store_path.Substring(0, $java_store_path.LastIndexOf('\') + 1) $backupP

鉴于PowerShell的Microsoft文档,我看不出为什么以下代码会因给定错误而失败。同样,当脚本太长时,PowerShell可能会失败。所有路径都是双引号字符串

##### ALGORITHM Take in keystore path, make a backup in an adjacent directory
$ksPath = $java_store_path.Substring(0, $java_store_path.LastIndexOf('\') + 1)
$backupPath = $ksPath + "backups"
New-Item $backupPath PowerShell -type directory -force
新项:找不到接受参数“PowerShell”的位置参数。


如果这是正确的,我的也应该是正确的。我正在服务器2012 R2上运行。

该页面上的示例完全错误。似乎他们是想引用路径
C:\Scripts\WindowsPowerShell
,或者他们忘记引用包含空格的目录

因此,它应该是其中之一:

New-Item c:\scripts\WindowsPowerShell -type directory
New-Item 'c:\scripts\Windows PowerShell' -type directory
New-Item "c:\scripts\Windows PowerShell" -type directory
问问你自己,
PowerShell
一个人指的是什么?它对应于什么参数


编辑:正如评论者所指出的,该示例应该显示
名称集
参数,其中指定了单独的
-Path
-Name
,据称
PowerShell
应该是
-Name
参数的值。这看起来确实正确。该示例不起作用(您的示例也不起作用)的原因是无法按位置指定
-Name
参数,您可以在下面链接的MSDN文章和内置帮助中看到这一点:

在这种情况下,他们的例子应该是这样的:

New-Item c:\scripts\Windows -Name PowerShell -type directory
New-Item -Path c:\scripts\Windows -Name PowerShell -type directory
因此,重申命名参数在这里是可行的,并且可以避免混淆


通常,您不应该在脚本中使用位置参数,除非它们非常清楚(即使如此,我还是建议避免)

使用命名参数会使这更容易理解。制表符补全有助于填充参数名称和完成路径(通常也有适当的引号)

我认为您应该将您的更改为:

New-Item -Path $backupPath -Type Directory -Force
看看technet的那篇文章,真的不太好。更好,这也是运行
Get Help New Item
时应该看到的信息


附带问题:

同样,当脚本太长时,PowerShell可能会失败


什么?

我不是在开玩笑。我们有一个5000行的PS monolith,用于服务器设置、配置和维护任务调度。它坏了,因为它太大了。参数只是通过函数落入彼此的位置,没有明显的原因,我们使用LLVM的静态分析工具来验证所编写的数据流。在5400行之后,它自己就崩溃了。我们正在将其分解为多个Scriptlet。同意。该示例特别指出,它将在
C:\Scripts
文件夹中创建一个名为
Windows PowerShell
的新文件夹。@而且,进一步看,我同意您和(现在已删除)其他注释,尽管我认为这只会进一步说明位置参数可能会令人困惑。我编辑以澄清。谢谢
New-Item c:\scripts\Windows -Name PowerShell -type directory
New-Item -Path c:\scripts\Windows -Name PowerShell -type directory
New-Item -Path $backupPath -Type Directory -Force