Solved

powershell ampersand issue

Posted on 2014-10-22
8
1,358 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 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 70

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A brief introduction to what I consider to be the best editor for PowerShell.
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…
The viewer will learn how to dynamically set the form action using jQuery.

752 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