Solved

Powershell losing variables

Posted on 2011-02-23
2
742 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

19 Experts available now in Live!

Get 1:1 Help Now