0

I am trying edit a HTML file in powershell, I want to remove the tag head and it contents and add CSS element style and it's contents to the HTML file. I am using Microsoft HTML document class to access HTML file. I am getting error The assignment expression is not valid at this line "$($($HTML.getElementsByTagName('head')).innerHTML) = $CssContent" in powershell

HTML file

<html xmlns:string="xalan://java.lang.String" xmlns:lxslt="http://xml.apache.org/xslt">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Summary</title>
<link rel="sheet" type="text/css" title="Style" href="sheet.css">
</head>

CSS content to be replaced in HTML file

body {
    font:normal 68% verdana,arial,helvetica;
    color:#000000;
}
table tr td, table tr th {
    font-size: 68%;
}

powershell Script

#get contents of file
$Content = Get-Content 'test.html' -raw
$CssContent = Get-Content 'test.css' -raw

# Create HTML file Object
$HTML = New-Object -Com "HTMLFile"
$HTML.IHTMLDocument2_write($Content)

#Assign InnerHTML to CssContent
$($($HTML.getElementsByTagName('head')).innerHTML) = $CssContent
#the error i get is "The assignment expression is not valid at this line"
3
  • Why are you not just using the built-in PowerShell cmdlets for XML? --- Get-Command -Name '*xml*'. Commented Mar 28, 2021 at 20:35
  • Assigning CSS to innerHTML makes no sense. You would have to append a style element as child of head element. Commented Mar 28, 2021 at 21:27
  • i was planning to use powershell replace to add style into the HTML file Commented Mar 30, 2021 at 3:30

1 Answer 1

1

Continuing from my comment.

You can also use RegEx, for this use case, though it is frowned upon relative to using the XML cmdlets.

($HtmlContent = @"
<html xmlns:string="xalan://java.lang.String" xmlns:lxslt="http://xml.apache.org/xslt">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Summary</title>
<link rel="sheet" type="text/css" title="Style" href="sheet.css">
</head>
"@ )
# Results
<#
<html xmlns:string="xalan://java.lang.String" xmlns:lxslt="http://xml.apache.org/xslt">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Summary</title>
<link rel="sheet" type="text/css" title="Style" href="sheet.css">
</head>
#>

($CssContent = @'
body {
    font:normal 68% verdana,arial,helvetica;
    color:#000000;
}
table tr td, table tr th {
    font-size: 68%;
}
'@)
# Results
<#
body {
    font:normal 68% verdana,arial,helvetica;
    color:#000000;
}
table tr td, table tr th {
    font-size: 68%;
}
#>

# Using a multi-line RegEx tag HTML tag match
($NewHtmlContent = $HtmlContent -replace '(?ms)^\<head.*?</head>', $CssContent)
# Results
<#
<html xmlns:string="xalan://java.lang.String" xmlns:lxslt="http://xml.apache.org/xslt">
body {
    font:normal 68% verdana,arial,helvetica;
    color:#000000;
}
table tr td, table tr th {
    font-size: 68%;
}

#>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.