如何在.bat中运行多个powershell命令
我制作了一个用于将计算机加入域的批处理文件。
Powershell -command " Add-Computer -DomainName blabla.com -Credential blabla\bla "
有效,但我还想添加 -Description 和输入。
Powershell -command "
$date = Get-Date
$cred= read-host -prompt 'IT username'
Add-Computer -DomainName blabla.com -Description "joined domain by $cred date $date" -Credential blabla\$cred "
没有用。
Powershell -command " $date = Get-Date "
Powershell -command " $cred= read-host -prompt 'IT username' "
Powershell -command " Add-Computer -DomainName blabla.com -Description "joined domain by $cred date $date" -Credential blabla\$cred "
也没用。
请我需要帮助
I made a batch file for joining computers to domain.
Powershell -command " Add-Computer -DomainName blabla.com -Credential blabla\bla "
works but I also want to add -Description with input.
Powershell -command "
$date = Get-Date
$cred= read-host -prompt 'IT username'
Add-Computer -DomainName blabla.com -Description "joined domain by $cred date $date" -Credential blabla\$cred "
did not work.
Powershell -command " $date = Get-Date "
Powershell -command " $cred= read-host -prompt 'IT username' "
Powershell -command " Add-Computer -DomainName blabla.com -Description "joined domain by $cred date $date" -Credential blabla\$cred "
didnt work either.
Please I need help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这需要通过一次调用
powershell.exe
来完成。使用分号终止每个 PowerShell 语句,并使用cmd
行继续符 CARET^
。这需要是一次调用的原因是 PowerShell 的单独调用不知道彼此做了什么。
这个没有经过测试。可能存在一些
cmd
语法问题,通常与引用有关。让 .bat 文件脚本调用 PowerShell 脚本会更容易。请注意,
Do
不是经过批准的 PowerShell 动词。使用命令Get-Verb
找到您认为合适的。This needs to be done in a single invocation of
powershell.exe
. Terminate each PowerShell statement with a SEMICOLON and use thecmd
line continuation character, CARET^
.The reason this needs to be one invocation is that separate invocations of PowerShell do not know what each other has done.
This is not tested. There may be some
cmd
syntax issues, typically about quoting. It would be easier to have the .bat file script call a PowerShell script.Note that
Do
is not an approved PowerShell verb. Use the commandGet-Verb
to find one you think is suitable.感谢@lit 和@Compo 这是最终结果。
thanks for @lit and @Compo here is the final result.