Difference between revisions of "Powershell snippets"

From UVOO Tech Wiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
## Get A/CNAME DNS Records from Windows DNS
 
## Get A/CNAME DNS Records from Windows DNS
 +
 
get-dnsRecords.ps1
 
get-dnsRecords.ps1
 
```
 
```
 
$ErrorActionPreference = "Stop"
 
$ErrorActionPreference = "Stop"
  
if ($args.Count -lt 1) {
+
if ($args.Count -lt 2) {
     Write-Host "Usage:  get-dnsRecords.ps1 <zone name/domain>"
+
     Write-Host "Usage:  get-dnsRecords.ps1 <zone name/domain> <windows dns hostname>"
     Write-Host "Example: get-dnsRecords.ps1 example.com"
+
     Write-Host "Example: get-dnsRecords.ps1 example.com my-windows-dns-host"
 
     exit 1
 
     exit 1
 
}
 
}
  
 
$zoneName = $args[0]
 
$zoneName = $args[0]
 
+
$dnsHost = $args[1]
# write-host "$zoneName"
 
# exit
 
  
 
$rsp = Invoke-Command -ComputerName wjp1-dc -ScriptBlock {
 
$rsp = Invoke-Command -ComputerName wjp1-dc -ScriptBlock {
   param($zoneName)
+
   param($zoneName, $dnsHost)
 
   $dnsRecords = Get-DnsServerResourceRecord -ZoneName $zoneName -RRType A
 
   $dnsRecords = Get-DnsServerResourceRecord -ZoneName $zoneName -RRType A
 
   foreach ($record in $dnsRecords) {
 
   foreach ($record in $dnsRecords) {
Line 27: Line 26:
 
     Write-Output "$($record.HostName).$zoneName"
 
     Write-Output "$($record.HostName).$zoneName"
 
   }
 
   }
} -ArgumentList $zoneName
+
} -ArgumentList $zoneName, $dnsHost
 
write-output "$rsp" | Out-File -FilePath ".\$($zoneName).records"
 
write-output "$rsp" | Out-File -FilePath ".\$($zoneName).records"
 
write-host "Records are in in file .\$($zoneName).records"
 
write-host "Records are in in file .\$($zoneName).records"
 +
```
 +
 +
 +
## SSL Verify from Above
 +
- https://github.com/genkiroid/cert
 +
 +
```
 +
$ErrorActionPreference = "Stop"
 +
 +
if ($args.Count -lt 1) {
 +
    Write-Host "Usage:  cert-scan.ps1 <zoneName>"
 +
    Write-Host "Example: cert-scan.ps1 example.com"
 +
    exit 1
 +
}
 +
 +
$zoneName = $args[0]
 +
write-host "Getting hosts from $($zoneName).records "
 +
 +
 +
$hosts = Get-Content .\$($zoneName).records
 +
$textToRemove = "@.$zoneName"
 +
$hosts = $hosts -replace [regex]::Escape($textToRemove), ""
 +
$hosts = -split $hosts
 +
 +
cert.exe -f json -skip-verify $hosts | ConvertFrom-Json | ConvertTo-Json -Depth 10 > .\$($zoneName).json
 +
write-host "Check $($zoneName).json for scan results."
 
```
 
```

Latest revision as of 15:44, 19 January 2024

Powershell Snippets

Get A/CNAME DNS Records from Windows DNS

get-dnsRecords.ps1

$ErrorActionPreference = "Stop"

if ($args.Count -lt 2) {
    Write-Host "Usage:   get-dnsRecords.ps1 <zone name/domain> <windows dns hostname>"
    Write-Host "Example: get-dnsRecords.ps1 example.com my-windows-dns-host"
    exit 1
}

$zoneName = $args[0]
$dnsHost = $args[1]

$rsp = Invoke-Command -ComputerName wjp1-dc -ScriptBlock {
  param($zoneName, $dnsHost)
  $dnsRecords = Get-DnsServerResourceRecord -ZoneName $zoneName -RRType A
  foreach ($record in $dnsRecords) {
    Write-Output "$($record.HostName).$zoneName"
  }
  $dnsRecords = Get-DnsServerResourceRecord -ZoneName $zoneName -RRType CName
  foreach ($record in $dnsRecords) {
    Write-Output "$($record.HostName).$zoneName"
  }
} -ArgumentList $zoneName, $dnsHost
write-output "$rsp" | Out-File -FilePath ".\$($zoneName).records"
write-host "Records are in in file .\$($zoneName).records"

SSL Verify from Above

$ErrorActionPreference = "Stop"

if ($args.Count -lt 1) {
    Write-Host "Usage:   cert-scan.ps1 <zoneName>"
    Write-Host "Example: cert-scan.ps1 example.com"
    exit 1
}

$zoneName = $args[0]
write-host "Getting hosts from $($zoneName).records "


$hosts = Get-Content .\$($zoneName).records
$textToRemove = "@.$zoneName"
$hosts = $hosts -replace [regex]::Escape($textToRemove), ""
$hosts = -split $hosts

cert.exe -f json -skip-verify $hosts | ConvertFrom-Json | ConvertTo-Json -Depth 10 > .\$($zoneName).json
write-host "Check $($zoneName).json for scan results."