Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Entry from the 2013 Scripting Games Advanced Event 6, reviewed at
function  Join-Computer {
            Adds server core machines to the domain from MAC address.
            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
            PS C:\> $domain = Get-Credential
            PS C:\> $local = Get-Credential
            PS C:\> type C:\macs.txt | Join-Computer -DomainCredential $domain -LocalCredential $local
            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
            Written by DC on 6/2/2013 for the 2013 scripting games. Final event :)
#Requires -Version 3
#Requires -Modules DHCPServer  
        [Parameter(Position=0, Mandatory=$true)]
        [Parameter(Position=1, Mandatory=$true)]
        [ValidateScript({if($_ -match '^([0-9A-F]{2}[-]){5}([0-9A-F]{2})$' -or $_ -match '^[0-9A-F]{12}$' )
                        Else{{throw "$_ is not a MAC address. Valid Formats are XX-XX-XX-XX-XX-XX or XXXXXXXXXXXX"}}})]
        $DHCPServer = 'DHCP1',
        $StartingNumber = 1
    begin {
        Workflow Join-WFComputer{
                [Parameter(Position=0, Mandatory=$true)]
            #Ran into an issue, Firewall was blocking the reboot computer command
            Write-Verbose -Message "Disabling firewall or else unable to restart-computer"
                Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled False
            Rename-Computer -NewName $NewName -Force
            Restart-Computer -Wait -Force
                Add-Computer -Domain dclab.local -Credential $DomainCred -OUPath $OU
                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
                Write-Verbose -Message "Machines will be added to $OU"
                $processOU = $true
            }#end if ADSI
                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"
                $ip = Get-DhcpServerv4Scope -ComputerName $DHCPServer -ErrorAction 'Stop' |
                    Get-DhcpServerv4Lease -ComputerName $DHCPServer -ClientId $mac -ErrorAction 'Stop' |
                    Select-Object -ExpandProperty IPAddress
                $ip = $ip.IPAddressToString
            }#end Try
                $process = $false
                Write-Warning -Message "Unable to get IP info for $mac"
            }#end Catch
                    Test-WSMan -ComputerName $ip -Authentication Default -Credential $LocalCredential -ErrorAction 'Stop' | Out-Null
                }#end Try
                    Write-Warning -Message "Unable to connect over WSMAN to $mac on $ip"
                    $process = $false
                }#end Catch
            }#end if process
                Write-Verbose -Message "$Mac information found. Building hashtable"
                $temp = @{
                        PSComputerName= $ip
                        DomainCred = $DomainCredential
                        NewName = ("{0}{1}" -f $NameBase,$StartingNumber)
                    $temp['OU'] = $OU
                    $temp['Verbose'] = $true
                $params[$count] += $temp
            }#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