Skip to content

Instantly share code, notes, and snippets.

@janegilring
Created June 8, 2013 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janegilring/5736499 to your computer and use it in GitHub Desktop.
Save janegilring/5736499 to your computer and use it in GitHub Desktop.
Entry from the 2013 Scripting Games Advanced Event 6, reviewed at blog.powershell.no
function  Join-Computer {
 
    <#
        .SYNOPSIS
            Adds server core machines to the domain from MAC address.
 
        .DESCRIPTION
            Takes a list of mac address and queries DHCP server for IP address.
            Machines are then renamed and joined to the domain.
 
        .PARAMETER  DomainCredential
            Credential to join domain
 
        .PARAMETER  LocalCredential
            Local credential to connect to the machines
     
        .PARAMETER  NameBase
            Machines will be named with this and an incremeting integer. Ex Server,Web,IIS.
            Default = 'Server'
         
        .PARAMETER  MACAddress
            MACAddress of machine to add. Accepts pipeline input.
     
        .PARAMETER  OU
            Option OU joined machines will be placed in.
     
        .PARAMETER  DHCPServer
            DHCP server to query for IP address. Default = DHCP1
         
        .PARAMETER  StartingNumber
            Number used to increment added machines. Default = 1
     
        .EXAMPLE
            PS C:\> $domain = Get-Credential
            PS C:\> $local = Get-Credential
            PS C:\> type C:\macs.txt | Join-Computer -DomainCredential $domain -LocalCredential $local
             
        .EXAMPLE
            PS C:\> $domain = Get-Credential
            PS C:\> $local = Get-Credential
            PS C:\> type C:\macs.txt | Join-Computer -DomainCredential $domain -LocalCredential $local -NameBase Web
                    -OU 'OU=WebServers,DC=mycomp,DC=local' -StartingNumber 40 -DHCPServer ADDHCP2
 
        .INPUTS
            System.String[]
 
        .OUTPUTS
            None
 
        .NOTES
            Written by DC on 6/2/2013 for the 2013 scripting games. Final event :)
 
        .LINK
            http://scriptinggames.org/019a63360a2f27f09f45.pdf
 
    #>
     
#Requires -Version 3
#Requires -Modules DHCPServer  
 
    [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true)]
        [System.Management.Automation.PSCredential]
        $DomainCredential,
        [Parameter(Position=1, Mandatory=$true)]
        [System.Management.Automation.PSCredential]
        $LocalCredential,
        [Parameter(Position=2)]
        [System.String]
        $NameBase='Server',
        [Parameter(Position=3,
                   Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [System.String[]]
        [ValidateScript({if($_ -match '^([0-9A-F]{2}[-]){5}([0-9A-F]{2})$' -or $_ -match '^[0-9A-F]{12}$' )
                        {$true}
                        Else{{throw "$_ is not a MAC address. Valid Formats are XX-XX-XX-XX-XX-XX or XXXXXXXXXXXX"}}})]
        $MACAddress,
        [Parameter(Position=4)]
        [System.String]
        $OU,
        [Parameter(Position=5)]
        [System.String]
        $DHCPServer = 'DHCP1',
        [Parameter(Position=6)]
        [System.Int32]
        $StartingNumber = 1
         
    )
    begin {
        Workflow Join-WFComputer{
            [CmdletBinding()]
            param(
                [Parameter(Position=0, Mandatory=$true)]
                [System.Management.Automation.PSCredential]
                $DomainCred,
                [Parameter(Position=1,Mandatory=$true)]
                [System.String]
                $NewName,
                [Parameter(Position=2)]
                [System.String]
                $OU
            )
            #Ran into an issue, Firewall was blocking the reboot computer command
            Write-Verbose -Message "Disabling firewall or else unable to restart-computer"
            InlineScript{
                Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled False
            }
            Rename-Computer -NewName $NewName -Force
            Restart-Computer -Wait -Force
             
            if($OU){
                Add-Computer -Domain dclab.local -Credential $DomainCred -OUPath $OU
            }
            else{
                Add-Computer -Domain dclab.local -Credential $DomainCred
            }          
             
            Write-Verbose -Message "All done configuring $PSComputerName"
            Restart-Computer -force
        }#end Join-WFComputer
         
        #We will use these later for our workFlow Parameters
        $params = New-Object -TypeName HashTable($MACAddress.Count)
        $count = 0
         
         
        $processOU = $false
        if($OU){
            if([ADSI]::Exists("LDAP://$ou")){
                Write-Verbose -Message "Machines will be added to $OU"
                $processOU = $true
            }#end if ADSI
            Else{
                Write-Warning -Message "$OU does not exists. Machines will be added to default location"
                $processOU = $false
            }#end else
        }
         
             
             
    }#End Begin
    process {
         
        foreach ($mac in $MACAddress) {
            $process = $true
            Write-Verbose -Message "Getting Information on $Mac"
            Try{
                $ip = Get-DhcpServerv4Scope -ComputerName $DHCPServer -ErrorAction 'Stop' |
                    Get-DhcpServerv4Lease -ComputerName $DHCPServer -ClientId $mac -ErrorAction 'Stop' |
                    Select-Object -ExpandProperty IPAddress
                $ip = $ip.IPAddressToString
            }#end Try
            Catch{
                $process = $false
                Write-Warning -Message "Unable to get IP info for $mac"
            }#end Catch
             
            if($process){
                Try{
                    Test-WSMan -ComputerName $ip -Authentication Default -Credential $LocalCredential -ErrorAction 'Stop' | Out-Null
                }#end Try
                Catch{
                    Write-Warning -Message "Unable to connect over WSMAN to $mac on $ip"
                    $process = $false
                }#end Catch
            }#end if process
             
            if($process){
                Write-Verbose -Message "$Mac information found. Building hashtable"
                $temp = @{
                        PSComputerName= $ip
                        PSCredential=$LocalCredential
                        DomainCred = $DomainCredential
                        NewName = ("{0}{1}" -f $NameBase,$StartingNumber)
                    }
                if($processOU){
                    $temp['OU'] = $OU
                }
                if($PSBoundParameters.ContainsKey('Verbose')){
                    $temp['Verbose'] = $true
                }
                 
                $params[$count] += $temp
                $count++
                $StartingNumber++
            }#end if process
             
        }#end Foreach Mac
    }
    end {
        if($count -gt 0){
            Join-WFComputer -PSParameterCollection $params.Values
        }
    }#end end :)
}#end function  Join-Computer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment