Powershell 中的意外令牌错误 [英] Unexpected token errors in Powershell

查看:71
本文介绍了Powershell 中的意外令牌错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来更改 Windows 服务器中的本地安全策略.当我在 PowerShell 提示符下自行运行这些命令时,它们运行良好.但是,当我运行脚本时,我在表达式或语句中收到一个 Unexpected token 'PasswordComplexity'". 错误.

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.

问题似乎源于脚本似乎没有执行 secedit 命令,因此 get-content 行没有要编辑的文件.

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.

为什么 secedit 没有运行?我尝试将 secedit 命令放在 if 语句之外,但得到相同的结果.

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
}

推荐答案

PowerShell 字符串文字必须用单引号 '...' 括起来:

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

'string'

或双引号 "...":

"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"

将输出:

My number: 123

而这个:

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

将输出:

My number: $var

这篇关于Powershell 中的意外令牌错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆