简体   繁体   English

使用批处理根据当前计算机名更改计算机名和用户名?

[英]Using Batch to change Computer Name and UserName based off of their current Computer Name?

I'm writing a batch that I'll remotely place in the all users startup folder using Desktop Central 9 /PsExec. 我正在编写一个批处理文件,它将使用Desktop Central 9 / PsExec远程放置在所有用户启动文件夹中。 We are going to start using a naming convention as part of an initiative to overhaul computer management. 我们将开始使用命名约定,作为全面改革计算机管理计划的一部分。 This batch needs to find what the current PC Name is and then change it based off of what I will have manually defined in the batch itself. 该批次需要找到当前的PC名称,然后根据我将在该批次本身中手动定义的名称对其进行更改。 It would also be nice to be able to then also change the users name based off of what computer name is initially found. 然后,能够根据最初找到的计算机名称来更改用户名,这也很好。 All computers use Windows 7 and above. 所有计算机都使用Windows 7及更高版本。 I wrote a quick batch of what I think it should look like, minus the user name modification, but there are nuances here and there I must be getting wrong. 我写了我认为应该简短的批处理,减去了用户名的修改,但是这里有些细微的差别,我肯定错了。 The "If" statement is my weakness with batch, but I know it will be extremely efficient in this case. “ If”语句是我对批处理的弱点,但我知道在这种情况下它将非常有效。 Can anyone correct what I'm doing wrong here? 有人可以纠正我在这里做错的事情吗? Much appreciated, this will help us A LOT! 非常感谢,这将帮助我们很多!

SET WMIC GET COMPUTERNAME %%CURRENT% 设置WMIC GET COMPUTERNAME %% CURRENT%

If %%CURRENT% = THOMAS-PC then REG ADD HKLM\\SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName /v ComputerName /t REG_SZ /d TGZ-DELL-001 /f 如果%% CURRENT%= THOMAS-PC,则添加REG HKLM \\ SYSTEM \\ CurrentControlSet \\ Control \\ ComputerName \\ ComputerName / v ComputerName / t REG_SZ / d TGZ-DELL-001 / f

If %%CURRENT% = CHARLES-PC then REG ADD HKLM\\SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName /v ComputerName /t REG_SZ /d TGZ-DELL-002 /f 如果%% CURRENT%= CHARLES-PC,则添加REG HKLM \\ SYSTEM \\ CurrentControlSet \\ Control \\ ComputerName \\ ComputerName / v ComputerName / t REG_SZ / d TGZ-DELL-002 / f

If %%CURRENT% = MATHEW-PC then REG ADD HKLM\\SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName /v ComputerName /t REG_SZ /d TGZ-DELL-003 /f 如果%% CURRENT%= MATHEW-PC,则添加REG HKLM \\ SYSTEM \\ CurrentControlSet \\ Control \\ ComputerName \\ ComputerName / v ComputerName / t REG_SZ / d TGZ-DELL-003 / f

  1. %%CURRENT% is incorrect. %% CURRENT%不正确。 Use %CURRENT% 使用%CURRENT%
  2. IF statements do not have a "then". IF语句没有“ then”。 Use parens if you have multiple lines in IF statement. 如果IF语句中有多行,请使用parens。
  3. Use = to SET and == to compare 使用=设置并使用==进行比较
  4. Use quotes as shown in my example so that the spaces, etc don't cause problems 如我的示例所示,使用引号使空格等不会引起问题
  5. No need for WMIC in this case. 在这种情况下,无需WMIC。

How about something like this? 这样的事情怎么样? I added /I to make the compare case insensitive. 我添加了/ I使比较大小写不敏感。 Leave that out if you wish... but then your compare would miss Thomas-PC, etc. 如果您愿意的话,可以省去它...但是那样您的比较就会错过Thomas-PC等。

**** CAUTION **** Untested! ****注意****未经测试! Of course you will test test on a test machine before messing up the registry on a working machine :) 当然,您需要先在测试计算机上测试测试,然后再在工作计算机上破坏注册表:)

@echo off
set "RegCmd=REG ADD HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d"

echo(this computer is %ComputerName%
if /I "%ComputerName%"=="THOMAS-PC" %RegCmd% TGZ-DELL-001 /f
if /I "%ComputerName%"=="CHARLES-PC" %RegCmd% TGZ-DELL-002 /f
if /I "%ComputerName%"=="MATHEW-PC" %RegCmd% TGZ-DELL-003 /f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM