Reputation: 395
I have 60 servers, VMs to be particular. And I have a local admin username and password. Instead of logging on to 60 servers using these local admin creds, it would be better if there is a script to check if I could log on to these servers as a local admin using the local admin creds. To be precise, I want to check.if I can use this local admin creds to successfully log on to all 60 VMs.
I have googled but no luck. I know stackoverflow is not a place to directly ask for a piece of code, but in this case I am forced to. Even if you dont have a direct answer please direct me somewhere.
gc serverlist.txt | % {
$admin_share = '\\' + $_ + '\admin$'
if (Test-Path $admin_share) {
Write-Host "Access test passed on $($_)"
} else {
Write-Host "Access test failed on $($_)"
}
}
Upvotes: 1
Views: 1412
Reputation: 364
This is a variation of your attempt. It assumes the same username and password for all computers.
#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"
}
}
Upvotes: 1
Reputation: 5
If you have ICMP enabled, you could use Test-Connection cmdlet with the Credential switch. You can see more details in example 3 here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-connection?view=powershell-6
Upvotes: 0