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

WIMMount (HSM) causing cluster storage to go redirected (2012r2 DC)

Failed to delete the test record dcdiag-test-record in zone test.com

Azure MFA with Azure AD and RDS