0

我是 Power shell 的新手,我似乎无法理解这个。

当您取出输入并为每个 $number 和 $Name 放入实际值时,该代码起作用,如底部示例所示。我发现与指定值不同,第 7 行 char 13 不能是 -$Name,而是必须是 $Name。这样做之后,它告诉我 ln 15 char 13 “找不到接受参数“-”的位置字符。其中“-”= $Name。

我需要改变什么才能让它发挥作用?我已经厌倦了使用 [parameter(Mandatory=$true,name='What would you like your folder to be named: ')] 但我似乎也无法让它工作。

任何帮助将不胜感激。

$number = read-host "How Many Folders would you like to create: "
$Name = read-host "What would you like your folder to be named: "
$intFolders = $number 
$intPad
$i = 1

New-Variable -$Name strPrefix -Value "$Name" -Option constant

    do {

        if ($i -lt $number)
            {$intPad=0 
                new-item -path c:\mytest -$Name $strPrefix$intPad$i -type directory}
        else
            {new-item -path c:\mytest -$Name $strPrefix$i -type directory}
            $i++}
        until ($i -eq $intFolders+1)

这是我能够开始工作的下面的代码,但我想确保当我离开时,这里的人们仍然可以使用它来满足他们的需求,因为他们不了解编码。

$intFolders = 500
$intPad
$i = 1

New-Variable -Name strPrefix -Value "0001_" -Option constant

    do {

        if ($i -lt 500)
            {$intPad=0 
                new-item -path c:\mytest -name $strPrefix$intPad$i -type directory}
        else
            {new-item -path c:\mytest -name $strPrefix$i -type directory}
            $i++}
        until ($i -eq $intFolders+1)

多亏了布鲁斯,这里是工作的最终代码,它完全可以做到它应该有的:)

[int]$start = read-host "What number would you like your folder to start at"
[int]$number = read-host "How Many Folders would you like to create"
$Name = read-host "What would you like your folder to be named"
$intFolders = $number 
$intPad
$i = $start

New-Variable -Name strPrefix -Value "$Name" -Option constant

    do {

        if ($i -lt 10)
            {$intPad="" 
                new-item -path c:\mytest -Name $strPrefix$intPad$i -type directory}
        else
            {new-item -path c:\mytest -Name $strPrefix$i -type directory}
            $i++}
        until ($i -eq $intFolders+1)
4

1 回答 1

1

改变

New-Variable -$Name strPrefix -Value "$Name" -Option constant
New-Variable -Name strPrefix -Value "$Name" -Option constant
与新项目行中的 -$name 相同。请参阅 Get-Help New-Variable 和 Get-Help New-Item。

于 2013-04-02T15:22:30.373 回答