Solved

powershell ampersand issue

Posted on 2014-10-22
8
1,153 Views
Last Modified: 2014-10-24
Hi all, i have found the following powershell script online, but when i run it i get many issues like Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
At line:5 char:53.  Any ideas?

thanks

$AllServers=Get-ADComputer -Filter {OperatingSystem -Like "Windows Server*"}
ForEach ($Server in $AllServers){
$Result=Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -Property DNSServerSearchOrder -ComputerName $Server.Name
$output = new-object PSObject
$output | add-member NoteProperty "ComputerName" $Server.Name
$output | add-member NoteProperty "DNSServerSearchOrder" $Result.DNSServerSearchOrder
$output
}
0
Comment
Question by:cwstad2
  • 4
  • 2
8 Comments
 
LVL 16

Assisted Solution

by:Joshua Grantom
Joshua Grantom earned 150 total points
ID: 40396987
There are a few places that it is using HTML for the quote special character (") instead of the actual "quote" character

This " should just be "

Corrected below
$AllServers=Get-ADComputer -Filter {OperatingSystem -Like "Windows Server*"}
ForEach ($Server in $AllServers){
$Result=Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -Property DNSServerSearchOrder -ComputerName $Server.Name
$output = new-object PSObject
$output | add-member NoteProperty "ComputerName" $Server.Name
$output | add-member NoteProperty "DNSServerSearchOrder" $Result.DNSServerSearchOrder
$output
} 

Open in new window

0
 
LVL 68

Accepted Solution

by:
Qlemo earned 350 total points
ID: 40397041
Joshua, there are two semicolons left from replacing " - just remove those too.

However, "we" don't do it this way anymore. More effective, without loosing readability:
ForEach ($Server in Get-ADComputer -Filter {OperatingSystem -Like "Windows Server*"}) {
  $Result=(Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -Property DNSServerSearchOrder -ComputerName $Server.Name).DNSServerSearchOrder
  new-object PSObject -Property @{
      ComputerName  = $Server.Name
      DNSServerSearchOrder = $Result
  }
} 

Open in new window

or, which might perform better depending on the size of AD:
Get-ADComputer -Filter {OperatingSystem -Like "Windows Server*"} | % {
  $Server = $_.Name
  $Result = (Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -Property DNSServerSearchOrder -ComputerName $Server).DNSServerSearchOrder
  new-object PSObject -Property @{
      ComputerName  = $Server
      DNSServerSearchOrder = $Result
  }
} 

Open in new window

0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40397072
Yep, I missed the semicolons, I was in the process of correcting it but we just had a fire drill so I had to leave my desk. Thanks
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40397095
Thank you
0
 
LVL 15

Author Comment

by:cwstad2
ID: 40397688
excellent thanks guys, is it possible to do the same for desktops such as windows XP, 7 and 8.

best regards
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40397703
All you have to do is change the

OperatingSystem -Like "Windows Server*"

to

OperatingSystem -Like "Windows XP*"
OperatingSystem -Like "Windows 7*"
OperatingSystem -Like "Windows 8*"

or you can take it out completely (using Qlemo's version of script)
This will return the result for every computer on your domain.

Get-ADComputer -Filter * | % {
  $comp = $_.Name
  $Result = (Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -Property DNSServerSearchOrder -ComputerName $comp).DNSServerSearchOrder
  new-object PSObject -Property @{
      ComputerName  = $comp
      DNSServerSearchOrder = $Result
  }
} 

Open in new window


or you can do everything but servers by changing -like to -NotLike

Get-ADComputer -Filter {OperatingSystem -NotLike "Windows Server*"} | % {
  $comp = $_.Name
  $Result = (Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -Property DNSServerSearchOrder -ComputerName $comp).DNSServerSearchOrder
  new-object PSObject -Property @{
      ComputerName  = $comp
      DNSServerSearchOrder = $Result
  }
} 

Open in new window

0
 
LVL 15

Author Closing Comment

by:cwstad2
ID: 40402444
Thanks guys
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now