Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

powershell ampersand issue

Posted on 2014-10-22
8
Medium Priority
?
1,587 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
7 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

783 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