0

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 }

  • Compare-Object is definitely one of the flakier cmdlets. It doesn't really behave like anything else. It often behaves more sensibly if you include the `-Passthru` common parameter. Back up, pick two files, and get the syntax working how you want. Then get it working in a loop. Alternately, you can ignore Compare-Object build your own loop with Get-Content (cat) and Select-String (almost grep). Other than that I don't really see a question here. – Bacon Bits Jun 03 '15 at 18:47
  • can you add a `write-host $SERVER` in your ForEach loop and make sure `$SERVER` matches what you expect? – LinkBerest Jun 03 '15 at 20:35
  • Thanks. I did try that and the data comes back with what looks like a cat/type of the file. Yet trying to get the output from the select-string yields nothing. I've tried straight file redirects (> and >>) out-file in the pipe and nothing comes of $SERVER in the loop from the file. I gotta be missing something – Michael Colletti Jun 03 '15 at 21:04
  • 1
    Can you edit and add some examples of the text file contents and the output you want? The first loop of your code is trying to load the content of a file called the servername and compare it to another file and then Export-CSV every time through the loop overwriting the output file every time. The second one looks like it should work, but presumably the formatting is different or clashing between the two files. One compare should do it: `Compare -Ref (gc ADservers.txt) -Diff (gc SCOMservers.txt) | Where SideIndicator -eq "<=" | select InputObject`. – TessellatingHeckler Jun 03 '15 at 22:53
  • Thank You! Thank you, TessellatingHeckler !!! That was it!! I am so happy and grateful!!! You rule! – Michael Colletti Jun 04 '15 at 17:41

0 Answers0