Hola, alguien me pueda ayudar. el problema es el siguiente que una no puedo resolverlo estoy un poco estancado.
Tengo que realizar un script que me resuleva el PING y DNS y toso esto escrtibirlo en un CSV. tengo hasta al momento el codigo pero aun no puedo armarlo dando tantas vueltas ya me quede un poco bloqueado. a ver si alguien mepuede ayudar gracias por todo.
Saludos
Clear
$log = "C:\log.log"
$output = @()
## Validar si el equipo hace PING
$PingMachines = Get-Content "C:\sql.txt"
ForEach($MachineName In $PingMachines){
$PingStatus = Get-WmiObject Win32_PingStatus -Filter "Address = '$MachineName'" | Select-Object StatusCode
#$tmp = New-Object Psobject -Property @{Name = $MachineName; Status = $PingStatus.StatusCode}
If ($PingStatus.StatusCode -eq 0){
$output += = "$machines - OK"
}
Else{
$output += = "$machines - NO_OK"
}
## Validar si el equipo resuelve el DNS
$result = $null
$currentEAP = $ErrorActionPreference
$ErrorActionPreference = "silentlycontinue"
$result = [System.Net.Dns]::GetHostbyName($MachineName)
$ErrorActionPreference = $currentEAP
If($Result){
$output += [string]$Result.HostName
}
Else{
$output += = "$machines - No HostNameFound"
}
}
$output | Export-csv c:\export.csv -NoTypeInformation
Solucion :
Si alguien esta interesado
$computers= Get-Content C:\sql.txt
$list = @()
Foreach ($computername in $computers)
{
If(Test-Connection $computername -Quiet)
{
write-host "$computername - $IP - OK" -ForegroundColor GREEN
Try
{
$IP = [System.Net.Dns]::GetHostEntry($computername).AddressList | %{$_.IPAddressToString}
$IP | %{$HostName = [System.Net.Dns]::GetHostEntry($_).HostName}
}
Catch
{
write-error "NO se puede resolver el IP o DNS."
}
$compStatus = New-Object PSObject -Property @{
Equipo = $computername
Status = $true
DNS = $HostName
IP = $IP
}
$list += $compStatus
}
Else
{
write-host "$computername - $IP - NO_OK" -ForegroundColor RED
$IP = $null
$HostName = $null
$compStatus = New-Object PSObject -Property @{
Equipo = $computername
Status = $false
DNS = $null
IP = $null
}
$list += $compStatus
}
}
$list | Export-Csv c:\prueba2.csv -NoTypeInformation