鉴于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
New-Item:找不到接受参数' PowerShell'
的位置参数https://technet.microsoft.com/en-us/library/ee176914.aspx
New-Item c:\scripts\Windows PowerShell -type directory
如果它有效,我的也应该有效。我在Server 2012 R2上运行。
答案 0 :(得分: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
问问自己,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
在这种情况下,他们的例子应该是这样的:
New-Item c:\scripts\Windows -Name PowerShell -type directory
New-Item -Path c:\scripts\Windows -Name PowerShell -type directory
如此重申,命名参数在这里起作用,并且可以避免混淆。
通常情况下,您不应该在脚本中使用位置参数,除非它们非常清晰(即便如此,我建议避免使用)。
使用命名参数可以更容易理解。 Tab-completion有助于填写参数名称和完成路径(通常也可以正确引用)。
我认为你应该改变你的:
New-Item -Path $backupPath -Type Directory -Force
查看那篇关于technet的文章,它真的不太好。 MSDN article on New-Item更好,这也是您在运行Get-Help New-Item
时应该看到的信息。
附带问题:
然后,当脚本太长时,PowerShell可能会失败。
什么?的