Powershell embedded stuff

Status
Not open for further replies.
First off, I'm just learning powershell. There is no real reason on why I want to do this code a certain way other than exploration (plus for some reason in my head I don't feel this requires a variable, and fewer variables = good for some reason to me)

Here is what I'm trying to do. I want to set the administrator password to adm_(lowercase computer name).

I know I can do it like this:

Code:
$a = gc env:computername
$a = $a.ToLower()
([adsi]"WinNT://localhost/AdminTest").SetPassword("adm_"+$a)

But what I would like to do is something like:
Code:
([adsi]"WinNT://localhost/AdminTest").SetPassword("adm_"+gc env:computername.ToLower())
Of course that doesn't work.

The error I get:
You must provide a value expression on the right-hand side of the '+' operator.
At line:1 char:58
+ ([adsi]"WinNT://localhost/AdminTest").SetPassword("adm_"+ <<<< gc env:computername.ToLower())
+ CategoryInfo : ParserError: :)) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression

Any way I can condense those 3 lines down into one?

thanks.
 

Bagheera

Ars Tribunus Angusticlavius
8,773
Subscriptor
Perhaps wrap the gc.... command in $()? Something like:
Code:
$(gc env:computername:ToLower)

So the whole command would be:
Code:
([adsi]"WinNT://localhost/AdminTest").SetPassword("adm_"+$(gc env:computername.ToLower()))

Putting text in $() tells Powershell that it's a script block and should be parsed as a command and not just a string of text.


EDIT: If you're scripting this, I think it's better to make it three lines of text rather than one. It might take the computer a hundredth of a second longer to run three lines of code indtead of one, but the code will be much easier to read to anyone who edits it in the future.
 
Status
Not open for further replies.