我正在尝试让我的unattend.xml文件使新映像的主机名成为BIOS的串行。老实说,我不在乎它是怎么做的或者用什么语言做的。读书我已经走到这一步但我被卡住了。
$Serial = Get-WMIObject -Class "Win32_BIOS" | Select -Expand SerialNumber
Rename-Computer $Serial
我收到此错误
Rename-Computer : Fail to rename computer 'test-B' to 'R9Z1EBP' due to the following exception: Access is denied.
At C:\Users\xiuhtecuhtli\Desktop\rename.ps1:2 char:1
+ Rename-Computer -NewName ($Serial)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (test-B:String) [Rename-Computer], InvalidOperationException
+ FullyQualifiedErrorId : FailToRenameComputer,Microsoft.PowerShell.Commands.RenameComputerCommand
答案 0 :(得分:2)
$Serial
在为其指定值之前不会返回任何内容。您可以执行WMI调用以获取序列号。
$Serial = Get-WMIObject -Class "Win32_BIOS" | Select -Expand SerialNumber
然后你的Rename-Computer cmdlet应该可以正常工作(你不需要在$ Serial周围使用括号,尽管在那里使用它们不会有任何损害)。就此而言,如果要重命名本地计算机,则唯一需要提供的是新名称。所以这应该有效:
Rename-Computer $Serial
答案 1 :(得分:1)
由于您标记了vbscript,因此这是一个未经测试的vbs解决方案。这允许您远程重命名计算机。您可以对其进行修改,并在成像完成后或首次重启时执行。
Const AdminAcc = "Administrator"
Const AdminPwd = "Administrator Password"
Sub RenameComputerBySerial()
RenameWithSerial "." ' Local Computer that runs the script
End Sub
Sub RenameWithSerial(sComputer)
On Error Resume Next
Dim oWMIService, colItems, oItem, sSerial
'--[ Connect to computer ]--
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,authenticationLevel=pktPrivacy}!\\" & sComputer & "\root\cimv2")
'--[ Extract Serial number from BIOS ]--
Set colItems = oWMIService.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
For Each oItem In colItems
sSerial = Trim(oItem.SerialNumber)
Next
Set colItems = Nothing
'--[ Rename Computer if Serial is non zero length ]--
If Len(sSerial) > 0 Then
Set colItems = oWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each oItem In colItems
oItem.Rename sSerial, AdminPwd, AdminAcc
Next
Set colItems = Nothing
'--[ Reboot the computer ]--
Set colItems = oWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each oItem In colItems
Select Case oItem.reboot
Case 0: Wscript.Echo "Computer renamed to """ & sSerial & """ and rebooted"
Case Else: Wscript.Echo "Computer Renamed but not rebooted"
End Select
Next
Set colItems = Nothing
End If
Set oWMIService = Nothing
If Err.Number <> 0 Then
Wscript.Echo "ERR(" & Err.Number & "):" & Err.Description
End If
End Sub
答案 2 :(得分:1)
我喜欢One-Liner(可能是因为我批量使用它......)
powershell rename-computer(gwmi win32_bios).serialnumber
当然需要一个Boot。
答案 3 :(得分:0)
由于原始错误规定序列号已从GWMI查询传递到restart-computer cmdlet,因此我怀疑在没有正确凭据的情况下执行命令。因此,您可以尝试使用已知的本地计算机管理用户/传递添加-localcredential。
$Serial = (gwmi win32_bios).serialnumber
Rename-computer $Serial -localcredential localhost\administrator
系统将提示您输入凭据。如果您希望自动将凭据传递到cmdlet,则还有各种其他脚本示例。