将CDATA字符串文字从JSON数组导入PowerShell

时间:2018-03-27 21:21:30

标签: json xml powershell cdata

所以我有一个大型规则数据库的XML值提取器。 我将值存储在Json文件中,其中包含每个文件的部分,然后是与其名称相关联的值 但是有些值是CDATA代码段 我可以把CDATA作为一个字符串文字来存储在Json中,但我似乎找不到一种方法来获取power shell让我把它放回到XML中。

  

无法设置“值”,因为只有字符串可用作设置XmlNode属性的值   在C:\ Users \ vagrant \ Desktop \ RuleSetter.ps1:24 char:21
  + $ rule.value = $ ruleFileSet。 ($ singleRuleXmlFile.directory.nam ...   +
      + CategoryInfo:NotSpecified:(:) [],SetValueException
      + FullyQualifiedErrorId:XmlNodeSetShouldBeAString`

Getter看起来像这样

$directories = Get-ChildItem -dir -path C:\inetpub\wwwroot\
foreach($instName in $directories){ 
if($instName -notlike 'client' -and $instName -notlike 'bin'-and $instName -notlike 'aspnet_client'-and $instName -notlike 'AccessLibertyLogs'){
    $obj =  [hashtable]@{}
    $head = [hashtable]@{Institution = $instName}
    $obj.Add('head',$head)
    $files = Get-ChildItem  C:\inetpub\wwwroot\$instName\filepath -Include Rules.XML -Recurse
    $files | 
    ForEach-Object {
        [xml]$temp = Get-Content -path $_.FullName
        [string]$title = $_.Directory.name
        [hashtable]$output = @{}
        foreach($rule in $temp.RuleCollection.Rules.Rule){
            $t = ""
            if($rule.value -isnot [string]){
                $t = $rule.value.innerXml
            }else{
                $t = $rule.value
            }
            $output.Add($rule.name,$t)
        }
        $obj.Add($title,$output)
    }
    $transfer = New-Object -TypeName PSObject -Property $obj
    $location = "C:\Users\vagrant\desktop\json\" + $instName + ".Json"
     $transfer | ConvertTo-Json -Compress | Out-file -FilePath $location
}  
}

Setter就像这样

Param(
[string]$location,
[string]$ruleConfigFileLocation)
$allInstConfigFiles = Get-ChildItem -Path C:\Users\vagrant\Desktop\json\

foreach($singleInstFile in $allInstConfigFiles) {

$instaName = $singleInstFile.BaseName

$allRuleXmlFileList = Get-ChildItem C:\inetpub\wwwroot\$instaName\Liberty     \Applications\Origination\Configuration -Include Rules.XML -Recurse

ForEach($singleRuleXmlFile in $allRuleXmlFileList) {

[xml]$singleRuleXmlFileContents = Get-Content -path $singleRuleXmlFile.FullName

    $singleInstFileContent = Get-Content $singleInstFile.FullName | ConvertFrom-Json

    foreach($ruleFileSet in $singleInstFileContent){

        foreach($rule in $singleRuleXmlFileContents.RuleCollection.Rules.Rule){
            #write-host $ruleFileSet.($singleRuleXmlFile.directory.name).($rule.name)
            if($ruleFileSet.($singleRuleXmlFile.directory.name).($rule.name) -as [xml])
            {
                $rule.value = $ruleFileSet.($singleRuleXmlFile.directory.name).($rule.name).OuterXml
                write-host 'skipped'
            }else{
                write-host 'not skipped'
                $rule.value = $ruleFileSet.($singleRuleXmlFile.directory.name).($rule.name)
            }            
      }            
    }
  }
}

0 个答案:

没有答案