【问题标题】:Confused with Powershell script get-content对 Powershell 脚本获取内容感到困惑
【发布时间】:2022-10-17 14:54:17
【问题描述】:
  1. 要求用户输入名称,在名称数组 person.dat 文件中搜索名称。如果找到该名称,则打印一个表,如果未找到该名称,则打印一条错误消息并询问用户另一个名称。
    persons.dat. 
    George Nelson,56,78000.00
    Mary Nathaniel,65,66300.00
    Rosy Ferreira,32,39000.00
    

    猜测这部分。

    While ($true){
    Write-Host $("1. Search by user name")
    Write-Host  $("2. List all:)
    $input = (Read-Host("Enter an option (0 to quit)"))##user will input value
    #if 1 is entered (Read-Host("Enter user name"))
    #if 2 is entered Print all#
    #if 0 is entered quit.#
    
    try{      ?             }
    
    catch  { 
    ## If input is invalid, restart loop 
    Write-host " User does not exist"    
    continue
    }
    
    0{
    Write-Host $("Thank you. Bye!")
    

    此底部将在表格中打印所有 3 个。

    $data = Get-Content "persons.dat"
    $line = $null;
    [String[]] $name = @();
    [int16[]] $age = @();
    [float[]] $salary = @();
    
    foreach ($line in $data)
    { #Split fields into values
    $line = $line -split (",")
    $name += $line[0];
    $age += $line[1];
    $salary += $line[2];
    }
    Write-Host $("{0,-20} {1,7} {2,11}" -f "Name", "Age", "Salary")
    Write-Host $("{0,-20} {1,7} {2,11}" -f "-----------", "---", "-----------")
    for 
    ($nextItem=0 ; $nextItem -lt $name.length; $nextItem++)
    
    {
    $val1n = $name[$nextItem];
    $val2n = $age[$nextItem]
    $val3n = $salary[$nextItem]
    Write-Host $("{0,-20} {1,7} {2,11:n2}" -f $val1n,
    $val2n, $val3n)
    }
    

【问题讨论】:

  • 那么,你的问题是什么?
  • 仅供参考……$Input 变量是保留变量。 PoSh 会随时欺骗它。所以你真的不能将它用于变量。 [咧嘴笑]
  • 谢谢李。我在课堂上学习脚本并且仍在学习...我将其更改为 $value。亚伯拉罕,我不知道如何从 dat 文件中打印特定的行。该脚本应查找用户输入的姓名,并从 dat 文件中打印姓名、年龄和薪水。非常感谢任何反馈。

标签: arrays powershell loops


【解决方案1】:

这是您可以做到的一种方法,希望内联 cmets 可以帮助您理解逻辑。由于您向我们展示的persons.dat 文件是逗号分隔的,我们可以使用ConvertFrom-Csv 将其转换为对象,通过这样做,您将不需要像您正在使用的那样构建输出到屏幕那些Write-Host 声明。

# Convert the file into an object
$persons = Get-Content persons.dat -Raw | ConvertFrom-Csv -Header "Name", "Age", "Salary"

function ShowMenu {
    # simple function to clear screen and show menu when called
    Clear-Host
    '1. Search by user name'
    '2. List all'
}

:outer while($true) {
    # clear screen and show menu
    ShowMenu
    while($true) {
        # ask user input
        $choice = Read-Host 'Enter an option (0 to quit)'
        # if input is 0, break the outer loop
        if(0 -eq $choice) {
            'Goodbye'
            break outer
        }
        # if input is not 1 or 2
        if($choice -notmatch '^(1|2)$') {
            'Invalid input!'
            $null = $host.UI.RawUI.ReadKey()
            # restart the inner loop
            continue
        }
        # if we are here user input was correct
        break
    }

    $show = switch($choice) {
        1 {
            # if choice was 1, ask user for a user name
            $user = Read-Host "Enter user name"
            # if user name exists in the `$persons` object
            if($match = $persons.where{ $_.Name -eq $user }) {
                # output this to `$show`
                $match
                # and exit this switch
                continue
            }
            # if user name was not found
            "$user was not found in list."
        }
        2 {
            # if input was 2, output `$persons` to `$show`
            $persons
        }
    }
    # show the object to the host
    $show | Out-Host
    # and wait for the user to press any key
    $null = $host.UI.RawUI.ReadKey()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 2010-12-26
    • 1970-01-01
    • 2019-12-22
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多