Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SamErde committed Feb 2, 2024
1 parent 9725f6c commit aa26022
Show file tree
Hide file tree
Showing 24 changed files with 1,735 additions and 0 deletions.
212 changes: 212 additions & 0 deletions OldSamples/Add-ADSubnets.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
#* FileName: Add-Subnets.ps1
#*=============================================================================
#* Script Name: Add Subnets
#* Created: [11/31/2010]
#* Author: Jerry Martin
#* Company: The Home Depot
#* Email: [email protected]
#* Web: http://www.homedepot.com
#* Powershell Version: 2.0
#* Reqrmnts: Microsoft Active Directory Module
#* Keywords:
#*=============================================================================
#* Purpose: This script was created to import a list of subnets from a CSV
#* File into Active Directory, Directory Services Site and Services Module.
#* This automates the process of creating Subnets and is intended to be used
#* in conjuction with the Add-SiteLinks, Add-Sites and Get-SitesAndServices
#* Scripts.
#*=============================================================================

#*=============================================================================
#* Usage.
#*=============================================================================
#* The parameters for this script are
#* -InputCSV "Path to CSV File" Default is ".\Subnets.csv"
#* -LogFile "Path to Log File" Default is ".\Logfile.txt"
#* -NewLog Y/N Defaults to N, enter Y to create a new log file
#*
#* Typical Usage would look like the following...
#* .\Add-Subnets.ps1 -InputCSV .\Subnetlist2.csv -Logfile C:\Logfile.txt
#* OR
#* .\Add-Subnets.ps1
#* Without using the Parameters the script will use the default file locations
#* for the Input CSV file and Log File, and will append to the existing log.
#*=============================================================================

#*=============================================================================
#* Creation Resources and Code Samples
#*=============================================================================
#* HOW TO CREATE A FUNCTION TO VALIDATE THE EXISTENCE OF AN AD OBJECT (Test-XADObject)
#* http://blogs.msdn.com/b/adpowershell/archive/2009/05/05/how-to-create-a-function-to-validate-the-existence-of-an-ad-object-test-xadobject.aspx
#*=============================================================================

#*=============================================================================
#* The Script
#*=============================================================================
#Get input from cmd line for Log file location and Input CSV file locations if
#They differ from the defaults.
Param (
[Parameter()][string]$InputCSV='.\Subnets.csv',
[Parameter()][String]$Logfile='.\Logfile.txt',
[Parameter()][String]$NewLog="N")

#Set the Script to continue on errors.
$ErrorActionPreference = "silentlycontinue"

#Clear the cmd window
Clear-Host

#Verify PowerShell Version 2
if ($Host.Version.Major -lt 2)
{Write-Host "Wrong Version of Powershell, Exiting now..."; Sleep 5 ; Exit}

#Verfiy the path to the Input CSV file
if (!(Test-Path $InputCSV))
{Write-Host "Your Input CSV does not exist, Exiting..." ; Sleep 5 ; Exit}

#Check for the existance of the Active Directory Module
If(!(Get-Module -ListAvailable | Where-Object {$_.name -eq 'ActiveDirectory'}))
{ The Active Directory module is not installed on this system. ; Sleep 5 ;exit }

#Import the Active Directory Module
Import-Module ActiveDirectory

#List the Current Domain that the script is using.
Write-host "The current Domain Contoller is" (Get-ADDomainController).HostName -BackgroundColor Yellow -ForegroundColor Black

If ($NewLog -eq 'Y')
{
Write-host "Creating a new log file"
#Rename Existing file to old.
Get-Item $Logfile | Move-Item -force -Destination { [IO.Path]::ChangeExtension( $_.Name, "old" ) }
}

#Add the Current Date to the log file
$Date = Get-Date
Add-Content $Logfile $Date

#Import the SubnetList CSV file.
# Please note,
# We are using the first line of the file to set the property names of the items
# imported, so the csv file should have the following as the first line.
# subnetname,sitename,subnetlocation,subnetdescription
#
# Then the contents should look something like this.
# 10.25.0.0/24,ST9990,"Store 9990","Test Subnet 0"
$SBin = Import-Csv $InputCSV


# Get the Subnet container
$ConfigurationDN = (Get-ADRootDSE).ConfigurationNamingContext
$SubnetDN = ("CN=Subnets,CN=Sites," + $ConfigurationDN)

#Create the funtion to test existance of the subnets for error checking.
#See Creation Resources and Code Samples Section to be linked for more info.
function Test-XADObject()
{
[CmdletBinding(ConfirmImpact="Low")]
Param
(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,HelpMessage="Identity of the AD object to verify if exists or not.")]
[Object] $Identity
)

trap [Exception]
{
return $false
}
$auxObject = Get-ADObject -Identity $Identity
return ($auxObject -ne $Null)
}

#Create the funtion to test existance of the sites for error checking.
# See Creation Resources and Code Samples Section to be linked for more info.
function Test-XADSite()
{
[CmdletBinding(ConfirmImpact="Low")]
Param
(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,HelpMessage="Identity of the AD object to verify if exists or not.")]
[Object] $Identity
)

trap [Exception]
{
return $false
}
$auxObject = Get-ADObject -Filter 'ObjectClass -eq "site"' -SearchBase $ConfigurationDN| Where-Object {$_.Name -eq $Identity}
return ($auxObject -ne $Null)
}

#null out i
$i = $null

#Loop through each line in the CSV file and create the subnets based on info in the CSV.
foreach ($SB in $SBin)
{
#Incriment i for each object in the csv file
$i++
# Create the new subnet container name
$NewSubnetDN = ("CN=" + $SB.subnetname +"," + $SubnetDN)


If (!(Test-XADSite -Identity $SB.Sitename))
{
Write-Host 'Site' $SB.Sitename 'Does Not Exist, Please create Sites before Subnets.' -BackgroundColor Red -ForegroundColor White
}
Else
{
#Test if the new subnet doesn't already exist
If (!(Test-XADObject -Identity $newSubnetDN))
{

#Creating the new subnet
$for = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$fortyp = [System.DirectoryServices.ActiveDirectory.DirectoryContexttype]"forest"
$forcntxt = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext($fortyp, $for)

$subnet = new-object System.DirectoryServices.ActiveDirectory.ActiveDirectorySubnet($forcntxt, $SB.subnetname, $SB.sitename)
$Subnet.Location = $SB.subnetlocation
$subnet.Save()

#Set the Subnet Description
$subnetde = $subnet.GetDirectoryEntry()
$subnetde.Description = $SB.subnetdescription
$subnetde.CommitChanges()

#Verify that the subnet was created sucessfully and update the log
If (Test-XADObject -Identity $newSubnetDN)
{
#Update the console
Write-Host "Created & Verified Subnet:" $SB.subnetname
#Update the log file
Add-Content -Path $Logfile -Value "$($SB.subnetname),Created and Verified"
}

#Log that we Attempted to create the subnet but could not verify it.
Else
{
#Update the Console
Write-Host "Created but not Verified Subnet:" $SB.subnetname
#Update the log file
Add-Content -Path $Logfile -Value "$($SB.subnetname),Created but not Verified "
}


}
#Make note that the subnet already existed.
Else {
#Update the Console
Write-Host "Subnet" $SB.subnetname "already exists. Skipping this subnet" -BackgroundColor Red -ForegroundColor White
#Update the log file.
Add-Content -Path $Logfile -Value "$($SB.subnetname),PreExisting"
}
}
#Give a graphical representation of the percentage of work completed.
Write-Progress -activity "Adding Subnets..." -status "Percent added: " -PercentComplete (($i / $SBin.length) * 100)

}

#*=============================================================================
#* End of The Script
#*=============================================================================
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$file = ""
$csv = Import-Csv $file
foreach ($row in $csv) {
$IP = $row.SourceIP
$row.SourceName = ([System.Net.DNS]::GetHostbyAddress($IP)).Hostname
}
$csv | Export-Csv "results.csv" -NoTypeInformation
50 changes: 50 additions & 0 deletions OldSamples/Computer Management/Disable-Poshv2.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[CmdletBinding()]

$TimeStamp = Get-Date -f yyyy-MM-dd--HH-mm
Start-Transcript -Path ".\Disable-Poshv2 Transcript $TimeStamp.log"
$Logfile = ".\Disable-Poshv2 Retries $TimeStamp.log"
$LogfileFailures = ".\Disable-Poshv2 Failures $TimeStamp.log"

$ServerFailures = @()
$exclusions = @("","","")

#Filtering out production servers as well as any SQL, Exchange, SharePoint (SPS), Telephony, rinf* servers, and any servers in the "Non Windows" OU.
$servers = (Get-ADComputer -Filter 'OperatingSystem -like "Windows Server*" -and Enabled -eq "True" -and Name -notlike "*sps*"' `
-SearchBase "DC=DOMAIN,DC=COM" -SearchScope Subtree).Name | Where-Object {$_ -notin $Exclusions} | Sort-Object
Write-Information "$servers.Count servers found."

foreach ($server in $servers) {
if (Test-WSMan -ComputerName $server -ErrorAction Ignore) {
try {
#If PowerShell version is higher than 2 and PowerShell-V2 InstallState is not 'Removed', then remove it through a remote session.
Invoke-Command -ComputerName $server -ErrorAction Stop -ScriptBlock {
if ( (Get-WindowsFeature PowerShell-V2).InstallState -ne "Removed" -and ($PSVersionTable.PSVersion.Major) -gt 2 ) {
Remove-WindowsFeature PowerShell-V2 -Confirm:$false | Format-Table @{Name="Server";Expression={& hostname.exe}},Success,ExitCode,RestartNeeded,FeatureResult
}
}
} #try
#The preceding if statement that tests WSMan connectivity will most likely preclude this catch statement from being encounterd.
catch {
Write-Warning "Trying to enable PS Remoting on $server."
& psexec \\$server -s powershell.exe Enable-PSRemoting -Force > $null

#If PowerShell version is higher than 2 and PowerShell-V2 InstallState is not 'Removed', then remove it through a remote session.
Invoke-Command -ComputerName $server -ErrorAction Continue -ScriptBlock {
if ( (Get-WindowsFeature PowerShell-V2).InstallState -ne "Removed" -and ($PSVersionTable.PSVersion.Major) -gt 2 ) {
Remove-WindowsFeature PowerShell-V2 -Confirm:$false | Format-Table @{Name="Server";Expression={& hostname.exe}},Success,ExitCode,RestartNeeded,FeatureResult
}
}
$error[0] | Out-File $Logfile -Append
} #catch
} #if
else {
Write-Warning "Failed to connect to $server."
$ServerFailures += $server
} #else
$ServerFailures | Out-File $LogfileFailures
} #foreach

if ($ServerFailures.Count -gt 0) {
Write-Warning "Some servers failed. Please review $LogfileFailures."
}
Stop-Transcript
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Import a CSV that should at least have a "SourceIP" column and a "Hostname" column.
$csv = ".\IPAddresses.csv"

# Import the CSV to a variable so we can update it and write the information back to the CSV at the end.
$IPAddressList = Import-Csv -Path $csv
# Using PowerShell 7's foreach-parallel. Should wrap this in a block that checks which version of PS is being used.
$IPAddressList | foreach-object {
$ip = $_.SourceIP
try {
$_.Hostname = ([System.Net.Dns]::GetHostEntry($ip)).HostName
}
catch {
Write-Error $error[0] #.Exception.Message.Split(':')[1]
}
}
# Write the data back to the CSV with the hostnames added.
$IPAddressList | Export-Csv -Path $csv
97 changes: 97 additions & 0 deletions OldSamples/Computer Management/Get-DNSHostEntryAsync.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
Function Get-DNSHostEntryAsync {
<#
.SYNOPSIS
Performs a DNS Get Host asynchronously
.DESCRIPTION
Performs a DNS Get Host asynchronously
.PARAMETER Computername
List of computers to check Get Host against
.NOTES
Name: Get-DNSHostEntryAsync
Author: Boe Prox
Version History:
1.0 //Boe Prox - 12/24/2015
- Initial result
.OUTPUT
Net.AsyncGetHostResult
.EXAMPLE
Get-DNSHostEntryAsync -Computername google.com,prox-hyperv,bing.com, github.com, powershellgallery.com, powershell.org
Computername Result
------------ ------
google.com 216.58.218.142
prox-hyperv 192.168.1.116
bing.com 204.79.197.200
github.com 192.30.252.121
powershellgallery.com 191.234.42.116
powershell.org {104.28.15.25, 104.28.14.25}
.EXAMPLE
Get-DNSHostEntryAsync -Computername 216.58.218.142
Computername Result
------------ ------
216.58.218.142 dfw25s08-in-f142.1e100.net
#>
#Requires -Version 3.0
[OutputType('Net.AsyncGetHostResult')]
[cmdletbinding()]
Param (
[parameter(ValueFromPipeline=$True)]
[string[]]$Computername
)
Begin {
$Computerlist = New-Object System.Collections.ArrayList
If ($PSBoundParameters.ContainsKey('Computername')) {
[void]$Computerlist.AddRange($Computername)
} Else {
$IsPipeline = $True
}
}
Process {
If ($IsPipeline) {
[void]$Computerlist.Add($Computername)
}
}
End {
$Task = ForEach ($Computer in $Computername) {
If (([bool]($Computer -as [ipaddress]))) {
[pscustomobject] @{
Computername = $Computer
Task = [system.net.dns]::GetHostEntryAsync($Computer)
}
} Else {
[pscustomobject] @{
Computername = $Computer
Task = [system.net.dns]::GetHostAddressesAsync($Computer)
}
}
}
Try {
[void][Threading.Tasks.Task]::WaitAll($Task.Task)
} Catch {}
$Task | ForEach {
$Result = If ($_.Task.IsFaulted) {
$_.Task.Exception.InnerException.Message
} Else {
If ($_.Task.Result.IPAddressToString) {
$_.Task.Result.IPAddressToString
} Else {
$_.Task.Result.HostName
}
}
$Object = [pscustomobject]@{
Computername = $_.Computername
Result = $Result
}
$Object.pstypenames.insert(0,'Net.AsyncGetHostResult')
$Object
}
}

}
Loading

0 comments on commit aa26022

Please sign in to comment.