powershell - 使用 CSV 文件和 Mac 地址重命名计算机
问题描述
我正在尝试设置在 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 中。但想让它先工作。请您提供的任何帮助都会有所帮助。
在进行建议的更正后,我在脚本运行时收到这些错误。
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)
解决方案
$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 '(:|-|\.)'
推荐阅读
- rest - 标准 REST JSON API 技术中的 GraphQL 替代方案
- sql-server - SQL Server 使用 OPENXML 反序列化 XML
- python - 使用 pygame 的 Raspery Pi 出错 没有可用的音频设备
- youtube - YouTube 嵌入,参数是否已停止使用?
- nvidia - 为什么HDMI没有输出,现在只有HDMI输出?
- c# - ef6 连接字符串已缓存且无法更改
- python - 用类的完整路径注释
- html - Vuetify CSS 在自定义 CSS 之后加载,如果使用 extractCSS 预渲染服务器不会加载相关链接
- spring - 如何在使用 Spring 表达式语言将服务响应分配给变量之前验证服务响应是否为空数组
- python - 搜索子列表之间的匹配项