Powershell 比较对象未按预期工作

发布于 2024-12-05 11:09:44 字数 787 浏览 0 评论 0原文

我正在运行以下脚本来将变量中的数据与文本文件中的数据进行比较。结果显示变量中的数据没有与文本文件相同,尽管我将它们格式化为相同并且它们在结果中看起来相同?它们看起来相同,为什么 powershell 认为它们不同?

脚本:

$rn = Get-WMIObject Win32_Process -computer servername -credential domain\administrator -filter "Name='process.exe'” | Select-Object path | Sort-Object path
$lst = Get-Content “C:\Scripts\Process Monitor Scripts\ProcessList.txt”
Compare-Object $lst $rn

Results:
InputObject                             SideIndicator
-----------                             -------------
@{Path=Y:\Folder1\process.exe}        =>
@{Path=Y:\Folder2\process.exe}        =>
@{Path=Y:\Folder3\process.exe}        =>
@{Path=Y:\Folder1\process.exe}        <=
@{Path=Y:\Folder2\process.exe}        <=
@{Path=Y:\Folder3\process.exe}        <=

I'm running the following script to compare data within a variable to data within a text file. The results show that non of the data within the variable is the same as the text file, although I've formatted them the same and they look the same in the results? They look identical, why does powershell think they're different?

Script:

$rn = Get-WMIObject Win32_Process -computer servername -credential domain\administrator -filter "Name='process.exe'” | Select-Object path | Sort-Object path
$lst = Get-Content “C:\Scripts\Process Monitor Scripts\ProcessList.txt”
Compare-Object $lst $rn

Results:
InputObject                             SideIndicator
-----------                             -------------
@{Path=Y:\Folder1\process.exe}        =>
@{Path=Y:\Folder2\process.exe}        =>
@{Path=Y:\Folder3\process.exe}        =>
@{Path=Y:\Folder1\process.exe}        <=
@{Path=Y:\Folder2\process.exe}        <=
@{Path=Y:\Folder3\process.exe}        <=

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

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

发布评论

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

评论(1

烛影斜 2024-12-12 11:09:44

展开路径属性,您将具有路径属性的对象与简单字符串进行比较:

$rn = Get-WMIObject Win32_Process -computer servername -credential domain\administrator -filter "Name='process.exe'” | select -expand path

Expand the path property, you're comparing objects with a path property against simple strings.:

$rn = Get-WMIObject Win32_Process -computer servername -credential domain\administrator -filter "Name='process.exe'” | select -expand path
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文