标题: [问题求助] 【已解决】如何使用powershell实现一键改名加域? [打印本页]
作者: kingshui 时间: 2016-8-22 12:36 标题: 【已解决】如何使用powershell实现一键改名加域?
本帖最后由 kingshui 于 2016-8-22 16:36 编辑
RT,
想实现一键改名加域的操作,但是用powershell ps1的话运行方式有点接受不了,能否用bat的方式来进行呢?- $domain = "chenker"
- $password = "asd=1234" | ConvertTo-SecureString -asPlainText -Force
- $username = "chenker\sszxw"
- $credential = New-Object System.Management.Automation.PSCredential($username,$password)
- Add-Computer -DomainName $domain -computername $env:hostname -NewName $env:newhostname -Credential $credential
- Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Restricted -Force
复制代码
其实是想把上面的代码写成一行,但是不懂怎么用。。。
类似powershell -command ******这样。
所以想向各位有经验的朋友请教。谢谢
下面这个方式需要在改名后重启才能用新计算机名加域,不是我想要的。- @echo off
- REM _______________________________________________________________________________
- >nul 2>&1 "%systemroot%\system32\cacls.exe" "%systemroot%\system32\config\system"
- if '%errorlevel%' NEQ '0' (
- goto UACPrompt
- ) else (goto gotAdmin)
- :UACPrompt
- echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
- echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
- "%temp%\getadmin.vbs"
- exit /B
- :gotAdmin
- if exist "%temp%\getadmin.vbs" (del "%temp%\getadmin.vbs")
- pushd "%CD%"
- cd /d "%~dp0"
- REM _______________________________________________________________________________
- set /p pcname=Please enter new pcname:
- WMIC computersystem where nmae="%computername%" call rename name=%pcname%
- ping -n 5 127.0.1 >nul
- WMIC computersystem where name="%computername%" call joindomainorworkgroup accountOU="OU=gsr;dc=chenker" fjoinoptions=3 name="chenker" username="chenker\sszxw" password="asd=1234"|find "2224" && WMIC computersystem where name="%computername%" call joindomainorworkgroup accountOU="OU=gsr;dc=chenker" fjoinoptions=1 name="chenker" username="chenker\sszxw“ password="asd=1234"
- pause
复制代码
作者: GNU 时间: 2016-8-22 15:03
test.bat- @echo off
- for /f "delims=:" %%i in ('findstr /n "^:JoinDomain$" "%~f0"') do (
- more +%%i "%~f0" > "%temp%\new.ps1"
- )
- powershell -f "%temp%\new.ps1"
- goto :eof
-
- :JoinDomain
- $domain = "chenker"
- $password = "asd=1234" | ConvertTo-SecureString -asPlainText -Force
- $username = "chenker\sszxw"
- $credential = New-Object System.Management.Automation.PSCredential($username,$password)
- Add-Computer -DomainName $domain -computername $env:hostname -NewName $env:newhostname -Credential $credential
- Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Restricted -Force
复制代码
作者: pcl_test 时间: 2016-8-22 15:50
bat和powershell混合编程
http://www.bathome.net/viewthread.php?tid=34884
作者: kingshui 时间: 2016-8-22 16:36
多谢楼上两位,采用了GNU的方式,
@pcl_test,发贴前有search过,用过混合编程的方法,但是这个方法不适合有交互的PS脚本使用,比如我需要输入东西的时候就用不了。
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |