我试图强制脚本接受用户输入,但它打破了第二个输入

时间:2013-04-02 13:53:53

标签: string powershell input directory

我是Power shell的新手,我似乎无法绕过这一个。

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

我需要更改才能使其正常运行?我厌倦了使用[参数(Mandatory = $ true,name ='你希望你的文件夹被命名为:')]但我似乎无法让它工作。

非常感谢任何帮助。

$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)

1 个答案:

答案 0 :(得分: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。