PowerShell script that does nothing
tito
-
zucrezel Posted messages 30 Status Member -
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
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
Hello,
First:
... You misplaced your closing brace ...
--
Best regards,
Jordane
First:
foreach($temp in $Users) { } ... You misplaced your closing brace ...
--
Best regards,
Jordane
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?
So I don't think that's it? Do you see anything else?
You also have an extra opening brace at the beginning of your script I think....
try this:
Note: In PowerShell .. we use Write-Host and not echo.
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.
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?
"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?
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 :
after correction :
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"
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.
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.