0

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

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

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

4

1 回答 1

0

您可以使用 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()
}
于 2019-05-08T15:10:16.283 回答