Solved

powershell ampersand issue

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

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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

679 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