Few of the articles are posted as Images, Please use Laptop / Computers to go through the articles for best experience. For phone users, switch to Web Version

Please Share with your colleagues if you found these blogs informative. Happy Learning :-)

Fetching all Windows Server details from Active Directory

 <# Created By Abhishek Bansal

Read Note 

Script Usage - Fetching all Windows Server details in the domain from Active Directory. Useful in doing inventory management.

Pre requisites :: Copy all the code into a text file, save it with an extension ".PS1". Once saved, run this script as Administrator

Execution & Outputs :: Once executed, there will be two Output files created as -

ADCompdetails.csv - Containing details of all the Windows server found in Active Directory.

Errorlogs.csv - Logs any error while fetching the uptime.#>

 

 $inputsrv = $(Get-ADComputer -Filter * -Properties OperatingSystem | ?{$_.OperatingSystem -match "Server"}).Name

foreach($server in $inputsrv)

{

$error.Clear()

    try{

    Get-ADComputer -Identity $server -Properties * | Select Name,OperatingSystem,DistinguishedName,IPv4Address,Enabled,Created | Export-Csv ./ADCompdetails.csv -NoTypeInformation -Append

        }

    catch

    {

    $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="ErrorMessage";e={$error.exception.Message}} | Export-Csv ./Errorlogs.csv -NoTypeInformation -Append

    }

}

 


Fetching Uptime of Remote Windows Servers

  <# Created By Abhishek Bansal

Read Note 

Script Usage - Useful in fetching uptime of Servers mentioned in a txt file.

Pre requisites :: Copy all the code into a text file, save it with an extension ".PS1". On same location create a txt file named "Servers.txt" containing name of servers. Once saved, run this script as Administrator.

Execution & Outputs :: Once executed, there will be two Output files created as -

Uptime.csv - Containing uptime of Servers

Errorlogs.csv - Logs any error while fetching the uptime.#>


function fetchuptime($server)

{

    try{

    $Error.Clear()

    $details = Invoke-Command -ComputerName $server -ErrorAction Stop -ScriptBlock{Get-CimInstance -ClassName Win32_OperatingSystem}

    return $($details.LocalDateTime - $details.LastBootUpTime)

    }

    catch

    {

    Write-Host -ForegroundColor Red "Logging error while fetching uptime of $server."

    $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="ErrorMessage";e={$Error.exception.Message}} | Export-Csv ./Errorlogs.csv -NoTypeInformation -Append

    return "Error"  

    }

}


$Servers = Get-Content .\Servers.txt

Write-Host "Total Servers count = $($servers.count)"

#Pause for confirmation..

foreach($server in $Servers)

{

$server = $server.trim()

 try{

    $Error.Clear()

    Write-Host -ForegroundColor Yellow "`nChecking if $server exist in the domain or not ..."

    $compdetails = Get-ADComputer -Identity $server -Properties OperatingSystem 

    Write-Host -ForegroundColor Green "$server exist in domain having $($compdetails.OperatingSystem) OS."

    $uptime = fetchuptime -server $server

    if($uptime -eq "Error")

    {

   continue

    }

    else

    {

    $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Uptime";e={$(echo "Uptime is $($uptime.Days)Days $($uptime.Hours)Hrs $($uptime.Minutes)mins")}} | Export-Csv ./Uptime.csv -NoTypeInformation -Append

    }

   }

catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

    {

    Write-Host -ForegroundColor Red "Logging error while fetching status of $server."

    $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="ErrorMessage";e={$Error.exception.Message}} | Export-Csv ./Errorlogs.csv -NoTypeInformation -Append

    }

}