variable not expanding in command, unsure why


hello, have relatively straightforward problem in script making, has variable not expanding unsure why , not sure else can further debug issue, advise appreciated.

basically have variable containing pc name:

$pcname="mypc"

and want use in script pc:

get-adcomputer -filter {name -like "$pcname"} -properties lastlogondate 

however, command returns nothing if manually put in name:

get-adcomputer -filter {name -like "mypc"} -properties lastlogondate 

it work properly.

what doing wrong?

i have tried:

get-adcomputer -filter {name -like "$($pcname)"} -properties lastlogondate 

which not work, me looks problem shell not expanding variable inside filter block, i'm not sure why happen.

edit:

i have found out doing:

get-adcomputer -filter {name -like $pcname} -properties lastlogondate 

does work, not me completely, search "$pcname*" continues fail, realize can append * variable before line, still figure out whats going on here why not expand string expected in filter block.


try:

get-adcomputer -filter "name -like `"$pcname*`""



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Generating Event ID 91, 44 with CA (In Event Viewer with source as Certsvc)

Group policy default policy cannot be edited because network name not found

Problem when changing update source