skip to Main Content

I am writing a PowerShell script to add a meta tag to the head section of a HTML file. The tag must be as follows: <meta name=”robots” content=”noindex, nofollow”>.

The general snippet below works for all attributes, except for attributes called name:

$newMeta = $doc.createElement("meta")
$newMeta.attributename = "attribute content"

So this works:

$newMeta = $doc.createElement("meta")
$newMeta.content = "noindex, nofollow"

But this does not:

$newMeta = $doc.createElement("meta")
$newMeta.name = "robots"

The name attribute is just ignored in the output.

How can I add an attribute called name to the output?

2

Answers


  1. Chosen as BEST ANSWER

    I see no more takers, so I will try to answer the question myself. From what I understand, it is not possible to set an attribute called "name" on an HTML element, either with SetAttribute() or with a property (object.attributename).

    I managed to obtain the same effect with a workaround: I first create an attribute with a placeholder name, then replace the placeholder name with the string "name".

    I am aware that is not an elegant solution, and I am somewhat ashamed of it, but it works. Maybe the following snippet can be of help to others.

    $file = "somefile.html"
    $content = Get-Content $file -Raw
    
    $doc = New-Object -ComObject "HTMLFile"
    $doc.IHTMLDocument2_write($content)
    
    $meta = $doc.getElementsByName("robots") | Select-Object -First 1
    
    if ($meta) {
        $meta.outerHTML = ""
        $doc.documentElement.outerHTML | Set-Content $file
    }
    
    $head = $doc.getElementsByTagName("head") | Select-Object -First 1
    $newMeta = $doc.createElement("meta")
    $newMeta.content = "noindex, nofollow"
    #SetAttribute() cannot create an attribute named "name". 
    #So instead I will give it a placeholder name and replace it later on.
    $newMeta.SetAttribute('__nameee__', 'robots')
    $newMeta.outerHTML | Add-Content $logFile
    $head.appendChild($newMeta)
    $doc.documentElement.outerHTML | Set-Content $file
    
    $htmlContent = Get-Content $file -Raw
    
    $pattern = "__nameee__"
    
    if ($htmlContent -match $pattern) {
    # Replace the placeholder attribute name with "name".
    $htmlContent = $htmlContent -replace $pattern, "name"
    
    # Overwrite the original HTML file with the updated content
        Set-Content $file -Value $htmlContent }
    else {
        Write-Host "The HTML file $htmlFilePath does not contain a robots metatag."
    }
    

  2. Assuming that $doc is an XmlDocument object, this is a case of PowerShell’s XML adapter having a conflict between the Name property on each xml node object, and the name attribute you want to add to the document itself.

    Use the SetAttribute() method to unambiguously set an attribute value:

    $newMeta.SetAttribute('name', 'robots')
    

    PS ~> $doc = [xml]::new()
    PS ~> $newMeta = $doc.CreateElement('meta')
    PS ~> $newMeta.OuterXml
    <meta />
    PS ~> $newMeta.SetAttribute('name', 'robots')
    PS ~> $newMeta.OuterXml
    <meta name="robots" />
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search