gpt4 book ai didi

regex - 如何使用PowerShell编辑HTML标签?

转载 作者:行者123 更新时间:2023-12-03 00:55:04 25 4
gpt4 key购买 nike

我创建了PS脚本,该脚本查询服务器上的事件日志,然后将信息写入普通的旧html文件。问题是html文件就是这样,plian。我想要做的是向其中添加jquery的DataTables,以便我可以对其进行排序和组织。

使用“ConvertTo-HTML -head”命令行开关,可以在编写文档之前将所需的代码插入到文档的开头。写入文件后,我需要将其打开并更改为。

我遇到的问题是,当脚本进入替换标签部分时,出现以下错误:

Set-Content : The process cannot access the file C:\users\norm\Documents\eventlogs\2011\02\System\04\tc2.html' because it is being used by another process.



我怀疑这是因为用于打开文件的管道仍将其打开。那么如何关闭文件?还是有更好的方法呢?

谢谢,
规范
    # EventLog viewer

# Head for the HTML document. Adds the CSS and JQuery DataTables to the document.
$h = '<head> <meta http-equiv="pragma" content="no-cache" />'
$h = $h + '<title>Celeste EventLog Viewer</title>'
$h = $h + '<link type="text/css" rel="stylesheet" href="css/demo_table_jui.css" />'
$h = $h + '<link type="text/css" rel="stylesheet" href="themes/base/jquery.ui.all.css" />'
$h = $h + '<link type="text/css" rel="stylesheet" href="css/stdcst.css" />'
$h = $h + '<script language="JavaScript" src="js/jquery.min.js"></script>'
$h = $h + '<script language="JavaScript" src="js/jquery.dataTables.js"></script>'
$h = $h + '</head>'
$h = $h + '<script type="text/javascript"> $(document).ready(function() {$("#t1").dataTable( {"bJQueryUI": true,"sPaginationType": "full_numbers", "bInfo" : true } ); });</script>'
$ServerNames = "s1","s2","s3","s4","s5","s6","s7","s8","s9"
$outdir = 'C:\users\norm\Documents\eventlogs\'
$year = get-date -uformat "%Y"
$month = get-date -uformat "%m"
$day = get-date -uformat "%d"
$count = $ServerNames.length

# Get the length of ServerNames, iterate through x until x is greater than ServerNames

for ($x=0; $x -lt $count; $x++)
{
# Before writing the file can begin the output directory needs to exsist. Test that it does, if not create it.
if (( Test-Path -path $outdir$year\$month\System\$day\) -ne $True)
{
New-Item $outdir$year\$month\System\$day\ -type directory
}
if (( Test-Path -path $outdir$year\$month\Application\$day\) -ne $True)
{
New-Item $outdir$year\$month\Application\$day\ -type directory
}
echo "Getting System log from " $ServerNames[$x]
#Query the System Event log and write it to an html file
Get-EventLog -computername $ServerNames[$x] -LogName System | ConvertTo-HTML -head $h | Out-File -encoding utf8 $outdir$year\$month\System\$day\"$($ServerNames[$x]).html";
# Query the Aplication Event log and write it to an html file
echo "Getting Application log from " $ServerNames[$x]
Get-EventLog -computername $ServerNames[$x] -LogName Application | ConvertTo-HTML -head $h | Out-File -encoding utf8 $outdir$year\$month\Application\$day\"$($ServerNames[$x]).html";
}

for ($x=0; $x -lt $count; $x++)
{
#Open the files and replace the table tag
Get-Content $outdir$year\$month\System\$day\"$($ServerNames[$x]).html" | ForEach-Object { $_ -replace '<table>', '<table id="t1">'} | Set-Content $outdir$year\$month\"$($ServerNames[$x]).html";
}

#give me a message to let me know the script has completed.
echo "Done!"

最佳答案

我将使用Handle调查哪些进程访问了该文件。类似地,您可以使用ProcessExplorer(运行,按CTRL + F,输入文件名,按Enter)。

编辑:

查看您的代码后,我看到了问题。
Get-Content | process something | Set-Content是您的问题。更改为此:

$a = Get-Content ... | process ...
$a | Set-Content

Cmdlet Get-Content打开文件,读取一行并将其发送到管道。然后对其进行处理,并将第一行通过管道传递给 Set-ContentSet-Content想写,但是 Get-Content尚未完成,因为它只读取第一行。

关于regex - 如何使用PowerShell编辑HTML标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4898881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com