2011-03-22 35 views
3

我有以下的.ini文件(名为metrics.ini)包含一个单独的记录,但它可能已添加到它在未来更多的记录:如何解析的.ini文件到PowerShell的变量(S)

$DatabaseConnection_STR=MYSERVER\MYINSTANCE 

我需要将此文件解析为PowerShell变量。我可以使用以下语法解析字符串,但是我无法创建新的$ DatabaseConnection_STR变量(基于从.ini文件解析的内容)。我不想在脚本中对$ DatabaseConnection_STR进行硬编码 - 我宁愿让脚本找出它,以便将来可以处理其他变量。

# This code assumes that no blank lines are in the file--a blank line will cause an early termination of the read loop 

    ######################################## 
    # 
    # Confirm that the file exists on disk 
    # 
    ######################################## 

    $IniFile_NME="C:\temp\metrics.ini" 

    dir $IniFile_NME 

    ######################################## 
    # 
    # Parse the file 
    # 
    ######################################## 

    $InputFile = [System.IO.File]::OpenText("$IniFile_NME") 

    while($InputRecord = $InputFile.ReadLine()) 
     { 
      # Display the current record 

      write-host "`$InputRecord=$InputRecord" 
      write-host "" 

      # Determine the position of the equal sign (=) 

      $Pos = $InputRecord.IndexOf('=') 
      write-host "`$Pos=$Pos" 

      # Determine the length of the record 

      $Len = $InputRecord.Length 
      write-host "`$Len=$Len" 

      # Parse the record 

      $Variable_NME = $InputRecord.Substring(0, $Pos) 
      $VariableValue_STR = $InputRecord.Substring($Pos + 1, $Len -$Pos -1) 

      write-host "`$Variable_NME=$Variable_NME" 
      write-host "`$VariableValue_STR=$VariableValue_STR" 

      # Create a new variable based on the parsed information--**the next line fails** 

      `$Variable_NME=$VariableValue_STR 
     } 
    $InputFile.Close() 

任何想法?

回答

0

这是有效的。事实证明,新变量命令不使用美元符号($)及其“-name”参数;所以,我不得不解析出来。见下文。

# This code assumes that no blank lines are in the file--a blank line will cause an early termination of the read loop 

######################################## 
# 
# Confirm that the file exists on disk 
# 
######################################## 

$IniFile_NME="C:\temp\metrics.ini" 

dir $IniFile_NME 

######################################## 
# 
# Parse the file 
# 
######################################## 

$InputFile = [System.IO.File]::OpenText("$IniFile_NME") 

while($InputRecord = $InputFile.ReadLine()) 
    { 
     # Display the current record 

     write-host "`$InputRecord=$InputRecord" 
     write-host "" 

     # Determine the position of the equal sign (=) 

     $Pos = $InputRecord.IndexOf('=') 
     write-host "`$Pos=$Pos" 

     # Determine the length of the record 

     $Len = $InputRecord.Length 
     write-host "`$Len=$Len" 

     # Parse the record 

     $Variable_NME = $InputRecord.Substring(1, $Pos -1) 
     $VariableValue_STR = $InputRecord.Substring($Pos + 1, $Len -$Pos -1) 

     write-host "`$Variable_NME=$Variable_NME" 
     write-host "`$VariableValue_STR=$VariableValue_STR" 

     # Create a new variable based on the parsed information 

     new-variable -name $Variable_NME -value $VariableValue_STR 
     get-variable -name $Variable_NME 
    } 
$InputFile.Close() 
6

使用split命令可能更简单,更简洁。您也可以存储在哈希表中配置的值:

$config = @{} 

Get-Content $IniFile_NME | foreach { 
    $line = $_.split("=") 
    $config.($line[0]) = $line[1] 
} 

你仍然可以使用创建变量如果不想要一个哈希表,但使用PowerShell的阅读,循环同样的方法和拆分将使它更轻松。