Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell losing variables

Posted on 2011-02-23
2
Medium Priority
?
763 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
[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
2 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
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 antispam), the admini…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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