ptea
asked on
Powershell losing variables
Hi there,
sometimes variables are losing their values and I dont know why - e.g.:
$Comp = gwmi -query ("Select SystemType,DnsHostName,Dom ain,Domain Role from Win32_ComputerSystem")
If ($comp.DomainRole -match "3")
{
$SetUsername=$comp.domain+ '\'+$UserN ame
$domain = New-Object DirectoryServices.Director yEntry
$search = [System.DirectoryServices. DirectoryS earcher]$d omain
$search.Filter = "(&(objectClass=Computer)( cn=$comp.d nshostname ))"
$Computer = $search.FindOne().GetDirec toryEntry( )
}
The variable $comp.dnshostname is not working at this position:
$search.Filter = "(&(objectClass=Computer)( cn=$comp.d nshostname ))"
But if I put the variable into a new one it works - e.g.
$Comp = gwmi -query ("Select SystemType,DnsHostName,Dom ain,Domain Role from Win32_ComputerSystem")
If ($comp.DomainRole -match "3")
{
$SetUsername=$comp.domain+ '\'+$UserN ame
$GetComp=$comp.dnshostname
$domain = New-Object DirectoryServices.Director yEntry
$search = [System.DirectoryServices. DirectoryS earcher]$d omain
$search.Filter = "(&(objectClass=Computer)( cn=$getcom p))"
$Computer = $search.FindOne().GetDirec toryEntry( )
}
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=$GetCom p))"
Why can't I work with $comp.dnshostname directly?
Cheerio
sometimes variables are losing their values and I dont know why - e.g.:
$Comp = gwmi -query ("Select SystemType,DnsHostName,Dom
If ($comp.DomainRole -match "3")
{
$SetUsername=$comp.domain+
$domain = New-Object DirectoryServices.Director
$search = [System.DirectoryServices.
$search.Filter = "(&(objectClass=Computer)(
$Computer = $search.FindOne().GetDirec
}
The variable $comp.dnshostname is not working at this position:
$search.Filter = "(&(objectClass=Computer)(
But if I put the variable into a new one it works - e.g.
$Comp = gwmi -query ("Select SystemType,DnsHostName,Dom
If ($comp.DomainRole -match "3")
{
$SetUsername=$comp.domain+
$GetComp=$comp.dnshostname
$domain = New-Object DirectoryServices.Director
$search = [System.DirectoryServices.
$search.Filter = "(&(objectClass=Computer)(
$Computer = $search.FindOne().GetDirec
}
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)(
Why can't I work with $comp.dnshostname directly?
Cheerio
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER