如何使用 PowerShell 替换 CSV 或 html 文件中的多个值?

发布于 2024-11-10 01:01:43 字数 536 浏览 4 评论 0原文

我编写了一个脚本来将文件夹对象列表读取到显示文件夹属性的 xml 文件中。它还会在稍后的时间点创建另一个 xml 文件。创建 delta xml 文件后,我导入两个 xml 文件并根据文件夹名称进行比较,以显示哪些文件夹已被删除或移除,并将结果以 html 格式保存到文件中以供查看。一切正常,但我想替换结果中的一些值。 compare-object cmd-let 允许我显示一些属性,但通过输入 => 来告诉更改发生在哪一边。对于在增量文件中添加的文件夹,或 <= 对于在增量文件中删除的文件夹。我真的很想替换 SideIndicator 的列名称并替换 =>或 <= 具有更直观的值。我尝试使用 -Replace {$_ $original, $newvalue} 类型方法。我在 Hey Scripting Guy 博客和其他一些示例中找到了指导,但似乎都没有达到我想要的效果。解决这个问题的最佳方法是什么?目前我不存储比较结果,只是格式化并转换为 HTML。任何建议表示赞赏。如果需要,我可以发布代码,但它大约有 60 行长,我真的在寻找完成此任务的最佳方法,而不一定需要有人编写代码。

谢谢!

I wrote a script to read a list of folder objects into an xml file showing the properties for the folder. It also creates another xml file at a later point in time. After the delta xml file is created, I import both xml files and compare them based on the folder name to display which folders have been deleted or removed and save the results in html format to a file for viewing. Everything works well, but I want to replace some of the values in the results. The compare-object cmd-let lets me display some attributes, but tells what side the change was on by putting => for a folder added in the delta file or <= for a folder removed in the delta file. I really would like to replace the column name of SideIndicator and the replace the => or <= values with something more intuitive. I played around with useing -Replace {$_ $original, $newvalue} type method. I found guidance on Hey Scripting Guy blog and some other examples around, but none seemed to do what I want. What is the best way to approach this? Currently I'm not storing the compared results, just formatting and converting to HTML. Any advice is appreciated. I can post the code if needed, but it is about 60 lines long and I'm really looking for the best way to accomplish this, not neccesarily someone to write the code.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2024-11-17 01:01:43

如果您只想替换输出显示中的列名称,可以创建自定义表:

http://gallery.technet.microsoft.com/scriptcenter/ed188912-1a20-4be9-ae4f-8ac46cf2aae4

If you just want to replace column names in the output display, you can create a custom table:

http://gallery.technet.microsoft.com/scriptcenter/ed188912-1a20-4be9-ae4f-8ac46cf2aae4

过去的过去 2024-11-17 01:01:43

这是一种方法,但我最终通过执行以下操作来修改 html 报告:

(Get-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm) | 
Foreach-Object {$_ -replace "SideIndicator", "Change Status"} |
Set-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm
(Get-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm) |
Foreach-Object {$_ -replace "=>", "New"} |
Set-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm
(Get-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm) | 
Foreach-Object {$_ -replace "<=", "Removed"} |
Set-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm 

作为替换我正在寻找的三个不同部分的一个语句,它似乎效果不佳。最终我会尝试一下并简化它一些,但现在它可以满足我的要求。

That is one approach, but I ended up modifying the html report by doing the following:

(Get-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm) | 
Foreach-Object {$_ -replace "SideIndicator", "Change Status"} |
Set-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm
(Get-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm) |
Foreach-Object {$_ -replace "=>", "New"} |
Set-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm
(Get-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm) | 
Foreach-Object {$_ -replace "<=", "Removed"} |
Set-Content $WorkingDirectory\FolderAudit_$UseDateTime.htm 

It didn't seem to work well as one statement that replaced the three different pieces I was looking for. Eventually I'll play around with it and streamline it some, but for now it does what I want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文