Solved

Powershell losing variables

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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