jhoward
jhoward

Reputation: 414

Unexpected token errors in Powershell

I'm working on a script to change local security policy in windows server. When I run these commands by themselves in a PowerShell prompt, they work fine. However when I run the script I am getting an "Unexpected token 'PasswordComplexity' in expression or statement." error.

The problem seems to stem from the fact that the script doesn't seem to be executing the secedit command, so the get-content lines have no file to edit.

Why is secedit not running? I've tried putting the secedit command outside of the if statement, but get the same results.

if ($win_ver -match "Server"){
    #export current secuirty policy
    secedit /export /cfg c:\new.cfg
    start-sleep -s 10
    #Disable Password Complexity
    ((get-content c:\new.cfg) -replace (‘PasswordComplexity = 1′, ‘PasswordComplexity = 0′)) | Out-File c:\new.cfg
    #Disable password expiration
    ((get-content c:\new.cfg) -replace (‘MaximumPasswordAge = 42′, ‘MaximumPasswordAge = -1′)) | Out-File c:\new.cfg
    #disable minmum password length
    ((get-content c:\new.cfg) -replace (‘MinimumPasswordLength = 6′, ‘MinimumPasswordLength = 1′)) | Out-File c:\new.cfg
    #import new security settings
    secedit /configure /db $env:windir\security\new.sdb /cfg c:\new.cfg /areas SECURITYPOLICY
}

Upvotes: 19

Views: 146660

Answers (1)

user2555451
user2555451

Reputation:

PowerShell string literals must be enclosed by either single quotes '...':

'string'

or double quotes "...":

"string"

Thus, the and characters that you are using are invalid and need to be replaced:

((get-content c:\new.cfg) -replace ('PasswordComplexity = 1', 'PasswordComplexity = 0')) | Out-File c:\new.cfg

Note too that string literals enclosed by single quotes will not expand variables. In other words, this:

$var = 123
Write-Host "My number: $var"

will output:

My number: 123

whereas this:

$var = 123
Write-Host 'My number: $var'

will output:

My number: $var

Upvotes: 23

Related Questions