InfrastructureFunctionsModule.psm1

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2022 v5.8.210
     Generated on: 14/10/2022 11:29
     Generated by: michaelq
    --------------------------------------------------------------------------------
    .DESCRIPTION
        Script generated by PowerShell Studio 2022
#>



    <#
        ===========================================================================
         Created with: SAPIEN Technologies, Inc., PowerShell Studio 2022 v5.8.210
         Created on: 12/10/2022 16:45
         Created by: michaelq
         Organization:
         Filename: InfrastructureFunctionsModule.psm1
        -------------------------------------------------------------------------
         Module Name: InfrastructureFunctionsModule
        ===========================================================================
    #>

    
    function Add-Users_to_Group
    {
    Param 
    ( 
        [Parameter(Mandatory = $true)] $SourceFile,  
        [Parameter(Mandatory = $true)] $DstGroupName 
    )  
    $VPNUsers = gc $SourceFile
    $VPNUsers | ForEach-Object { Add-ADGroupMember -Identity $DstGroupName -Members $_ }
    }
    
    Function Get-AllAADRecursiveMembersofGroupsstartingwith{
    
    [cmdletbinding()]
    param(
    [parameter(Mandatory=$True)]$StartofGroupname,
    [parameter(Mandatory=$True)]$OutFileLocation = "c:\Temp" #default
    )
    
    # Connect to azure
    
    Connect-AzureAD
    
    # Functions
    
        # Get-DateTime
        Function Get-DateTime {(Get-Date).ToString("yyyy-MM-dd_HH-mm-ss")}
    
        # Get-RecursiveAzureAdGroupMemberUsers
        Function Get-RecursiveAzureAdGroupMemberUsers{
        [cmdletbinding()]
        param(
           [parameter(Mandatory=$True,ValueFromPipeline=$true)]
           $AzureADGroup
        )
            Begin{
                If(-not(Get-AzureADCurrentSessionInfo)){Connect-AzureAD}
            }
            Process {
                [array]$ErrorArray=@()
                $AzureGroup = Get-AzureADGroup -ObjectId $AzureADGroup
                Write-Verbose -Message "Enumerating $($AzureGroup.DisplayName)"
                $Members = Get-AzureADGroupMember -ObjectId $AzureGroup.ObjectId -All $true
                $UserMembers = $Members | Where-Object{$_.ObjectType -eq 'User'}
                If($Members | Where-Object{$_.ObjectType -eq 'Group'}){
                    $UserMembers += $Members | Where-Object{$_.ObjectType -eq 'Group'} | ForEach-Object {
                        $LoopVar = $_
                        try {
                            Get-RecursiveAzureAdGroupMemberUsers -AzureGroup $_
                        }
                        catch {
                            $LoopVar # Current object that (probably) caused the exception
                            $_       # Current ErrorRecord that holds the exception data
                            {$ErrorArray+=(New-Object PSCustomObject -Property @{"GroupName"=$LoopVar.DisplayName;"Error"=$_;"Erroring Group Detail"=$LoopVar;})}
                            #Write-host -ForegroundColor Red Error enumerating Group - $LoopVar.DisplayName
                            #write-host -ForegroundColor Red $_
                            #write-host -ForegroundColor Red $LoopVar
    
                        }
                    }
                }
            }
            end {
                Return $UserMembers
                # Return $ErrorArray | Export-Csv C:\Temp\Errors_RecursiveGroupMembersOfGroupsStartingWith-$($StartofGroupname)-$($datetime).csv -NoTypeInformation -Append
    
            }
        }
    
    # Enumerate Groups
    
    #$StartofGroupname = Read-Host "Please enter start of AAD GroupName followed by a *"
    
    $Groups = Get-AzureADGroup -Top 100000 | ? {$_.DisplayName -like "$($StartofGroupname)*"} | Sort-Object -Property DisplayName
    
    # GetTime for Output
    
    function Get-DateTime {(Get-Date).ToString("yyyy-MM-dd_HH-mm-ss")}
    $datetime = Get-DateTime
    
    # Configure output array
    
    [array]$OutputArray=@()
    
    
    Foreach ($Group in $Groups)
    {
    $AllNestedUsers = Get-RecursiveAzureAdGroupMemberUsers $Group.ObjectId
    $AllNestedUsers = $AllNestedUsers | Sort-Object -Property DisplayName | Get-Unique
    if ($AllNestedUsers -eq $Null) {$OutputArray+=(New-Object PSCustomObject -Property @{"GroupName"=$Group.DisplayName;"DisplayName"="No Users in Group";"UserPrincipalName"="No Users in Group";;"Usertype"="No Users in Group";"AccountEnabled"="No Users in Group";"Office"="No Users in Group";})}
    else {
    foreach ($AllNestedUser in $AllNestedUsers)
        {$OutputArray+=(New-Object PSCustomObject -Property @{"GroupName"=$Group.DisplayName;"DisplayName"=$AllNestedUser.DisplayName;"UserPrincipalName"=$AllNestedUser.UserPrincipalName;"Usertype"=$AllNestedUser.Usertype;"AccountEnabled"=$AllNestedUser.AccountEnabled;"Office"=$AllNestedUser.PhysicalDeliveryOfficeName;})}}}
    $OutputArray | Export-Csv $OutFileLocation\RecursiveGroupMembersOfGroupsStartingWith-$($StartofGroupname)-$($datetime).csv -NoTypeInformation
    
    }
    
    Function Get-DateTime {(Get-Date).ToString("yyyy-MM-dd_HH-mm-ss")}
    
    Function Get-DCDNSForwarders {
    
    # Define Simple Functions
    
    function Get-DateTime {(Get-Date).ToString("yyyy-MM-dd_HH-mm-ss")}
    
    # Define Variables
    
    $dcs = Get-ADDomainController -Filter * | Sort-Object
    $datetime = Get-DateTime
    $outfile = "c:\temp\DCs_DNSForwarders_$($datetime).csv"
    
    # Define Array
    
    [array]$OutputArray=@()
    
    # Define Task
    
    foreach ($DC in $DCs)
        {
        $date = get-date
        $forwarders = Get-DnsServerForwarder -ComputerName $DC
        $OutputArray+=(New-Object PSCustomObject -Property @{"Date"=$date;"DomainController"=$DC;"DNSForwarderIPAddress"=$forwarders.IPaddress;})
        }
    
    # Output File and Print Data
     
    $OutputArray | Out-File $outfile
    $OutputArray
    }
    
    Function Get-NICDCDNSServers {
    
    # Define Simple Functions
    
    function Get-DateTime {(Get-Date).ToString("yyyy-MM-dd_HH-mm-ss")}
    
    # Define Variables
    
    $dcs = Get-ADDomainController -Filter * | Sort-Object
    $datetime = Get-DateTime
    $outfile = "c:\temp\DCs_Interface_DNSServers_$($datetime).csv"
    
    # Define Array
    
    [array]$OutputArray=@()
    
    # Define Task
    
    foreach ($DC in $DCs)
        {
        $date = get-date
        $CimSession = New-CimSession -ComputerName $dc.Name 
        if ($CimSession -eq $null) {$OutputArray+=(New-Object PSCustomObject -Property @{"Date"=$date;"DomainController"=$DC;"DNSServerIPAddress"="NoResultReturned";})} else 
        {
        $DNSconfig = get-DnsClientServerAddress -CimSession $CimSession | ? {$_.InterfaceAlias -like "Server*" -and $_.AddressFamily -eq "2"}
        $OutputArray+=(New-Object PSCustomObject -Property @{"Date"=$date;"DomainController"=$DC;"DNSServerIPAddress"=$DNSconfig.serveraddresses;})
        }
        }
    
    # Output File and Print Data
     
    $OutputArray | Out-File $outfile
    $OutputArray
    }
    
    function Get-FSMORoles {
    Param 
    ( 
        [Parameter(Mandatory = $true)] $Domain 
    )  
    Import-Module activedirectory
    [array]$OutputArray=@()
    $ADforestRoles = Get-ADForest $Domain  # | ft DomainNamingMaster, SchemaMaster
    $ADDomainRoles = Get-ADDomain $Domain  # | ft InfrastructureMaster, PDCEmulator, RIDMaster
    $OutputArray+=(New-Object PSCustomObject -Property @{"DomainNamingMaster"=$ADforestRoles.DomainNamingMaster; `
                                                        "SchemaMaster"=$ADforestRoles.SchemaMaster; `
                                                        "InfrastructureMaster"=$ADdomainRoles.InfrastructureMaster; `
                                                        "PDCEmulator"=$ADdomainRoles.PDCEmulator; `
                                                        "RIDMaster"=$ADdomainRoles.RIDMaster;})
    
    $OutputArray
    }
    
    Function Get-Uptime {
    
    <#
    .SYNOPSIS
        Gets Local Machine Uptime
    .DESCRIPTION
        Created by Mike Quick 12/10/2022
        Gets Local Machine Uptime
    .PARAMETER Filename
        NONE
    .EXAMPLE
        Get-Uptime
    .LINK
        https://manage.wix.com/dashboard/7ffa7bd8-227b-4959-a850-9ad3e4e64523/blog/a2373973-8168-4160-8de6-03c57bc761c5/edit?tab=published&lang=en
    #>

    
    $Computerinfo = Get-ComputerInfo 
    $Boottime = (get-date) - (gcim Win32_OperatingSystem).LastBootUpTime
     
    write-host  $Computerinfo.OsLastBootUpTime Boot Time
    write-host  $Boottime.Days Days
    write-host  $Boottime.Hours Hours 
    write-host  $Boottime.Minutes Minutes
    write-host  $Boottime.Seconds Seconds
    
    }
    
    function Remove-Whitespace {
        
    <#
    .SYNOPSIS
    Working?
    .DESCRIPTION
    Working?
    Set the parameters for the new rule.
    Working?
    Working?
    .PARAMETER DisplayName
    Working?
    .PARAMETER LocalPort
    Working?
    .PARAMETER Direction
    Working?
    .PARAMETER Action
    Working?
    .PARAMETER Exists
    Working?
    >EXAMPLE
    WORKING?
    #>

    
    [cmdletbinding()]
    
    param 
    (
    [parameter(Mandatory=$True)]$Filename
    )
    $File = Get-Content $Filename
    $File | Foreach {$_.TrimEnd()} | Foreach {$_.TrimStart()} | Set-Content $Filename
    }
    
    function Search-FileAllLocalDisks {
        param (
        [Parameter(Mandatory = $true)] $Filename
        )
    
        $disks = Get-WmiObject -Class Win32_volume -Filter "DriveType = 3" | Where-Object {$_.DriveLetter -cne $null} -ErrorAction SilentlyContinue
        
        $Files = foreach ($disk in $disks.name) {Get-ChildItem "$disk" -File -Recurse -Filter $Filename -ErrorAction SilentlyContinue}
    
        return ,$Files 
    }