PowerShell script that does nothing

tito -  
zucrezel Posted messages 30 Status Member -
Hello everyone
I have a small problem or is it really one?
I followed a tutorial online for importing a CSV file into PowerShell to create user accounts. I don't see any error messages, yet the accounts are not being created. Do you have any idea where this might be coming from, please?

Thanks in advance to anyone who can shed some light... :s

Import-Module ActiveDirectory
{
$Users = Import-Csv -Delimiter ";" -Path "C:\Users\adm-tito\Desktop\test.csv"
foreach($temp in $Users) { }

$upn = $temp.SamAccountName + "Nougatine.global"
$name = $temp.firstname + " " + $temp.Lastname
$fName = $temp.Firstname
$SAM = $temp.SAMAccountName
$password = $temp.Password
$description = $temp.Description
$ou = $temp.OU

try{

New-ADUser -Name $name -SamAccountName $SAM -UserPrincipalName $upn -DisplayName $name -GivenName $fName -SurName $temp.Lastname -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -PasswordNeverExpires $true -Description $description -Path $ou

echo "User added: $name"

} catch {
echo "User not added: $name"
}

}

Configuration: Windows / Chrome 59.0.3071.115

8 answers

jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
Hello,

First:
 foreach($temp in $Users) { } 

... You misplaced your closing brace ...

--
Best regards,
Jordane
0
tito
 
Hello Jordan, Thank you for taking the time to look into my issue. Well, when I remove it to put it at the end, it shows me that I'm missing a brace where I removed it... "It tells me Missing body of the statement in the foreach loop."

So I don't think that's it? Do you see anything else?
0
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
You also have an extra opening brace at the beginning of your script I think....

try this:
 Import-Module ActiveDirectory $Users = Import-Csv -Delimiter ";" -Path "C:\Users\adm-tito\Desktop\test.csv" foreach($temp in $Users) { $upn = $temp.SamAccountName + "Nougatine.global" $name = $temp.firstname + " " + $temp.Lastname $fName = $temp.Firstname $SAM = $temp.SAMAccountName $password = $temp.Password $description = $temp.Description $ou = $temp.OU try{ New-ADUser -Name $name -SamAccountName $SAM -UserPrincipalName $upn -DisplayName $name -GivenName $fName -SurName $temp.Lastname -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -PasswordNeverExpires $true -Description $description -Path $ou Write-Host "User added: $name" } catch { Write-Host "User not added: $name" } } 


Note: In PowerShell .. we use Write-Host and not echo.
0
tito
 
Okay, thanks! I'll try that when I get home and I’ll keep you posted!
0
tito
 
Hello, I tried what you told me and now I have the following red line:
"The term << "Write-Host" "User not added: $name" >> is not recognized as the name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path exists, verify that the path at line : 13 Character : 48

+ Write Host "User not added: $name" <<<<
+ CategoryInfo : ObjectNotFound: <Write-Host"Util... added : $name"String> [], CommandNotFoundException
+FullyQualifiedErrorId : CommandNotFoundException

Any idea?
0
tito
 
Hello Jordan, did you forget about me?
0
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
Are you in PowerShell 2 or 3 or another version?
0
tito > jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention  
 
Nom Valeur
---- -----
PSVersion 5.1.14393.1358
PSEdition Bureau
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.1358
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
0
zucrezel Posted messages 30 Status Member 4
 
Hello,

Given the error message "Command NotFoundException", you forgot the space on line 21 between the CmdLet name (Write-Host) and its parameter (which you correctly wrote on line 19).
Without the space separator, the entire line 21 is treated as a CmdLet that is not recognized by PowerShell.

line 21 before correction :
 Write-Host"User not added: $name"


after correction :
 Write-Host "User not added: $name"
0
tito
 
Hello zucrezel,
Thank you for looking into my case. I just copied and pasted what I was given. Thanks for the correction :) It works, let's say partially, because I no longer have the error but my users are still not created and I don't see why that's happening. The script runs now, but in the end I have 12 "User not added," which is the number of users I'm trying to add. Could you please see what's going wrong?
Really thanks again for your help.
0
zucrezel Posted messages 30 Status Member 4
 
Hello,
In the catch, add the retrieval of error messages (you may have a write permission issue in the AD):
$errmsg=$_.Exception.message $errinfo=$_.Exception.itemname write-host $errinfo $errmsg 
0