PowerShell新项目位置参数奇数

pat*_*p93 4 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)
$backupPath = $ksPath + "backups"
New-Item $backupPath PowerShell -type directory -force
Run Code Online (Sandbox Code Playgroud)

New-Item:找不到接受参数'PowerShell'的位置参数.

https://technet.microsoft.com/en-us/library/ee176914.aspx

New-Item c:\scripts\Windows PowerShell -type directory
Run Code Online (Sandbox Code Playgroud)

如果那是有效的,我的也应该是.我在Server 2012 R2上运行.

bri*_*ist 6

该页面上的示例完全错误.它们似乎意味着引用路径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
Run Code Online (Sandbox Code Playgroud)

问问自己,PowerShell单独指的是什么?它对应的参数是什么?


编辑:正如评论者指出的那样,该示例应该显示nameSet参数,其中一个单独的-Path-Name被指定,并且据称PowerShell应该是-Name参数的值.这看起来是正确的.该示例不起作用的原因(以及您的原因)是因为-Name无法在位置指定参数,您可以在我链接到下面的MSDN文章和内置帮助中看到:

Type: String
Parameter Sets: nameSet
Aliases: 

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
Run Code Online (Sandbox Code Playgroud)

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

New-Item c:\scripts\Windows -Name PowerShell -type directory
New-Item -Path c:\scripts\Windows -Name PowerShell -type directory
Run Code Online (Sandbox Code Playgroud)

如此重申,命名参数在这里起作用,并且可以避免混淆.


一般来说,你不应该在脚本中使用位置参数,除非它们非常清楚(即便如此,我建议避免使用).

使用命名参数可以更容易理解.tab-completion有助于填写参数名称和完成路径(通常也有适当的引用).

我认为你应该改变你的:

New-Item -Path $backupPath -Type Directory -Force
Run Code Online (Sandbox Code Playgroud)

看看那篇关于technet的文章,它真的不太好.有关New-ItemMSDN文章更好,这也是您在运行时应该看到的信息Get-Help New-Item.


问题:

然后,当脚本太长时,PowerShell可能会失败.

什么?