Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

powershell ampersand issue

Posted on 2014-10-22
8
Medium Priority
?
1,505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
8 Comments
 
LVL 16

Assisted Solution

by:Joshua Grantom
Joshua Grantom earned 600 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 71

Accepted Solution

by:
Qlemo earned 1400 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

715 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