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")
$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
This way is better than before
$hash = @{}$content = get-content "D:\hash.txt"$hash = $content.replace(";","`n") | ConvertFrom-StringData
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
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
After executing the $hash = $content.replace(";","`n") | ConvertFrom-StringData
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
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()
you can see like this...
PS C:\Users\vstarmanv> $contentserver = 192.168.10.174 ;username = root ;password = msys#123
___________________________________________________________________________________________________________________________________________________PS C:\Users\vstarmanv> $content.gettype()
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object
when i execute th $content.getType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
can u help me ...
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
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.gettype()
it is not change from array to string
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() } }
thanks Tobias ..,
It works fine ,now i read the value using key()