Take two variables and put them on the same line

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Take two variables and put them on the same line



I have a script (thanks in largely to this site) that takes names of people from a text file and splits them into FullName, FirstName, LastName and FirstLetter.



I now plan on importing these into AD and largely, I know what I am doing.



However, I am struggling with the following section


New-ADUser -Name



I would like to do something like this


$result.ForEach(
New-ADUser -Name $_.FirstName + $_.LastName -GivenName $_.FirstName -Surname
$_.LastName -AccountPassword
(ConvertTo-SecureString -AsPlainText "APassword!" -Force)
-PasswordNeverExpires $True -UserPrincipalName
"$_.FirstLetter+$_.LastName@vennershipley.co.uk"
-SamAccountName "$_.FirstLetter $_.LastName"
-Path 'OU=Users,OU=London,OU=Sites,DC=MyCompany,DC=local'
)



This returns a error stating 'The name provided is not a properly formed account name'. Now I presume this is because, if I do this


$result.FirstName + $result.LastName



It returns the 3 first names and the 3 last names on seperate lines, so I would presume it is trying to name each person with a name on two seperate lines like



So how would I make the result display on one line, presuming this is the issue?



Also, if there are better ways of doing the AD Creation then please advise, I am still learning!





Close $result.FirstName and $result.LastName in brackets: ($_.FirstName + $_.LastName) But the first name and the last Name would be concatenated. If you want to separate them, use it like this: ($_.FirstName + " " + $_.LastName)
– Vladimir Bundalo
Aug 10 at 9:31





1 Answer
1



Enclose the two variables in parentheses to get the results of the addition just like you did in the ConvertTo-SecureString part:


ConvertTo-SecureString


$result.ForEach(
New-ADUser -Name ($_.FirstName + "" + $_.LastName) -GivenName $_.FirstName -Surname
$_.LastName -AccountPassword
(ConvertTo-SecureString -AsPlainText "APassword!" -Force)
-PasswordNeverExpires $True -UserPrincipalName
"$_.FirstLetter+$_.LastName@vennershipley.co.uk"
-SamAccountName "$_.FirstLetter $_.LastName"
-Path 'OU=Users,OU=London,OU=Sites,DC=MyCompany,DC=local'
)



Now, as you asked for better ways, see few possibilities including Splatting parameters and string.format (-f) Also notice the differents between the string formatting in the Name and SamAccountName Parameters:


(-f)


Name


SamAccountName


foreach ($user in $result)

$Params = @
Name = "$($user.FirstName) $($user.LastName)"
GivenName = $user.FirstName
Surname = $user.LastName
AccountPassword = (ConvertTo-SecureString -AsPlainText "APassword!" -Force)
PasswordNeverExpires = $true
UserPrincipalName = "01@vennershipley.co.uk" -f $user.FirstName.Chars(0),$_.Lastname
SamAccountName = "01" -f $user.FirstName.Chars(0),$_.Lastname
Path = 'OU=Users,OU=London,OU=Sites,DC=MyCompany,DC=local'


New-ADUser @Params




One more thing: to check everything is good just before executing this in production, i suggest you to add the -WhatIf parameter to the New-ADUser cmdlet, it will demonstrate the operation but will not run it


-WhatIf


New-ADUser





This is really helpful. However, I ran this with -Whatif and it gives me the results that look correct. One example being What if: Performing the operation "New" on target "CN=Jolene Matthew,OU=Users,OU=London,OU=VS Sites,DC=Vennershipley,DC=local". But, when I run the actual line, it still errors New-ADUser : The name provided is not a properly formed account name At line:2 char:5 + New-ADUser -Name ($_.FirstName + " " + $_.LastName) -GivenName $_ ...
– JRawlings
Aug 10 at 10:29





try my version... also post the results of $result.ForEach( ($_.FirstName + " " + $_.LastName) )
– Avshalom
Aug 10 at 10:31



$result.ForEach( ($_.FirstName + " " + $_.LastName) )





Looks to work. The server is unwilling to process the request which is most likely a setting by my boss somewhere so will have a talk with him now. Thank you for you help! Results for $result.ForEach( ($_.FirstName + " " + $_.LastName)) Jolene Matthew Ilka Clune Emma Bowyer
– JRawlings
Aug 10 at 10:33







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS