About Me

My photo
HANUMANGARH, RAJASTHAN, India

Thursday 15 September 2022

Get Domain Name with DC

 $ADForestInfo = Get-ADForest

$ADForestInfoName = $ADForestInfo.Name

$ADForestInfoDomains = $ADForestInfo.Domains

$ADForestInfoForestMode = $ADForestInfo.ForestMode

$AllDCs = $Null

ForEach ($DomainDNS in $ADForestInfoDomains)

{

[string]$DomainDC = (Get-ADDomainController -Domain $DomainDNS -discover -service “ADWS”).HostName

$DomainInfo = Get-ADDomain -server $DomainDC

$DomainInfoDomainMode = $DomainInfo.DomainMode

[array]$AllDomainDCs = Get-ADDomainController -server $DomainDC -filter *

$AllDomainDCsCount = $AllDomainDCs.Count

Write-Output “Discovered $AllDomainDCsCount Domain Controllers in the $DomainDNS Domain (DFL: $DomainInfoDomainMode) ”

[array]$AllDCs += $AllDomainDCs

}

$AllDCsCount = $AllDCs.Count

Write-Output “Discovered $AllDCsCount Domain Controllers in the AD Forest $ADForestInfoName (FFL: $ADForestInfoForestMode) ”

$AllDCs | select domain,name,operatingsystem,OperatingSystemServicePack | sort domain | ft -auto

Get all DC detail with OS, IP, Forest and Site Name

 # Import AD module

Import-Module ActiveDirectory

 

# Get your ad domain

$DomainName = (Get-ADDomain).DNSRoot

 

# Setup email parameters

$subject = "Domain Controllers in $DomainName"

$priority = "Normal"

$smtpServer = "smtp.powershellbros.com"

$emailFrom = "paweljanowicz@powershellbros.com"

$emailTo = (Get-ADUser -Identity $ENV:Username -Properties mail).mail

$port = 25

 

# Get all DC's

$AllDCs = Get-ADDomainController -Filter * -Server $DomainName | Select-Object Hostname,Ipv4address,isGlobalCatalog,Site,Forest,OperatingSystem

  

# Create empty DataTable object

$DCTable = New-Object System.Data.DataTable

      

# Add columns

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[0].Caption = "Hostname"

$DCTable.Columns[0].ColumnName = "Hostname"

  

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[1].Caption = "IPv4Address"

$DCTable.Columns[1].ColumnName = "IPv4Address"

                      

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[2].Caption = "isGlobalCatalog"

$DCTable.Columns[2].ColumnName = "isGlobalCatalog"

$DCTable.Columns[2].DataType = "Boolean"

  

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[3].Caption = "Site"

$DCTable.Columns[3].ColumnName = "Site"

  

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[4].Caption = "Forest"

$DCTable.Columns[4].ColumnName = "Forest"

  

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[5].Caption = "OperatingSystem"

$DCTable.Columns[5].ColumnName = "OperatingSystem"

 

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[6].Caption = "PingStatus"

$DCTable.Columns[6].ColumnName = "PingStatus"

 

# Loop each DC                        

ForEach($DC in $AllDCs)

{  

    $ping = ping $DC.Hostname -n 1 | Where-Object {$_ -match "Reply" -or $_ -match "Request timed out" -or $_ -match "Destination host unreachable"}

 

    switch ($ping)

    {

        {$_ -like "Reply*" }                          { $PingStatus = "Success" }

        {$_ -like "Request timed out*"}               { $PingStatus = "Timeout" }

        {$_ -like "Destination host unreachable*"}    { $PingStatus = "Unreachable" }

        default                                       { $PingStatus = "Unknown" }

    }

          

    $DCTable.Rows.Add(  $DC.Hostname,

                        $DC.Ipv4address,

                        $DC.isGlobalCatalog,

                        $DC.Site,

                        $DC.Forest,

                        $DC.OperatingSystem,

                        $PingStatus

                              

                        )| Out-Null                          

}

 

# Display results in console 

$DCTable | Sort-Object Site | Format-Table

 

#Creating head style

$Head = @"

<style>

  body {

    font-family: "Arial";

    font-size: 8pt;

    }

  th, td, tr { 

    border: 1px solid #e57300;

    border-collapse: collapse;

    padding: 5px;

    text-align: center;

    }

  th {

    font-size: 1.2em;

    text-align: left;

    background-color: #003366;

    color: #ffffff;

    }

  td {

    color: #000000;

     

    }

  .even { background-color: #ffffff; }

  .odd { background-color: #bfbfbf; }

  h6 { font-size: 12pt; 

       font-color: black;

       font-weight: bold;

       }

 

 text { font-size: 10pt;

        font-color: black;

        }

 }

</style>

"@

 

 

# Email body

[string]$body = [PSCustomObject]$DCTable | Select-Object Hostname,Ipv4address,isGlobalCatalog,Site,Forest,OperatingSystem,PingStatus | Sort-Object -Property Site | ConvertTo-HTML -Head $head -Body "<h6>Domain Controllers</h6></font>"

 

# Send the report email

Send-MailMessage -To $emailTo -Subject $subject -BodyAsHtml $body  -SmtpServer $smtpServer -Port $port -From $emailFrom -Priority $priority

 

Resolve SID to user name

 Function f-sid {

param ( [Parameter(Mandatory=$true)][String]$Sid)

$objSID = New-Object System.Security.Principal.SecurityIdentifier($Sid)

Try {

($objSID.Translate( [System.Security.Principal.NTAccount])).value

}

Catch {

Write-host “`nCouldn’t find any entry matching SID : $Sid” -foregroundcolor cyan

}

}

#Put all SID in text file and run:

get-content C:\Users\su-skumar\Desktop\ser.txt | % { f-sid $_ }

Wednesday 7 September 2022

Get installed Software on Local Machine

 $list=@()

$InstalledSoftwareKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"

$InstalledSoftware=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$env:computername)

$RegistryKey=$InstalledSoftware.OpenSubKey($InstalledSoftwareKey) 

$SubKeys=$RegistryKey.GetSubKeyNames()

Foreach ($key in $SubKeys){

$thisKey=$InstalledSoftwareKey+"\\"+$key

$thisSubKey=$InstalledSoftware.OpenSubKey($thisKey)

$obj = New-Object PSObject

$obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $env:computername

$obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))

$obj | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))

$list += $obj

}

$list | where { $_.DisplayName } | select ComputerName, DisplayName, DisplayVersion | FT

Export contact from Justdial

  Extract Data From JustDial using Selenium Let us see how to extract data from Justdial using Selenium and Python. Justdial is a company th...