简体   繁体   English

使用if语句的powershell foreach对象

[英]powershell foreach-object with if statements

I'm trying to run this script that basically reads/interprets text in from a textfile and stores the "key" and "value" into a hashtable. 我正在尝试运行此脚本,该脚本基本上从文本文件读取/解释文本,并将“键”和“值”存储到哈希表中。 Now I want to change a specific key and value as I am reading the text from the text file. 现在,当我从文本文件中读取文本时,我想更改特定的键和值。

The following is the text in my file: 以下是我文件中的文本:

line1 = apple  
line2 = firetruck  
line3 = cricket  
line4 = gorilla  
line5 = elephant  
line6 = banana  
line7 = jumper  
line8 = hat  
line9 = deer  
line10 = igloo  

Here is my code: 这是我的代码:

param
(
    [Parameter(Mandatory=$true,  HelpMessage="Please specify the text filename.")]$fileName
)


$hashTable = @{}
$textFile = Get-Content $filename | ForEach-Object `
{
    $equalindex = $_.IndexOf("=")
    $key = $_.Substring(0,$equalindex)

    if($key -like "line2")
    {
        $key = "line2 has been changed"
    }

    $remainingLen = (($_.Length - 1) -$equalindex)
    $value = $_.Substring($equalindex +1, $remainingLen)

    if($value -like "value2")
    {
        $value = "value2 has been changed"
    }

    $hashtable.Add($key,$value)
} 

$hashtable.Set_Item("line5","line5 value has been changed")
$hashtable.GetEnumerator() | Sort-Object name

When I run my code it outputs the hashtable but does not change my "line2" key or my "value2" value. 当我运行代码时,它会输出哈希表,但不会更改我的“ line2”键或“ value2”值。 Is this the correct/most logical way to do what I'm trying to accomplish? 这是我要完成的正确/最合乎逻辑的方法吗?

You need to remove leading and (or) trailing spaces. 您需要删除前导和(或)尾随空格。 Try this: 尝试这个:

$ht=@{}

Get-Content $filename | foreach{

    $key,$value = $_.Split('=') | foreach {$_.Trim()}

    if($key -like "line2")
    {
       $key = "line2 has been changed"
    }

    if($value -like "value2")
    {
        $value = "value2 has been changed"
    }

    $ht.Add($key,$value)
}

$ht.GetEnumerator() | Sort-Object name


Name                           Value                                                                                                                                     
----                           -----                                                                                                                                     
line1                          apple                                                                                                                                     
line10                         igloo                                                                                                                                     
line2 has been changed         firetruck                                                                                                                                 
line3                          cricket                                                                                                                                   
line4                          gorilla                                                                                                                                   
line5                          elephant                                                                                                                                  
line6                          banana                                                                                                                                    
line7                          jumper                                                                                                                                    
line8                          hat                                                                                                                                       
line9                          deer   

I think the trailing space throwing you off. 我认为后面的空间会让您失望。

Change this: 更改此:

$key = $_.Substring(0,$equalindex)

to this: 对此:

$key = $_.Substring(0,$equalindex).Trim(' ')

or maybe the index is off as this seems to work too: 或者也许索引已关闭,因为这似乎也可以工作:

$key = $_.Substring(0,$equalindex - 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM