简体   繁体   English

如何在Powershell中使用字符串变量?

[英]How Do I Use a string a variable in Powershell?

I am very very new to PowerShell. 我对PowerShell非常新。 I am trying to create a script to create new user in AD, with prompts for each switch, but I am struggling a little with the Syntax and was hoping someone could help me. 我正在尝试创建一个脚本来在AD中创建新用户,并提示每个开关,但我正在努力与语法,并希望有人可以帮助我。

Currently I have: 目前我有:

$FirstName = Read-Host -Prompt "Please enter the first name"

$LastName = Read-Host -Prompt "Please enter the last name"

New-ADUser -Name $FirstName.$LastName -GivenName $FirstName -Surname $LastName

The above gives me the prompts that I want, but then when I input the last name, it returns the following error: 上面给出了我想要的提示,但是当我输入姓氏时,它返回以下错误:

"New-ADUser : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or 
empty, and then try the command again.
At line:3 char:18
+ New-ADUser -Name $FirstName.$LastName -GivenName $FirstName -Surname  ...
+                  ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.NewADUser
"

I assume this is because I have not put the correct entry in after -Name. 我假设这是因为我没有在-Name之后输入正确的条目。

Hopefully one of you can help me and give me an explanation & example as to what I should do. 希望你们中的一个能帮助我,并给我一个解释和示例,说明我应该做些什么。

Change this line: 改变这一行:

New-ADUser -Name $FirstName.$LastName -GivenName $FirstName -Surname $LastName

to: 至:

New-ADUser -Name "$FirstName.$LastName" -GivenName $FirstName -Surname $LastName

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

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