Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming Computers with CSV files and Mac Address

Tags:

powershell

I am trying to set up a master CSV file that gets checked when a PowerShell script runs during an MDT task sequence. What the script is trying to do is take the mac address of the physically connected nic card and compare it to the CSV files to see if it matches the Mac address that is saved in the file. If the mac address matches the value in the CSV it renames the computer to value that is paired with the Mac Address. If there is no match rename it based on the computer serial number.

Here is the what I have some far.

$computer = Get-WmiObject Win32_ComputerSystem -ComputerName $oldname
#$Machost is Mac Address of the active Network Card
$MacHost=Get-NetAdapter -Name "Ethernet" | Select Macaddress

$ConvertMacHost= $MacHost[0] -replace '(:|-|\.)'
Write-Host "You mac address is: " $MacHost
#MacHost gets reformatted to take out Semicolons - Ex 0011223344AA


#MacLab is a variable that stores the Computer name and Mac Address from File created in Deep Freeze that matches Mac Address of host.
#MacLab is formated to make all Alpha in Mac Address Capitalized 
$MacLab=Import-Csv 'C:/projectcsv/LH_office.csv' | Select Workstations,@{Label ="Mac Address"; Expression ={$_."Mac Address".ToUpper()}}|where-object {$_."Mac Address" -eq $ConvertMacHost}|Select Workstations

#Checks to see if Host Mac Address Matches CSV File
If ([string]::IsNullorEmpty($MacLab))
{

    Write-Warning  -Message "No Mac Address found in our Records that Match the Host Machine Mac Address: Exit code 11001"  
    write-host "" 
    exit 11001
   
    }Else{
        
$strUser = "******\domainadd"
$strDomain = "******"
$strPassword = ConvertTo-SecureString "*******" -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PsCredential $strUser,
$strPassword

#---- Define Prefix to be used -----#
$strPrefix='LH'
$strEnding='DESK'

#----- Get Serial Number ------#

$SN=(gwmi win32_BIOS).serialnumber

#--If working with VM, the SN length is longer than 15 Char------#
if($SN.length -gt 11) {
    #Get the last 7 Char of Serial
    $StrName=($SN.substring($SN.length - 11)) -replace "-",""
           
            $strComputerName=$strPrefix+"-"+$StrName+"-"+$strEnding
    } else {$strComputerName=$strPrefix+"-"+$SN+"-"+$strEnding

    }

    #------ Rename the Computer using WMI --------#
    #$computer=gwmi win32_computersystem
    #$computer.rename($strComputerName)

    #-----Powershell 5 above use-------#

    rename-computer -NewName $StrComputerName -PassThru -DomainCredential $Credentials

When I run the code I get this error message:

Error Message

I added the computer I was working to the csv and still get the error. I know later I have to add the function where it checks to see if the name is already in AD. but want to get it working first. Please any help you can give would be helpful.

Edit: Here is a picture of the CSV file that shows what information is in there. CSV File

After making the corrections suggested I get these errors when the scripts run.

At \\LH-WDS-MDT\DeploymentShare$\Scripts\CSV_Computer_Rename_LH.ps1:7 char:13
+ $DriveMap = New-PSDrive -Name "M" -Root "\\LH-WDS-MDT\CSV_Files" -PSP ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TaskSequencePSHost  7/13/2020 2:24:53 PM    0 (0x0000)
InvalidOperation: (M:PSDriveInfo) [New-PSDrive], Win32Exception TaskSequencePSHost  7/13/2020 2:24:54 PM    0 (0x0000)
Exception calling "Substring" with "1" argument(s): "StartIndex cannot be less than zero.
Parameter name: startIndex" TaskSequencePSHost  7/13/2020 2:24:55 PM    0 (0x0000)
At \\LH-WDS-MDT\DeploymentShare$\Scripts\CSV_Computer_Rename_LH.ps1:12 char:1
+ $CompNameSerial = ($CompSerialNumber.substring($CompSerialNumber.leng ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TaskSequencePSHost  7/13/2020 2:24:55 PM    0 (0x0000)
NotSpecified: (:) [], MethodInvocationException TaskSequencePSHost  7/13/2020 2:24:55 PM    0 (0x0000)
Skip computer 'ADMINIS-8EMV500' with new name '@{Workstations=LH-TestMo-5040}' because the new name is not valid. The new computer name entered is not properly formatted. Standard names may contain letters (a-z, A-Z), numbers (0-9), and hyphens (-), but no spaces or periods (.). The name may not consist entirely of digits, and may not be longer than 63 characters.  TaskSequencePSHost  7/13/2020 2:25:03 PM    0 (0x0000)
At \\LH-WDS-MDT\DeploymentShare$\Scripts\CSV_Computer_Rename_LH.ps1:29 char:5
+     rename-computer -NewName $NameComputerCSV -PassThru -DomainCreden ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TaskSequencePSHost  7/13/2020 2:25:03 PM    0 (0x0000)
InvalidArgument: (@{Workstations=LH-TestMo-5040}:String) [Rename-Computer], InvalidOperationException   TaskSequencePSHost  7/13/2020 2:25:03 PM    0 (0x0000)
like image 772
Trevor Roney Avatar asked Nov 06 '22 06:11

Trevor Roney


1 Answers

$MacHost is receiving an object with properties, so you need to specify the property you want or you get weird results, which is causing Where-Object to fail.

Specifically, $ConvertMacHost ended up being something like @{MacAddress=9CB6D0959AE5}

This line

$ConvertMacHost= $MacHost[0] -replace '(:|-|\.)'

should be

$ConvertMacHost= $MacHost[0].MacAddress -replace '(:|-|\.)'

like image 146
Jonathon Anderson Avatar answered Nov 15 '22 05:11

Jonathon Anderson