【问题标题】:Prompt domain admin credentials before running Powershell script在运行 Powershell 脚本之前提示域管理员凭据
【发布时间】:2019-09-26 07:20:28
【问题描述】:

我想在域管理员凭据下运行一些表单,我需要脚本本身中的全部内容。

我在网上找到了一些技巧,但没有什么非常方便或预期的。我不想使用任何 .bat 文件等。供您参考,我已使用 Powershell Studio 编写了所有脚本。

你们有没有解决方案来收集域管理员凭据、检查这些凭据并最终使用这些凭据运行脚本?

【问题讨论】:

  • Get-Credential 不会做获取部分吗?然后通过访问需要它的东西来测试它。
  • 正如@Lee_Dailey 所说,Get-Credential 是如果您需要在运行时提示输入凭据的方法,但如果您想安全地存储它们以供脚本获取,请查看在previous answer of mine
  • 你是唯一一个会运行这些脚本的人吗?您是否需要存储域管理员凭据?您是否希望运行脚本的人每次都必须输入域管理员?您是否需要它以便没有域管理员可以运行它但看不到用户名和密码?我需要更多信息...
  • @ArcSet,我不是唯一会使用它的人,我不需要存储这些凭据。因此,我需要用户输入他/她的域管理员凭据才能使脚本正常工作。最后,这些表单运行的任何进程都需要用户以域管理员身份登录。

标签: forms powershell scripting credentials


【解决方案1】:

您可以使用 LDAP 检查域凭据:

# Load the required assembly
Add-Type -AssemblyName 'System.DirectoryServices.Protocols'

# Specify credential details
$domain = 'example.com'
$userName = 'Username'
$password = 'Password'

try {
    # Create a credential object
    $netCred = New-Object -TypeName System.Net.NetworkCredential -ArgumentList $userName, $password

    # Create an LdapConnection object
    $connection = New-Object -TypeName  System.DirectoryServices.Protocols.LdapConnection -Argumentlist $domain
    $connection.Credential = $netCred

    # Attempt to connect
    # Will raise an exception if credentials are wrong, DC is unavailable, etc
    $connection.Bind()

    # Do something with the valid credentials
    Write-Output -InputObject "Credentials are good!"
}
catch [System.DirectoryServices.Protocols.LdapException] {
    # Failed to connect, so give the user a friendly error message
    Write-Output -InputObject "Error connecting to the '$domain' as '$userName': $($_.Exception.Message)"
}
finally {
    # Dispose of the connection
    $connection.Dispose()
}

【讨论】:

  • 谢谢你的sn-p。但是,您的脚本会测试域凭据。我的观点是能够以另一个用户身份运行 Powershell 脚本。我不想使用批处理文件或任何同步文件来运行它。我正在考虑创建一个将凭证对象作为参数调用的函数,这样我就可以以另一个用户的身份运行该脚本。这可以想象吗?
猜你喜欢
  • 1970-01-01
  • 2020-06-27
  • 2013-11-24
  • 1970-01-01
  • 2019-03-30
  • 1970-01-01
  • 2023-01-24
  • 2016-09-25
  • 2020-04-30
相关资源
最近更新 更多