在PowerShell中缺少终结符和缺少关闭

时间:2016-05-10 10:20:51

标签: powershell syntax

我对PowerShell很陌生,如果这对我来说很明显,那就道歉了。

我有以下PowerShell脚本;

<input type="text" id="text1" runat="server" onkeypress="return IsAsterik(event);" 
        ondrop="return false;" onpaste="return false;" />
<span id="error" style="color: Red; display: none">* not allowed</span>

我收到以下错误......

$FieryChasm = {

  Clear-Host

    Write-Host "`n This script is for dropping user accounts from the Active Directory.`n`n`n It will :`n`n - Disable the AD account`n - Reset the AD password`n - Move the account to the Disabled OU`n - Set the expiry date on the account to yesterday's date`n - Remove all @ groups`n- Hide the user from the email exchange`n`n`n`n Input the UserID`n"

    $UserID = Read-Host -Prompt ' '
    Clear-Host

    $title = "`n You input '$UserID'"
    $message = "`n`n Are you certain you want to process this UserID as a leaver?`n`n`n`n"

    $yes = New-Object System.Management.Automation.Host.ChoiceDescription " &Yes", `
    "Yes, process this userID as a leaver.`n"

    $no = New-Object System.Management.Automation.Host.ChoiceDescription " &No", `
    "No, take me back a step so I can input the UserID again.`n"

    $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

    $result = $host.ui.PromptForChoice($title, $message, $options, 1)

    Clear-Host

    switch ($result)
    {
      0 {
        Write-Host "`n Disabling account...`n"
          Disable-ADAccount -Identity $UserID

          Write-Host "`n Moving to OU 'Disabled Accounts'...`n"
          Move-ADObject -Identity $UserID -TargetPath "OU=Disabled Accounts,DC=my-company,DC=co,DC=uk"

          Write-Host "`n Resetting password...`n"
          $YouShallNotPass = (Get-Random -input "Da$her","Danc%r","Pr$ncer","V!xen","C$met","Cup!d","Donn%r","Bl!tzen") + (Get-Random -Minimum 1000 -Maximum 999999) + (Get-Random -input "Da$her","Danc%r","Pr$ncer","V!xen","C$met","Cup!d","Donn%r","Bl!tzen")
          Set-ADAccountPassword -Reset -NewPassword $YouShallNotPass –Identity $UserID

          Write-Host "`n Setting expiry date...`n"
          $Yesterday = (Get-Date).AddDays(-1).ToString('dd/MM/yyyy')
          Set-ADAccountExpiration $UserID -DateTime $Yesterday

          Write-Host "`n Removing AD groups...`n"
          Get-ADuser $UserID -property MemberOf | % {$_.MemberOf | Get-ADGroup | select Name | sort name} | clip
          Get-ADGroup -Filter 'name -like "@*"' | Remove-ADGroup -identity $UserID

          Write-Host "`n Hiding user from Exchange...`n"
          Set-Mailbox -Identity DOMAIN\$UserID -HiddenFromAddressListsEnabled $true

          Write-Host "`n Completed.`n`n $UserID has been processed as a leaver.`n`n`n Press any key to go back to the fiery chasm from whence you came ..."                               

          $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

      }

      1 {
        Write-Host "`n You selected No.`n`n User was NOT set as a leaver.`n`n`n Press any key to go back to the fiery chasm from whence you came ..."
      }
    }

  .$FieryChasm
}

有人可以解释原因,并给我一些提示,让我不再犯同样的错误吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

啊,明白了!

第37行,就是这个;

Set-ADAccountPassword -Reset -NewPassword $YouShallNotPass -Identity $UserID

标识之前的破折号&#39;是一个不正确的Unicode字符。我更换了它,错误消失了:))