如何在PowerShell中编辑后存储文件?

问题描述 投票:-4回答:1

我有文本文件。我对文件进行了编辑,完成编辑后我想存储数据并通过编辑覆盖文件。

My file
***********************************
;ML=DPC
;CRM=AE
;********************************** 

Function CRM
{
    Param(
    [parameter(mandatory=$true)]$FilePath,
    [parameter(mandatory=$true)] $CRM
    #parameter(mandatory=$true)] $Variable_Name
    )

$filteredContent = (Get-Content $FilePath) -replace '^;' -replace '\\','\\' |
    Where-Object{-not $_.startswith('*')} 
$information = [pscustomobject]($filteredContent -join "`r`n" | ConvertFrom-StringData)


$CRM_1 = $information.CRM
$CRM_Edit = "$CRM"
$CRM_Edit | Out-File -FilePath $FilePath -Force
}
#---------------------------------------------------------------------------------------------------------------------#

在将CRM的值编辑为AFF后,我的期望是,我将有一个这样的文本文件。

***********************************
;ML=DPC
;CRM=AFF
;**********************************
powershell
1个回答
1
投票

我重写了你的功能,因为虽然ConvertFrom-Stringdata很适合阅读,但是将数据以你想要的格式写回文件似乎很麻烦。你还在你的函数中硬编码了“CRM”字符串,这使得ConvertFrom-Stringdata不那么强大。以下是使用“CRM”硬编码字符串的示例。

Function CRM
{
    Param(
    [parameter(mandatory=$true)] $FilePath,
    [parameter(mandatory=$true)] $CRM
    )

$FileContents = Get-Content $FilePath
$FileContents -replace "^(?<1>;CRM=).*","`${1}$CRM" | Set-Content $FilePath

}

执行上面的功能:

CRM $filepath "AFF"
Get-Content $filepath
***********************************
;ML=DPC
;CRM=AFF
;**********************************

将更新文件内容格式中任何键值的替代函数如下:

Function Update-Value
{
    Param(
    [parameter(mandatory=$true)] $FilePath,
    [parameter(mandatory=$true)] $Key,
    [parameter(mandatory=$true)] $Value
    )

$FileContents = Get-Content $FilePath
$FileContents -replace "^(?<1>;$Key=).*","`${1}$Value" | Set-Content $FilePath

}

运行上面函数的输出:

Get-Content $filepath
***********************************
;ML=DPC
;CRM=AE
;**********************************

Update-Value $filepath "CRM" "AFF"
Update-Value $filepath "ML" "NewML"

Get-Content $filepath
***********************************
;ML=NewML
;CRM=AFF
;**********************************

如果我要增强你的功能,保持使用ConvertFrom-Stringdata,尽可能少地改变,我会做如下的事情:

Function CRM
{
    Param(
    [parameter(mandatory=$true)]$FilePath,
    [parameter(mandatory=$true)] $CRM
    #parameter(mandatory=$true)] $Variable_Name
    )

$FileContents = Get-Content $FilePath
$filteredContent = $FileContents -replace '^;' -replace '\\','\\' |
    Where-Object{-not $_.startswith('*')} 
$information = [pscustomobject]($filteredContent -join "`r`n" | ConvertFrom-StringData)

$information.CRM = $CRM
$informationFormatted = ForEach ($p in $information.psobject.properties) {
    ";{0}={1}" -f $p.name,$p.value
    }
$FileContents[0],$informationFormatted,$FileContents[-1] | Set-Content $filepath
}

说明:

-replace运营商正在完成所有工作。要替换的文本^(;$Key=).*使用正则表达式匹配机制。 ^表示一行的开头。括号创建一个捕获组,可以在替换字符串中作为$1访问。 ;=是文字比赛。 $Key被函数调用中的$Key参数的值替换。 .*是所有字符,直到当前行的结尾。 $1$Value是替代正则表达式。 $1()在匹配字符串中注明的捕获组1中捕获的内容。 $Value是调用函数时$Value参数的值。 $1被反向转义,因此PowerShell不会尝试将其扩展为字符串变量。它是一个正则表达式特定的语言元素,表示替换模式而不是典型变量。 $Key$Value被评估为变量的原因是因为它们存在于双引号(")之间并且没有被转义。

© www.soinside.com 2019 - 2024. All rights reserved.