不再需要使用out file only立即导入文件,只需使用PowerShell转换基类型即可

不再需要使用out file only立即导入文件,只需使用PowerShell转换基类型即可,powershell,Powershell,我试图将下面的文件转换为一个不包含注释“#”、不包含空行、不需要的空格以及每行仅包含一个条目的文件。我不确定如何在不需要输出文件然后重新导入的情况下运行以下代码。应该有代码不需要那个步骤,但我找不到。我写剧本的方式对我来说也不太合适,尽管它很有效。好像有一种更优雅的方式来做我正在尝试的事情,但我就是看不到 文件更改前:TNSNames.ora #Created 9_27_16 #Updated 8_30_19 AAAA.world=(DESCRIPTION =(ADDRESS_LIST =

我试图将下面的文件转换为一个不包含注释“#”、不包含空行、不需要的空格以及每行仅包含一个条目的文件。我不确定如何在不需要输出文件然后重新导入的情况下运行以下代码。应该有代码不需要那个步骤,但我找不到。我写剧本的方式对我来说也不太合适,尽管它很有效。好像有一种更优雅的方式来做我正在尝试的事情,但我就是看不到

文件更改前:TNSNames.ora

#Created 9_27_16
#Updated 8_30_19

AAAA.world=(DESCRIPTION =(ADDRESS_LIST = 
        (ADDRESS = 
          (COMMUNITY = tcp.world)
          (PROTOCOL = TCP)
          (Host = www.url1111.com)
          (Port = 1111)
        )
    )
    (CONNECT_DATA = (SID = SID1111)
    )
  )
  
#Created 9_27_16

BBBB.world=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=url2222.COM)(Port=2222))(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=url22222.COM)(Port=22222)))(CONNECT_DATA=(SID=SID2222)))
CCCC.world=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=url3333.COM)(Port=3333))(CONNECT_DATA=(SID=SID3333)))
DDDD.url       =(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=URL4444   )(Port=4444))(ADDRESS=(COMMUNITY=TCP.world)(PROTOCOL=TCP)(Host=URL44444   )(Port=44444)))(CONNECT_DATA=(SID=SID4444   )(GLOBAL_NAME=ASDF.URL)))

#Created 9_27_16
#Updated 8_30_19
文件更改后:

AAAA.world=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=www.url1111.com)(Port=1111)))(CONNECT_DATA=(SID=SID1111)))
BBBB.world=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=url2222.COM)(Port=2222))(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=url22222.COM)(Port=22222)))(CONNECT_DATA=(SID=SID2222)))
CCCC.world=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=url3333.COM)(Port=3333))(CONNECT_DATA=(SID=SID3333)))
DDDD.url=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=URL4444)(Port=4444))(ADDRESS=(COMMUNITY=TCP.world)(PROTOCOL=TCP)(Host=URL44444)(Port=44444)))(CONNECT_DATA=(SID=SID4444)(GLOBAL_NAME=ASDF.URL)))
代码:


因此,问题的关键在于您将$data声明为[string],这很好,因为您的一些替换操作作为单个字符串可能工作得更好。只是$Results最后也是一个字符串,所以当您尝试在底部附近索引$Results时,这些操作都失败了。但是,您可以使用-split运算符轻松地将$Results变量转换为字符串数组,这样就不需要将字符串保存到磁盘并重新导入即可完成相同的操作。见下面的评论

# Get the file
[System.IO.FileInfo] $File = 'C:\temp\TNSNames.ora'
[string] $data = (Get-Content $File.FullName | Where-Object { !$_.StartsWith('#') }).ToUpper()

# Convert the data.  This part is where any (CONNECT_DATA entry ends up on it's own line.
$Results = $data.Replace(' ', '').Replace("`t", '').Replace(')))', ")))`n")

# You do not need to do this next section.  Essentially this is just saving your multiline string 
# to a file and then using Get-Content to read it back in as a string array

    # Convert $Results from BaseType of System.Object to System.Array
    # $Path = 'c:\temp\StringResults.txt'
    # $Results | Out-File -FilePath $Path
    # $Results = Get-Content $Path

# Instead split your $Results string into multiple lines using -split
# this will do the same thing as above without writing to file
$Results = $Results -split "\r?\n"

# Find all lines that start with '(CONNECT_DATA'
for ($i = 0; $i -lt $Results.Length - 1; $i++) {
    if ($Results[$i + 1].StartsWith('(CONNECT_DATA')) {
        # Add the '(CONNECT_DATA' line to the previous line
        $Results[$i] = $Results[$i] + $Results[$i + 1]
        # Blank out the '(CONNECT_DATA' line
        $Results[$i + 1] = ''
    }
}

# Remove all blank lines
$FinalForm = $null
foreach ($Line in $Results) {
    if ($Line -ne '') {
        $FinalForm += "$Line`n"
    }
}
$FinalForm
另外,为了好玩,试试这个

((Get-Content 'C:\temp\tnsnames.ora' |
        Where-Object {!$_.StartsWith('#') -and ![string]::IsNullOrWhiteSpace($_)}) -join '' -replace '\s' -replace '\)\s?\)\s?\)', ")))`n" -replace '\r?\n\(Connect_data','(connect_data').ToUpper()
((Get-Content 'C:\temp\tnsnames.ora' |
        Where-Object {!$_.StartsWith('#') -and ![string]::IsNullOrWhiteSpace($_)}) -join '' -replace '\s' -replace '\)\s?\)\s?\)', ")))`n" -replace '\r?\n\(Connect_data','(connect_data').ToUpper()