Powershell Compare-Object and getting the Differences into a File

被刻印的时光 ゝ 提交于 2019-12-13 03:37:39

问题


I'm really new to PS and have been cracking my head on a particular problem. I'm trying to get the differences between what is in AD and what SCOM is monitoring already. Based on that I'll run a discovery or push out a look for certain services and if the ports are open.

I've been able to build two files for comparison using Get-SCOMAgent and Get-ADComputer and it works like a champ. My problem is that the compare-object is somewhat limited in getting what is present in one file and not in the other. So I figured I'd loop through and see what I can find.

"Looping through to compare files"
ForEach ($SERVER in Get-Content "c:\SCOM\XXXX\ServersInADFormatted.txt")
{
    Compare-Object -ReferenceObject $(Get-Content "$SERVER") -DifferenceObject $(Get-Content "c:\scom\XXXX\ServersWithSCOMAgentsFormatted.txt")  | Export-Csv -Path "c:\scom\XXXX\DIFFERENCES.csv"
}

Update: I'm trying this now but not getting any output:

ForEach ($SERVER in Get-Content "c:\SCOM\XXXX\ServersInADFormatted.txt")
{
    select-string -path "c:\SCOM\XXXX\ServersWithSCOMAgentsFormatted.txt" -pattern $SERVER
}

Really all this needs to act like is a sdiff or a grep which I can export out to a file that I can use to do the discovery using the SCOM PS API. I know there are differences in the way strings are handled in the win os but not connecting the dots.

Any help is greatly appreciated.

#

For some reason the compare isn't working. Seemingly it is the differences in the amount of whitespaces after the servername. This leads to innaccurate results since the servers are actually in both places but the compare maks them look unique.

Both text files have something like this server1.somedomain.com (spaces after last character)

I know I'll need to strip the whitespaces and have tried .Trimend but when I look at the file in notepad there are still spaces. I need to have a CRLF but that's all.

foreach ($line in $SERVERSINAD) { $SERVERSINAD = $line.TrimEnd(); $line += "$line`n" $SERVERSINAD >FormattedFile }

来源:https://stackoverflow.com/questions/30628041/powershell-compare-object-and-getting-the-differences-into-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!