如何在.bat中运行多个powershell命令

发布于 2025-01-10 19:37:11 字数 691 浏览 0 评论 0原文

我制作了一个用于将计算机加入域的批处理文件。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一刻暧昧 2025-01-17 19:37:11

这需要通过一次调用 powershell.exe 来完成。使用分号终止每个 PowerShell 语句,并使用 cmd 行继续符 CARET ^

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 的单独调用不知道彼此做了什么。

这个没有经过测试。可能存在一些 cmd 语法问题,通常与引用有关。让 .bat 文件脚本调用 PowerShell 脚本会更容易。

powershell -NoLogo -NoProfile -File "%~dpDo-JoinDomain.ps1"

请注意,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 the cmd line continuation character, CARET ^.

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;

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.

powershell -NoLogo -NoProfile -File "%~dpDo-JoinDomain.ps1"

Note that Do is not an approved PowerShell verb. Use the command Get-Verb to find one you think is suitable.

浪漫人生路 2025-01-17 19:37:11
@echo off
Powershell -command " $date = Get-Date; $cred= read-host -prompt 'IT username'; add-Computer -DomainName tpicomp.com -Description "joined domain by $cred date $date" -Credential tpicomp\$cred "

感谢@lit 和@Compo 这是最终结果。

@echo off
Powershell -command " $date = Get-Date; $cred= read-host -prompt 'IT username'; add-Computer -DomainName tpicomp.com -Description "joined domain by $cred date $date" -Credential tpicomp\$cred "

thanks for @lit and @Compo here is the final result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文