【问题标题】:VBScript Renaming Windows Computer with Options带有选项的 VBScript 重命名 Windows 计算机
【发布时间】:2013-05-20 10:40:25
【问题描述】:

你们在一个 VBScript 方面帮了我很大的忙,所以我打算再按你的方式扔另一个。或许你可以帮我抚平皱纹。

我使用此脚本的目的是重命名 PC。计算机名称是提示文本(我提供的,比如位置代码)、连字符和从 WMIC 调用的计算机序列号的最后 10 位数字的组合。即 1275-XXXXXXXXXX

我的问题如下:

  1. 如果我的代码超过 10 个字符,就会出错。我想修复 那。我确定这只是我编码的方式,无事可做 从 WMIC 中提取。
  2. 如果从 WMIC 中提取序列号时出错,比如因为没有值,我希望系统提示我在其中输入一些内容。然后在最后它将使用 Input1(位置代码)和 Input2(如果 SN 拉取失败,我会提供),在中间打一个连字符,然后应用它。
  3. 如果确实失败,我的错误输出将无法正常工作。我不知道为什么。

我找到了很多不同的解决方案来重命名 PC,无论是我输入的内容还是专门用于提取 SN,但不适用于我的具体情况。一如既往,我们将不胜感激任何帮助。 :)

这是我的代码:

'Rename computer by serial # v1.0 November 2009
dim Bios, BiosSerial, objFSO, objTextFile
'Const ForReading = 1, ForWriting = 2, ForAppending = 8

'get PropertyID
strInput = UserInput( "Enter the BHMS Property ID:" )

Function UserInput( myPrompt )
' This function prompts the user for some input.
' When the script runs in CSCRIPT.EXE, StdIn is used,
' otherwise the VBScript InputBox( ) function is used.
' myPrompt is the the text used to prompt the user for input.
' The function returns the input typed either on StdIn or in InputBox( ).
' Written by Rob van der Woude
' http://www.robvanderwoude.com
' Check if the script runs in CSCRIPT.EXE
If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
    ' If so, use StdIn and StdOut
    WScript.StdOut.Write myPrompt & " "
    UserInput = WScript.StdIn.ReadLine
Else
    ' If not, use InputBox( )
    UserInput = InputBox( myPrompt )
End If
End Function

'Obtain Serial Number.
for each Bios in GetObject("winmgmts:").InstancesOf ("win32_bios")
BiosSerial = Bios.SerialNumber
exit for
next

strNewSN = BiosSerial

' If the SN is longer than 10 characters, truncate to the last 10.
If Len(strNewSN) < 9 Then
    strNewSN = Right(BiosSerial, 10)
    strNewPCName = strInput+"-"+strNewSN
End If

Set WshNetwork = WScript.CreateObject("WScript.Network")

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
err = objComputer.Rename(strNewPCName)

if err <> 0 then
wscript.echo "There was an error renaming the PC. Please restart and try again, or rename it manually."
else
wscript.echo "PC successfully renamed: " & strNewPCName
end if

Next

2013 年 5 月 29 日编辑:我已根据您的建议进行了一些更改,并且在第 36 行字符 1“预期语句”代码 800A0400 上出现错误。对我来说看起来不错,所以我错过了什么?这是我的代码的新粘贴,第 36 行注明。

dim Bios, BiosSerial, objFSO, objTextFile
'Const ForReading = 1, ForWriting = 2, ForAppending = 8

' Prompt for PropertyID
strInput = UserInput( "Enter the BHMS Property ID:" )

Function UserInput( myPrompt )
    ' Check if the script runs in CSCRIPT.EXE
    If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
        ' If so, use StdIn and StdOut
        WScript.StdOut.Write myPrompt & " "
        UserInput = WScript.StdIn.ReadLine
    Else
        ' If not, use InputBox( )
        UserInput = InputBox( myPrompt )
    End If
End Function

' Obtain Serial Number.
for each Bios in GetObject("winmgmts:").InstancesOf ("win32_bios")
BiosSerial = Bios.SerialNumber
exit for
next

strNewSN = BiosSerial

If IsEmpty(BiosSerial) Then
  strNewSN = UserInput("There is no serial number listed in the BIOS. Provide an alternative: ")
Else
  strNewSN = BiosSerial
End If

' If the SN is longer than 10 characters, truncate to the last 10.
If Len(strNewSN) > 10 Then strNewSN = Right(BiosSerial, 10)
strNewPCName = strInput & "-" & strNewSN
End If                                                           'LINE36'

Set WshNetwork = WScript.CreateObject("WScript.Network")

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
err = objComputer.Rename(strNewPCName)

On Error <> 0 Then
wscript.echo "There was an error renaming the PC. Please restart and try again, or rename it manually."
Else
wscript.echo "PC successfully renamed: " & strNewPCName
end if

    Next

【问题讨论】:

    标签: windows vbscript rename


    【解决方案1】:

    如果我的代码超过 10 个字符,就会出错。我想解决这个问题。我确定这只是我编码的方式,与从 WMIC 中提取无关。

    ' If the SN is longer than 10 characters, truncate to the last 10.
    If Len(strNewSN) < 9 Then
      strNewSN = Right(BiosSerial, 10)
      strNewPCName = strInput+"-"+strNewSN
    End If
    

    您的评论说,如果序列号长于序列号,您想使用序列号的最后 10 个字符,但您的代码仅在序列号短于 9 时才使用最后 10 个字符强>字符。把它改成

    If Len(strNewSN) > 10 Then strNewSN = Right(BiosSerial, 10)
    strNewPCName = strInput & "-" & strNewSN
    

    如果从 WMIC 中提取序列号时出错,说因为没有价值,我想被提示在它的位置输入一些东西。然后在最后它将使用 Input1(位置代码)和 Input2(如果 SN 拉取失败,我提供的内容),在中间打一个连字符,然后应用它。

    您可以使用IsEmpty() 来检查BiosSerial 变量是否有值:

    If IsEmpty(BiosSerial) Then
      strNewSN = UserInput("Enter fake serial number:")
    Else
      strNewSN = BiosSerial
    End If
    

    如果失败,我的错误输出将无法正常工作。我不知道为什么。

    定义“不工作”。您得到什么结果,与您预期的结果有何不同?

    顺便说一句,您不应该使用err 作为变量的名称。 Err 是 VBScript 在handling terminating errors 的上下文中提供的内在对象。


    编辑:您在第 36 行有一个虚假的 End If

    If Len(strNewSN) > 10 Then strNewSN = Right(BiosSerial, 10)
    strNewPCName = strInput & "-" & strNewSN
    End If

    删除该行,错误将消失。

    在 VBSCript 中,If 语句可以有两种形式:

    • 关闭End If:

      If condition Then
        instruction
      End If
      

      使用此表单时,instruction 不得Then 关键字位于同一行。

    • 不关闭End If:

      If condition Then instruction
      

      使用此表单时,instruction 必须Then 关键字位于同一行,并且不得后跟 End If

    在代码的第 34 行中,如果序列号长于 10 个字符,则将其截断为 10 个字符,然后执行下一行,无论是否必须截断序列号(该行必须无条件执行,所以我从Then 分支中删除了它):

    If Len(strNewSN) > 10 Then strNewSN = Right(BiosSerial, 10)
    strNewPCName = strInput & "-" & strNewSN

    相当于这个:

    If Len(strNewSN) > 10 Then
      strNewSN = Right(BiosSerial, 10)
    End If
    strNewPCName = strInput & "-" & strNewSN
    

    【讨论】:

    • 我在我的编辑中发布了一个新的 sn-p 代码。你能看看我做错了什么吗?非常感谢您的帮助和洞察力! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2021-08-13
    • 2014-03-07
    • 2017-06-12
    • 1970-01-01
    相关资源
    最近更新 更多