string 在powershell中连接两个字符串并作为一个参数提供

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45091283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:37:55  来源:igfitidea点击:

Concat two strings in powershell and supply as one parameter

stringpowershellconcatenation

提问by BLang

I am trying to concatinate two strings into a variable and then supple the new variable as a parameter in a command

我试图将两个字符串连接成一个变量,然后将新变量作为命令中的参数提供

$firstName = Read-Host -Prompt 'enter user first name'
$lastName = Read-Host -Prompt 'enter user last name'
$userAblevetsEmail = '' + $firstName + '.' + $lastName + '@company.com'
New-MsolUser -UserPrincipalName $userAblevetsEmail  -DisplayName $firstName + " " + $lastName -FirstName $firstName

I get the following error:

我收到以下错误:

"New-MsolUser : A positional parameter cannot be found that accepts argument '+'."

“New-MsolUser:找不到接受参数“+”的位置参数。”

回答by Jason Snell

Your code New-MsolUser -UserPrincipalName $userAblevetsEmail -DisplayName $firstName + " " + $lastName -FirstName $firstNameis slightly off.

你的代码New-MsolUser -UserPrincipalName $userAblevetsEmail -DisplayName $firstName + " " + $lastName -FirstName $firstName有点不对劲。

The info following -DisplayNamecannot have spaces without being contained inside of something.

下面的信息-DisplayName不能有空格而不包含在某物内。

Here is an example fix:

这是一个示例修复:

New-MsolUser -UserPrincipalName $userAblevetsEmail -DisplayName "$firstName $lastName" -FirstName $firstName

New-MsolUser -UserPrincipalName $userAblevetsEmail -DisplayName "$firstName $lastName" -FirstName $firstName

You can simply use the variables directly inside quotation marks.

您可以简单地直接在引号内使用变量。