मैं एक file.properties है और उसकी सामग्री है कि मान लें करते हैं:पढ़ें एक गुण powershell में दाखिल
app.name=Test App
app.version=1.2
...
मैं app.name का मूल्य कैसे मिल सकता है?
मैं एक file.properties है और उसकी सामग्री है कि मान लें करते हैं:पढ़ें एक गुण powershell में दाखिल
app.name=Test App
app.version=1.2
...
मैं app.name का मूल्य कैसे मिल सकता है?
आप ConvertFrom- उपयोग कर सकते हैं StringData एक हैश तालिका की कुंजी = मूल्य जोड़े कन्वर्ट करने के लिए:
$filedata = @'
app.name=Test App
app.version=1.2
'@
$filedata | set-content appdata.txt
$AppProps = convertfrom-stringdata (get-content ./appdata.txt -raw)
$AppProps
Name Value
---- -----
app.version 1.2
app.name Test App
$AppProps.'app.version'
1.2
धन्यवाद !!! इस प्रकार मैं अपने कोड लागू किया है: $ AppProps = convertfrom-stringdata (मिल-सामग्री ./app.properties रॉ) $ AppProps.'client.version ' – jos11
'-Raw' झंडा PowerShell 3 में जोड़ दिया गया है यदि आप PowerShell 2 पर इसका उपयोग करना चाहते हैं तो 'गेट-कंटेंट $ फ़ाइल' का उपयोग करें इसके बजाय आउट-स्ट्रिंग '। – Koraktor
अगर वहाँ ऐसा करने का कुछ Powershell एकीकृत तरीका है मैं नहीं जानता, लेकिन मैं regex के साथ यह कर सकते हैं:
$target = "app.name=Test App
app.version=1.2
..."
$property = "app.name"
$pattern = "(?-s)(?<=$($property)=).+"
$value = $target | sls $pattern | %{$_.Matches} | %{$_.Value}
Write-Host $value
प्रिंट चाहिए "परीक्षण ऐप्लिकेशन"
आप आपको मिल-सामग्री के लिए "रॉ" दलील मौजूद नहीं किया जा सकता है powershell v2.0 के साथ चला रहे हैं। इस मामले में आप निम्नलिखित का उपयोग कर सकते हैं।
सी की सामग्री: \ अस्थायी \ data.txt:
वातावरण = क्यू GRZ
TARGET_SITE = FSHHPU
कोड:
$file_content = Get-Content "C:\temp\Data.txt"
$file_content = $file_content -join [Environment]::NewLine
$configuration = ConvertFrom-StringData($file_content)
$environment = $configuration.'environment'
$target_site = $configuration.'target_site'
मैं करना चाहता था यदि आपको भागने की आवश्यकता है तो समाधान जोड़ें (उदाहरण के लिए यदि आपके पास बैकस्लाश के साथ पथ हैं):
$file_content = Get-Content "./app.properties" -raw
$file_content = [Regex]::Escape($file_content)
$file_content = $file_content -replace "(\\r)?\\n", [Environment]::NewLine
$configuration = ConvertFrom-StringData($file_content)
$configuration.'app.name'
रॉ के बिना
:
$file_content = Get-Content "./app.properties"
$file_content = [Regex]::Escape($file_content -join "`n")
$file_content = $file_content -replace "\\n", [Environment]::NewLine
$configuration = ConvertFrom-StringData($file_content)
$configuration.'app.name'
या एक पंक्ति फैशन में:
(ConvertFrom-StringData([Regex]::Escape((Get-Content "./app.properties" -raw)) -replace "(\\r)?\\n", [Environment]::NewLine)).'app.name'
आप रेगुलर एक्सप्रेशन या 'इस्तेमाल कर सकते हैं -split'' = पर लाइनों '। –