Author Posts

November 1, 2017 at 9:38 am

Hi all,
I have written a line of code which will create a list of ADComputer objects from a list of hostnames based on finding certain 'operatingsystem' value's as below:

foreach ($Machine in $Servers) {
$UseableOS += get-adcomputer -Identity $Machine -Properties * | Select-Object -property name,operatingsystem | where-object -filterscript { $PSItem.operatingsystem -like "Windows Server 2008*" -or $PSItem.operatingsystem -like "Windows Server 2012*" }
}

This works find and dandy and creates a list of all servers with Windows Server 2008 or Windows Server 2012 OS's.
However I need to produce the results of the complete opposite and have tried using the -notlike operator as below...

foreach ($Machine in $Servers) {
$UnUseableOS += get-adcomputer -Identity $Machine -Properties * | Select-Object -property name,operatingsystem | where-object -filterscript { $PSItem.operatingsystem -notlike "Windows Server 2008*" -or $PSItem.operatingsystem -notlike "Windows Server 2012*" }

This is not producing the expected results of finding any other text within the operating system value however it will return blank entries?

Any help as to why this is the case would be much appreciated.

Many thanks,
}

November 1, 2017 at 1:01 pm

Assuming you are looking for something like 2003 server which wouldn't match 2008 or 2012, in boolean world you want to say "find anything that doesn't match 2008 and doesn't match 2012. so try changing your "-or" to "-and".

November 1, 2017 at 1:03 pm

Ahhh … 😉 … a really common mistake … you have to change your filter script from -or to -and. … schould look like this I guess:

foreach ($Machine in $Servers){
    get-adcomputer -Identity $Machine -Properties operatingsystem  | 
        where-object -filterscript { $PSItem.operatingsystem -notlike 'Windows Server 2008*' -and $PSItem.operatingsystem -notlike 'Windows Server 2012*'} |
            Select-Object -property name,operatingsystem
}

November 7, 2017 at 11:49 am

You can try Get-ADComputer cmdlet which gets a computer or performs a search to retrieve multiple computers.

Parameter Set: Filter
Get-ADComputer -Filter [-AuthType {Negotiate | Basic} ] [-Credential ] [-Properties ] [-ResultPageSize ] [-ResultSetSize ] [-SearchBase ] [-SearchScope {Base | OneLevel | Subtree} ] [-Server ] [ ]

Parameter Set: Identity
Get-ADComputer [-Identity] [-AuthType {Negotiate | Basic} ] [-Credential ] [-Partition ] [-Properties ] [-Server ] [ ]

Parameter Set: LdapFilter
Get-ADComputer -LDAPFilter [-AuthType {Negotiate | Basic} ] [-Credential ] [-Properties ] [-ResultPageSize ] [-ResultSetSize ] [-SearchBase ] [-SearchScope {Base | OneLevel | Subtree} ] [-Server ] [ ]