【问题标题】:Renaming Computers with CSV files and Mac Address使用 CSV 文件和 Mac 地址重命名计算机
【发布时间】:2020-07-14 04:04:24
【问题描述】:

我正在尝试设置在 MDT 任务序列期间运行 PowerShell 脚本时检查的主 CSV 文件。该脚本试图做的是获取物理连接的网卡的 MAC 地址,并将其与 CSV 文件进行比较,以查看它是否与文件中保存的 Mac 地址匹配。如果 mac 地址与 CSV 中的值匹配,则会将计算机重命名为与 Mac 地址配对的值。如果没有匹配则根据计算机序列号重命名。

这里是我的一些东西。

$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

当我运行代码时,我收到以下错误消息:

我将正在使用的计算机添加到 csv 中,但仍然出现错误。我知道以后我必须添加函数来检查名称是否已经在 AD 中。但想让它先工作。请您提供任何帮助。

编辑:这是 CSV 文件的图片,显示了其中的信息。

在进行建议的更正后,脚本运行时出现这些错误。

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)

【问题讨论】:

  • 尝试像这样$ConvertMacHost= $MacHost[0].MacAddress -replace '(:|-|\.)' 设置$ConvertMacHost,更好吗?
  • 在 if 语句之前检查 MacLab 的值,看看它是否确实为 null 或空...MacLab 中的 select 子句似乎很长,看看没有它会发生什么。 CSV 的内容是什么?
  • 添加了 CSV 文件的图片。不知道怎么上传,所以可以下载。
  • 这些错误看起来与您发布的代码无关...显示错误的下划线部分与您的代码中的任何内容都不对应。
  • 脚本运行时 MDT 给出的输出日志的错误。所以我不确定。

标签: powershell


【解决方案1】:

$MacHost 正在接收具有属性的对象,因此您需要指定所需的属性,否则会得到奇怪的结果,这会导致 Where-Object 失败。

具体来说,$ConvertMacHost 最终变成了类似于@{MacAddress=9CB6D0959AE5}

这一行

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

应该是

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

【讨论】:

  • 在对脚本进行更正后,我在运行时收到这些错误。有没有人有什么想法。
猜你喜欢
  • 1970-01-01
  • 2011-06-14
  • 2011-09-04
  • 2021-01-20
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
相关资源
最近更新 更多