【问题标题】:Unexpected token errors in PowershellPowershell 中出现意外的令牌错误
【发布时间】:2021-03-04 17:36:16
【问题描述】:

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

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

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

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


    【解决方案1】:

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

    'string'
    

    或双引号"...":

    "string"
    

    因此,您使用的 字符无效,需要替换:

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

    还要注意,用单引号括起来的字符串文字不会扩展变量。换句话说,这是:

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

    将输出:

    My number: 123
    

    而这个:

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

    将输出:

    My number: $var
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      相关资源
      最近更新 更多