Solved

Powershell losing variables

Posted on 2011-02-23
2
743 Views
Last Modified: 2012-05-11
Hi there,

sometimes variables are losing their values and I dont know why - e.g.:

$Comp = gwmi -query ("Select SystemType,DnsHostName,Domain,DomainRole from Win32_ComputerSystem")

If ($comp.DomainRole -match "3")
{
$SetUsername=$comp.domain+'\'+$UserName
$domain = New-Object DirectoryServices.DirectoryEntry
$search = [System.DirectoryServices.DirectorySearcher]$domain
$search.Filter = "(&(objectClass=Computer)(cn=$comp.dnshostname))"
$Computer = $search.FindOne().GetDirectoryEntry()
}

The variable $comp.dnshostname is not working at this position:

$search.Filter = "(&(objectClass=Computer)(cn=$comp.dnshostname))"


But if I put the variable into a new one it works - e.g.

$Comp = gwmi -query ("Select SystemType,DnsHostName,Domain,DomainRole from Win32_ComputerSystem")

If ($comp.DomainRole -match "3")
{
$SetUsername=$comp.domain+'\'+$UserName
$GetComp=$comp.dnshostname
$domain = New-Object DirectoryServices.DirectoryEntry
$search = [System.DirectoryServices.DirectorySearcher]$domain
$search.Filter = "(&(objectClass=Computer)(cn=$getcomp))"
$Computer = $search.FindOne().GetDirectoryEntry()
}

I put the variable $comp.dnshostname into a new one $GetComp:

$GetComp=$comp.dnshostname

Now it works at the above mentioned position:

$search.Filter = "(&(objectClass=Computer)(cn=$GetComp))"

Why can't I work with $comp.dnshostname directly?

Cheerio


0
Comment
Question by:ptea
2 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 total points
ID: 34960115
Without testing try to put the var inside this $() like this

$search.Filter = "(&(objectClass=Computer)(cn=$($comp.dnshostname)))"
0
 

Author Comment

by:ptea
ID: 34960242
works - thx
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

20 Experts available now in Live!

Get 1:1 Help Now