Retreiving ADComputer Obj based on filter...

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
cody m

Re: Retreiving ADComputer Obj based on filter...

Post by cody m »

So you're almost right with using -notlike to get everything that is not Windows 2008, and Windows 2012; but you need to use -and rather than -or so it would look like this.

Code: Select all

foreach ($Machine in $Servers) {
$UnUseableOS += get-adcomputer -Identity $Machine -Properties * | Select-Object -property name,operatingsystem | where-object -filterscript { $PSItem.operatingsystem -notlike "Windows Server 2008*" -and $PSItem.operatingsystem -notlike "Windows Server 2012*" }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Retreiving ADComputer Obj based on filter...

Post by jvierra »

You don't need to or want to use select-object:

Code: Select all

$servers |
    ForEach-Object{get-adcomputer $_ -Properties operatingsystem} |
    Where-Object{$_.operatingsystem -notmatch 'Windows Server 2008|Windows Server 2012' }|
    Select-Object name, operatingsystem
Selecting and formatting should always be done last. Using "match" is more flexible and can coalesce logic statements for simplicity and readability.
This topic is 6 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked