VBScript 使用选项重命名 Windows 计算机 [英] VBScript Renaming Windows Computer with Options

查看:40
本文介绍了VBScript 使用选项重命名 Windows 计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们用一个 VBScript 帮了我很大的忙,所以我打算用另一种方式来帮助我.或许你能帮我抚平皱纹.

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

我的问题如下:

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

我找到了许多不同的解决方案,可以根据我输入的内容或专门用于拉取 SN 来重命名 PC,但不适用于我的特定情况.一如既往,任何帮助将不胜感激.:)

这是我的代码:

'Rename computer by serial # v1.0 2009 年 11 月暗淡的 Bios、BiosSerial、objFSO、objTextFile'Const ForReading = 1, ForWriting = 2, ForAppending = 8'获取属性IDstrInput = UserInput("请输入 BHMS 属性 ID:")函数用户输入( myPrompt )' 该函数提示用户进行一些输入.' 当脚本在 CSCRIPT.EXE 中运行时,使用 StdIn,' 否则使用 VBScript InputBox() 函数.' myPrompt 是用于提示用户输入的文本.' 该函数返回在 StdIn 或 InputBox( ) 中键入的输入.' 由罗伯范德沃德撰写' http://www.robvanderwoude.com' 检查脚本是否在 CSCRIPT.EXE 中运行If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" 然后' 如果是这样,请使用 StdIn 和 StdOutWScript.StdOut.Write myPrompt &""用户输入 = WScript.StdIn.ReadLine别的' 如果不是,则使用 InputBox( )用户输入 = 输入框(我的提示)万一结束函数'获取序列号.对于 GetObject("winmgmts:").InstancesOf ("win32_bios") 中的每个 BiosBiosSerial = Bios.SerialNumber退出下一个strNewSN = BiosSerial' 如果 SN 超过 10 个字符,则截断到最后 10 个字符.如果 Len(strNewSN) <9 然后strNewSN = Right(BiosSerial, 10)strNewPCName = strInput+"-"+strNewSN万一Set WshNetwork = WScript.CreateObject("WScript.Network")strComputer = "."Set objWMIService = GetObject("winmgmts:" _&"{impersonationLevel=impersonate}!\\" &strComputer &"\root\cimv2")设置 colComputers = objWMIService.ExecQuery _(从 Win32_ComputerSystem 中选择 *")对于 colComputers 中的每个 objComputer错误 = objComputer.Rename(strNewPCName)如果错误<>0 那么wscript.echo "重命名 PC 时出错.请重新启动并重试,或手动重命名."别的wscript.echo "PC 成功重命名:" &字符串新PC名称万一下一个

5/29/2013 我根据您的建议进行了一些更改,但在第 36 行字符 1预期语句"代码 800A0400 上出现错误.对我来说看起来不错,所以我错过了什么?这是我的代码的新粘贴,注释了第 36 行.

dim Bios、BiosSerial、objFSO、objTextFile'Const ForReading = 1, ForWriting = 2, ForAppending = 8' 提示输入 PropertyIDstrInput = UserInput("请输入 BHMS 属性 ID:")函数用户输入( myPrompt )' 检查脚本是否在 CSCRIPT.EXE 中运行If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" 然后' 如果是这样,请使用 StdIn 和 StdOutWScript.StdOut.Write myPrompt &""用户输入 = WScript.StdIn.ReadLine别的' 如果不是,则使用 InputBox( )用户输入 = 输入框(我的提示)万一结束函数' 获取序列号.对于 GetObject("winmgmts:").InstancesOf ("win32_bios") 中的每个 BiosBiosSerial = Bios.SerialNumber退出下一个strNewSN = BiosSerial如果 IsEmpty(BiosSerial) 那么strNewSN = UserInput("BIOS 中没有列出序列号.提供一个替代方案:")别的strNewSN = BiosSerial万一' 如果 SN 超过 10 个字符,则截断到最后 10 个字符.如果 Len(strNewSN) >10 然后 strNewSN = Right(BiosSerial, 10)strNewPCName = strInput &"-" &字符串NewSN如果'LINE36'结束Set WshNetwork = WScript.CreateObject("WScript.Network")strComputer = "."Set objWMIService = GetObject("winmgmts:" _&"{impersonationLevel=impersonate}!\\" &strComputer &"\root\cimv2")设置 colComputers = objWMIService.ExecQuery _(从 Win32_ComputerSystem 中选择 *")对于 colComputers 中的每个 objComputer错误 = objComputer.Rename(strNewPCName)出错时<>0 那么wscript.echo "重命名 PC 时出错.请重新启动并重试,或手动重命名."别的wscript.echo "PC 成功重命名:" &字符串新PC名称万一下一个

解决方案

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

' 如果 SN 超过 10 个字符,则截断到最后 10 个字符.如果 Len(strNewSN) <9 然后strNewSN = Right(BiosSerial, 10)strNewPCName = strInput+"-"+strNewSN万一

您的评论说,如果序列号长于该序列号,您希望使用序列号的最后 10 个字符,但只有当序列号短于 9 时,您的代码才使用最后 10 个字符强>字符.把它改成

If Len(strNewSN) >10 然后 strNewSN = Right(BiosSerial, 10)strNewPCName = strInput &"-" &字符串NewSN

<块引用>

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

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

If IsEmpty(BiosSerial) ThenstrNewSN = UserInput("请输入假序列号:")别的strNewSN = BiosSerial万一

<块引用>

如果失败,我的错误输出将不起作用.我不知道为什么.

定义不起作用".您得到了什么结果,与您期望的结果有何不同?

顺便说一句,您不应该使用 err 作为变量的名称.Err 是 VBScript 在处理终止错误的上下文.

<小时>

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

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

删除该行,错误就会消失.

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

  • 关闭End If:

    If 条件 Then操作说明万一

    使用这种形式时,instruction 不能Then关键字在同一行.

  • 不关闭End If:

    If 条件 Then 指令

    使用这种形式时,instruction 必须Then关键字在同一行,不得后跟 End If.

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

如果 Len(strNewSN) > 10 那么 strNewSN = Right(BiosSerial, 10)strNewPCName = strInput & "-" & strNewSN

相当于:

If Len(strNewSN) >10 那么strNewSN = Right(BiosSerial, 10)万一strNewPCName = strInput &"-" &字符串NewSN

You guys did great helping me with one VBScript, so I'm going to throw another your way. Perhaps you can help me iron out the wrinkles.

My intention with this script is to rename the PC. The computer name is a combination of prompted text (something I provide, say a location code), a hyphen, and the last 10 digits of the computer's serial number called from WMIC. i.e. 1275-XXXXXXXXXX

My problems here are the following:

  1. If my code is over 10 characters, it just errors out. I want to fix that. I'm sure it's just the way I have it coded and nothing to do with pulling from WMIC.
  2. If it does error out pulling the serial number from WMIC, say because there's no value, I want to be prompted to enter something in it's place. Then at the end it will take Input1 (the location code) and Input2 (what I provide if the SN pull fails), smack a hyphen in the middle, and apply it.
  3. My error out isn't working if it does fail. I don't know why.

I've found so many different solutions for renaming PC's either what I type in or specifically for pulling the SN, but not for my specific situation. Any help would be GREATLY appreciated, as always. :)

Here's my code:

'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

5/29/2013 EDIT: I've made some changes based on your suggestion, and I'm getting on error on line 36 char 1 "Expected Statement" Code 800A0400. It looks fine to me so what am I missing? Here's a new paste of my code with line 36 notated.

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

解决方案

If my code is over 10 characters, it just errors out. I want to fix that. I'm sure it's just the way I have it coded and nothing to do with pulling from 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

Your comment says that you want to use the last 10 characters from the serial number if the serial number is longer than that, but your code takes the last 10 characters only if the serial number is shorter than 9 characters. Change that into

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

If it does error out pulling the serial number from WMIC, say because there's no value, I want to be prompted to enter something in it's place. Then at the end it will take Input1 (the location code) and Input2 (what I provide if the SN pull fails), smack a hyphen in the middle, and apply it.

You could use IsEmpty() to check if the BiosSerial variable has a value:

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

My error out isn't working if it does fail. I don't know why.

Define "isn't working". What result do you get, and how is it different from the result you expect?

BTW, you shouldn't use err as a name for a variable. Err is an intrinsic object that VBScript provides in the context of handling terminating errors.


Edit: You have a spurious End If in line 36:

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

Remove that line and the error will disappear.

In VBSCript an If statement can have two forms:

  • With closing End If:

    If condition Then
      instruction
    End If
    

    When using this form, instruction must not be on the same line as the Then keyword.

  • Without closing End If:

    If condition Then instruction
    

    When using this form, instruction must be on the same line as the Then keyword and must not be followed by an End If.

In line 34 of your code you truncate the serial number to 10 characters if it's longer than that, and then execute the next line regardless of whether the serial number had to be truncated or not (that line must be executed unconditionally, so I removed it from the Then branch):

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

which is equivalent to this:

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

这篇关于VBScript 使用选项重命名 Windows 计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆