Reputation: 35
I've been utilising PowerShell to query a domain and filter results on domain clients and servers within the domain for reporting purposes.
I've got a working PowerShell command that utilises the "Get-ADComputer" module to list all members of the domain and display there OS and OS properties in a table which can be exported to csv.
PS C:\Users\Account.Domain> Get-ADComputer -Filter * -Property * | Format-Table Name,OperatingSystem,OperatingSystemService
Pack,OperatingSystemVersion -Wrap –Auto
Name OperatingSystem OperatingSystemServicePack OperatingSystemVersion
---- --------------- -------------------------- ----------------------
SRV-DC02 Windows Server 2012 R2 Datacenter 6.3 (9600)
SRV-DC01 Windows Server 2012 R2 Datacenter 6.3 (9600)
SRV-FTP01 Windows Server 2012 R2 Datacenter 6.3 (9600)
SRV241 Windows Server 2008 R2 Standard Service Pack 1 6.1 (7601)
Computer01 Windows 7 Professional Service Pack 1 6.1 (7601)
Computer02 Windows 8.1 Pro 6.3 (9600)
I'm trying to utilise 2 additional versions of this command which filter out the above results further and only display entries which include the Get-ADComputer Properties field of 'OperatingSystem' that include the words "Windows Server". This PowerShell command is throwing a syntax error which I'm not sure of the cause?
PS C:\Users\Account.Domain> Get-ADComputer -Filter {OperatingSystem -Like “Windows *Server*”} -Property * | Format-Table Na
me,OperatingSystem,OperatingSystemServicePack -Wrap –Auto
Get-ADComputer : Error parsing query: 'OperatingSystem -Like “Windows *Server*”' Error Message: 'syntax error' at
position: '23'.
At line:1 char:1
+ Get-ADComputer -Filter {OperatingSystem -Like “Windows *Server*”} -Property * | ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADComputer], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr
osoft.ActiveDirectory.Management.Commands.GetADComputer
The second iteration of the command is the reverse of the first; it should exclude all entries which contain the word "server" however this command is also not working and throws the below syntax error?
PS C:\Users\Account.Domain> Get-ADComputer -Filter {OperatingSystem -NotLike “*server*”} -Property * | Format-Table Name,Op
eratingSystem,OperatingSystemServicePack -Wrap -Auto
Get-ADComputer : Error parsing query: 'OperatingSystem -NotLike “*server*”' Error Message: 'syntax error' at position:
'26'.
At line:1 char:1
+ Get-ADComputer -Filter {OperatingSystem -NotLike “*server*”} -Property * | Forma ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADComputer], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr
osoft.ActiveDirectory.Management.Commands.GetADComputer
Thanks.
Upvotes: 1
Views: 1547
Reputation: 437978
The likely cause of your problem is that you're using non-ASCII-range ("Unicode") quotation marks in your filters, which I'm guessing is not supported (I can't verify this myself).
While PowerShell itself treats ASCII-range and equivalent non-ASCII-range quotation marks interchangeably, the outside world usually does not.
While it may not be obvious, arguments you pass to -Filter
are the outside world, because it is the Active Directory provider that interprets it, whose syntax rules differ from PowerShell's, despite superficial similarities.
Therefore, you should formulate your -Filter
arguments with ASCII-range quotation marks only:
-Filter 'OperatingSystem -like "Windows *Server*"'
-Filter 'OperatingSystem -notlike "Windows *Server*"'
Note that I've used '...'
- single-quoting - to enclose the argument, rather than script-block syntax ({ ... }
), which - although (regrettably) widespread - is best avoided.
Always use a string - never a script block ({ ... }
) - to pass a -Filter
argument.
Upvotes: 1