在Powershell中使用Microsoft HTML文档类编辑HTML

在Powershell中使用Microsoft HTML文档类编辑HTML,html,powershell,dom,Html,Powershell,Dom,我正在尝试在powershell中编辑一个HTML文件,我想删除标记头及其内容,并将CSS元素样式及其内容添加到HTML文件中。我正在使用Microsoft HTML文档类访问HTML文件。 我收到一个错误,在powershell中的“$($($HTML.getElementsByTagName('head')).innerHTML)=$CssContent”这一行,赋值表达式无效 HTML文件 powershell脚本 继续我的评论 对于这个用例,您也可以使用RegEx,尽管相对于使用XML

我正在尝试在powershell中编辑一个HTML文件,我想删除标记头及其内容,并将CSS元素样式及其内容添加到HTML文件中。我正在使用Microsoft HTML文档类访问HTML文件。 我收到一个错误,在powershell中的“$($($HTML.getElementsByTagName('head')).innerHTML)=$CssContent”这一行,赋值表达式无效

HTML文件

powershell脚本


继续我的评论

对于这个用例,您也可以使用RegEx,尽管相对于使用XML cmdlet,它是不受欢迎的

($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%;
}

#>
($HtmlContent=@)
总结
"@ )
#结果
($CssContent=@'
身体{
字体:标准68%verdana,arial,helvetica;
颜色:#000000;
}
表tr td,表tr th{
字体大小:68%;
}
'@)
#结果
#使用多行正则表达式标记HTML标记匹配

($NewHtmlContent=$HtmlContent-replace'(?ms)^\为什么不使用内置的PowerShell cmdlet for XML?--
Get命令名“*XML*”
。将CSS分配给
innerHTML
毫无意义。您必须将
style
元素作为
head
元素的子元素附加。我正计划使用PowerShell replace将样式添加到HTML文件中
body {
    font:normal 68% verdana,arial,helvetica;
    color:#000000;
}
table tr td, table tr th {
    font-size: 68%;
}
#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"
($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%;
}

#>