如何验证PowerShell凭据是域凭据?

时间:2017-02-17 17:02:48

标签: powershell authentication active-directory credentials

此处发布的解决方案就是我在下面尝试的内容:Validating PowerShell PSCredentials

我的脚本要求将域凭据作为参数传递。以下是我尝试验证信用的方式:

if ($username -and $pass) {
  $Password = ConvertTo-SecureString $pass -AsPlainText -Force
  $Credentials = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList $username, $Password

  $TestUsername = $Credentials.username
  $TestPassword = $Credentials.GetNetworkCredential().password

  # Get current domain using logged-on user's credentials
  $CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
  $domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$TestUsername,$TestPassword)

  if ($domain.name -eq $null) {
   write-host "Authentication failed - please verify your username and password."
   exit #terminate the script.
  } else {
   write-host "Successfully authenticated with domain $domain.name"
  }

}

但出于某种原因,这总是让我感到"Authentication failed - please verify your username and password"。问题是什么,您的解决方案是否也可以使用 runas 命令,其中不将cos作为参数传递?

0 个答案:

没有答案