Powershell 使用CSV文件和Mac地址重命名计算机
我正在尝试设置一个主CSV文件,当PowerShell脚本在MDT任务序列期间运行时,该文件将被检查。脚本试图做的是获取物理连接的nic卡的mac地址,并将其与CSV文件进行比较,以查看它是否与文件中保存的mac地址匹配。如果mac地址与CSV中的值匹配,则会将计算机重命名为与mac地址配对的值。如果没有匹配项,请根据计算机序列号重命名 这是我目前所拥有的Powershell 使用CSV文件和Mac地址重命名计算机,powershell,Powershell,我正在尝试设置一个主CSV文件,当PowerShell脚本在MDT任务序列期间运行时,该文件将被检查。脚本试图做的是获取物理连接的nic卡的mac地址,并将其与CSV文件进行比较,以查看它是否与文件中保存的mac地址匹配。如果mac地址与CSV中的值匹配,则会将计算机重命名为与mac地址配对的值。如果没有匹配项,请根据计算机序列号重命名 这是我目前所拥有的 $computer = Get-WmiObject Win32_ComputerSystem -ComputerName $oldname
$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)
$MacHost
正在接收具有属性的对象,因此您需要指定所需的属性,否则会得到奇怪的结果,这将导致Where object
失败
具体来说,$ConvertMacHost
最终类似于@{MacAddress=9CB6D0959AE5}
这条线
$ConvertMacHost=$MacHost[0]-替换“(:|-|\”
应该是
$ConvertMacHost=$MacHost[0]。MacAddress-replace'(:|-| \.)
尝试这样设置$ConvertMacHost$ConvertMacHost=$MacHost[0]。MacAddress-replace'(:|-| \.)
更好吗?检查if语句之前的MacLab
的值,查看它是否确实为null或空。。。MacLab
中的select子句似乎很长,看看没有它会发生什么。CSV的内容是什么?添加了CSV文件的图片。不知道如何上传以便下载。这些错误看起来与您发布的代码无关。。。显示错误的带下划线部分与代码中的任何内容都不对应。当脚本运行时MDT给出的输出日志出现错误时。所以我不确定是否正确。在对脚本进行更正后,当脚本运行时,我会得到这些错误。有人有什么想法吗。