Read value from text file and store in the hash and retreive the value using key() method

rated by 0 users
This post has 11 Replies | 2 Followers

Top 150 Contributor
Posts 21
sensoft1988 Posted: 06-21-2011 4:05 AM

 

server = 192.168.10.174 ;username = root ;password =  pass in hash.txt

get the  key and value from  hash text and create a hash  and get the value for the key

eg: $hash.key("server")

 

 

Top 50 Contributor
Posts 51

$hash = @{}
$content = get-content "D:\hash.txt"


$lines = $content.split(';')
foreach($line in $lines)
{
    $temp = $line.split('=')   
    $hash.add($temp[0].trim().toString(),($temp[1]).trim().toString())
}

you can user $hash, like this
$hash.keys
 username
 server
 password

$hash.server
192.168.10.174
 

Top 50 Contributor
Posts 51

This way is better than before

$hash = @{}
$content = get-content "D:\hash.txt"
$hash = $content.replace(";","`n") | ConvertFrom-StringData

Top 150 Contributor
Posts 21

 

thanks for your useful information. 

After executing the $lines =$content.split(';')

It shows the following error

Method invocation failed because [System.Object[]] doesn't contain a method named 'split'.

 

+ CategoryInfo          : InvalidOperation: (split:String) [], RuntimeException

+ FullyQualifiedErrorId : MethodNotFound

Sorry :

 I want to execute  this command in powerCLI

 

 

 

Top 150 Contributor
Posts 21

thanks for your useful information. 

After executing the $hash = $content.replace(";","`n") | ConvertFrom-StringData

It shows the following error

Method invocation failed because [System.Object[]] doesn't contain a method named 'replace'.

At line:1 char:25

+ $hash = $content.replace <<<< (";","`n") | ConvertFrom-StringData

    + CategoryInfo          : InvalidOperation: (replace:String) [], RuntimeException

    + FullyQualifiedErrorId : MethodNotFound

 

 

Top 50 Contributor
Posts 51

check your "hash.txt" location.

$content = get-content "your hash.txt asolute path"

in my case

"D:\hash.txt"

 

then

excute $content , you can see contents of text.

and you can recognize type of variable.

$content.gettype()

Top 50 Contributor
Posts 51

you can see like this...

PS C:\Users\vstarmanv> $content
server = 192.168.10.174 ;username = root ;password =  msys#123

___________________________________________________________________________________________________________________________________________________
PS C:\Users\vstarmanv> $content.gettype()

IsPublic IsSerial Name                                     BaseType                                                                                  
-------- -------- ----                                     --------                                                                                  
True     True     String                                   System.Object  

Top 150 Contributor
Posts 21

when i execute th $content.getType()

 

IsPublic IsSerial Name                                     BaseType

-------- -------- ----                                     --------

True     True     Object[]                                 System.Array

can u help me ...

Top 50 Contributor
Posts 51

Maybe, your text file split by '\n'

like this

server = 192.168.10.174 ;
username = root ;
password =  msys#123


Then use this

$hash = get-content "D:\hash.txt" | ConvertFrom-StringData

Top 150 Contributor
Posts 21

thanks  vstarmanv.,

My text file

D:\hash.txt

 

server = 192.168.10.174 

username = root

password = 123

 

server1 =  192.168.1.173

username1  = roots 

password1 = 123

After executed this step

$hash = get-content "D:\hash.txt" | ConvertFrom-StringData

 

$hash.gettype()

 

IsPublic IsSerial Name                                     BaseType

-------- -------- ----                                     --------

True     True     Object[]                                 System.Array

 

it is not change from array to string 

$hash.split('`n ')
It shows
Method invocation failed because [System.Object[]] doesn't contain a method named 'Split'.
At line:1 char:12

 

 

Top 10 Contributor
Posts 624
Microsoft MVP
Top Contributor

try this:


$hash = @{}
Get-Content c:\hash.txt | Where-Object { $_ } |
ForEach-Object {
  $info = $_.Split('=')
  if ($info.Count -eq 2) {
    $hash.$($info[0].Trim()) = $info[1].Trim()
  }
  }

Top 150 Contributor
Posts 21

thanks  Tobias ..,

It works fine ,now i read the value using key()

Page 1 of 1 (12 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.