简体   繁体   English

将计算机加入域的 Powercli 脚本

[英]Powercli script to join computer to the Domain

I'm tring to run Powercli script from VM that have all the modules nedded.我正在尝试从包含所有模块的 VM 运行 Powercli 脚本。 The script should insert other VM to domain.该脚本应将其他 VM 插入域。

$ScriptText ="Add-Computer -DomainName foofoo.com -DomainCredential " + $DomainC +"  -OUPath 'OU=CACI,OU=Computers,OU=bla,OU=Regions,DC=bla,DC=com'" 
echo $Script
Invoke-VMScript -vm $VMName -GuestCredential $AdminC -ScriptText $ScriptText 

all the variables inserted correctly.所有变量都正确插入。

runing Add-Computer -DomainName foofoo.com -DomainCredential $DomainC -OUPath 'OU=CACI,OU=Computers,OU=bla,OU=Regions,DC=bla,DC=com' from the other vm poweshell console is running well and the output message WARNING: The changes will take effect after you restart the computer..从另一个 vm poweshell 控制台运行Add-Computer -DomainName foofoo.com -DomainCredential $DomainC -OUPath 'OU=CACI,OU=Computers,OU=bla,OU=Regions,DC=bla,DC=com'运行良好并且output 消息WARNING: The changes will take effect after you restart the computer..

$Script return: $脚本返回:

Add-Computer -DomainName foofoo.com -DomainCredential System.Net.NetworkCredential -OUPath 'OU=CACI,OU=Computers,OU=bla,OU=Regions,DC=bla,DC=com'

but after that this script stuck and I have no error or other output.但在那之后这个脚本卡住了,我没有错误或其他 output。

Any idea what is the reason for that?知道这是什么原因吗?

The Add-Computer CMDlet takes a credential object for the domain credential parameter. Add-Computer CMDlet 将凭证 object 用于域凭证参数。 By trying to convert that to a string in your $scripttext variable - you're losing the credential type in the conversion.通过尝试将其转换为 $scripttext 变量中的字符串 - 您在转换中丢失了凭证类型。 You need to make a credential object inside your script text rather than passing in a variable containing the credential object.您需要在脚本文本中创建凭证 object,而不是传入包含凭证 object 的变量。 This adds some complexity because you generally want to pull a password from a secure vault.这增加了一些复杂性,因为您通常希望从安全保险库中提取密码。 The below examples shows how to include the password as a plain text - but this isn't really advised for obvious reasons.下面的示例显示了如何将密码包含为纯文本 - 但出于明显的原因,并不建议这样做。

$scripttext = @'
$user = "UserName"
$password = ConvertTo-SecureString "bar" -AsPlainText -Force
$DomainC = New-Object PSCredential $user, $password
Add-Computer -DomainName foofoo.com -DomainCredential $DomainC -OUPath 'OU=CACI,OU=Computers,OU=bla,OU=Regions,DC=bla,DC=com'
'@
Invoke-VMScript -vm $VMName -GuestCredential $AdminC -ScriptText $ScriptText 

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

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