如何针对一组服务器验证本地管理员凭据

问题描述 投票:1回答:2

我有60台服务器,特别是VM。我有一个本地管理员用户名和密码。如果有一个脚本可以检查我是否可以使用本地管理员信用卡作为本地管理员登录这些服务器,而不是使用这些本地管理员信用登录到60台服务器。确切地说,我想检查。如果我可以使用此本地管理员信用卡成功登录到所有60个VM。

我用Google搜索但没有运气。我知道stackoverflow不是直接要求一段代码的地方,但在这种情况下我被迫。即使你没有直接的答案,请指导我某个地方。

gc serverlist.txt | % {
    $admin_share = '\\' + $_ + '\admin$'
    if (Test-Path $admin_share) {
        Write-Host "Access test passed on $($_)"
    } else {
        Write-Host "Access test failed on $($_)"
    }
}
powershell
2个回答
1
投票

这是您尝试的变体。它假定所有计算机都使用相同的用户名和密码。

#Get list of cserver names from file
$Servers = Get-Content -Path  serverlist.txt
#Prompt user foe credentials
$Cred = Get-Credential 

foreach ($Svr in $Servers)
{
    #path to share (\\ServerName\Admin$)
    $Path = '\\' + $Svr + '\Admin$'

    Try
    {
        #Map admin share as PS Drive (NOT visable in the GUI) 
        #ErrorAction stop will jump to catch block if the connection is unsuccessfull
        New-PSDrive -Name TEST -PSProvider FileSystem -Root $Path  -Credential $Cred -ErrorAction Stop

        #Success Message
        Write-Output "Connection Successful $Svr"

        #Remove drive before next loop
        Remove-PSDrive -Name TEST
    }

    Catch
    {
        #Error message if connection fails
        Write-Warning -Message "Connect failed $Svr"
    }
}

0
投票

如果启用了ICMP,则可以将Test-Connection cmdlet与Credential开关一起使用。您可以在此处查看示例3中的更多详细信息:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-connection?view=powershell-6

© www.soinside.com 2019 - 2024. All rights reserved.