Llirrom Posted August 21, 2024 Posted August 21, 2024 Hi all! I am quite new to using Powershell and Active Directory, and have a script to import new PC's into AD: Import-Module ActiveDirectory $CSV = Read-Host -Prompt ".\RenamePC.csv" $OU= Read-Host -Prompt "OU=CLASROOM Base,OU=COMPUTERS, DC=SCHOOL, DC= " Import-Csv -Path $CSV | ForEach-Object { New-ADComputer -Name $_.Computer -Path $OU -Enabled $True} The CSV contains the new PC names in the A Column from row 1-9. I run this script directly on the AD server and enter the correct name and OU. My issue is that this script runs and then does nothing to update AD, this is my 2nd week in this position so I am still really new to running powershell scripts. Any advice would be greatly appreciated!
pete Posted August 21, 2024 Posted August 21, 2024 If that's supposed to be "classroom", is there a typo in the OU name or your script ("CLASRoom Base" vs "CLASSRoom Base")? Though I'd expect powershell to throw a "I couldn't find that OU" error. What happens if you run it line-by-line from an elevated powershell prompt? (and I'm assuming someone's told you to right-click the OU in AD Users & Comptuers and select "refresh" so the contents are up to date in the GUI)
Llirrom Posted August 21, 2024 Author Posted August 21, 2024 Apologies, the prompt on "CLASROOM Base" was me changing the output of the prompts as to not give away any information on the structure when posting online. I have run the script in powershell line by line and as a result I am getting the following error: New-ADComputer : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument thatis not null or empty, and then try the command again. It appears to be hitting the ForEach loop and not being able to find the new names I have input on the CSV file: + ... Path $CSV | ForEach-Object { New-ADComputer -Name $_.Computer -Path $ ...+ ~~~~~~~~~~~ + CategoryInfo : InvalidData: ( [New-ADComputer], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.NewADComp uter
Llirrom Posted August 21, 2024 Author Posted August 21, 2024 This is a powershell script from the previous technician here who has now left. I changed the details of the prompts as I didn't want to give any information out online that I wasn't supposed to! I posted the errors above, my current thinking is that it's not reading the CSV file how I intended as it is set-up as: Name: New-Computer1 New-Computer2 etc...
snagrat Posted August 21, 2024 Posted August 21, 2024 Is the header on the CSV column called “computer”? That is what $_.computer is looking for
Roberto Posted August 21, 2024 Posted August 21, 2024 (edited) ^ bet that’s it. Can you paste the first two lines of the csv file here? Edited August 21, 2024 by Roberto 1
Llirrom Posted August 22, 2024 Author Posted August 22, 2024 (edited) I really appreciate the help guys! This is the first couple lines of the CSV: Computer C03-ICT-01 C03-ICT-02 Sorry for the lack of formatting, Computer is in A1 and the names go down the column. Edited August 22, 2024 by Llirrom
snagrat Posted August 22, 2024 Posted August 22, 2024 That looks ok then. Is the directory PS is running from contain the csv file? Typing .\RenamePC.csv would suggest it is. Tbh I’ve never done it this way. I would have a line specifically stating the csv path like: $csv = Import-csv -Path c:\filepath\filename.csv Then lose the whole “read-prompt” bit
Llirrom Posted August 22, 2024 Author Posted August 22, 2024 We have progress! It appears to now be reading the CSV file but I get a new error: New-ADComputer : A required privilege is not held by the clientAt C:\temp\New-Computer-REF.ps1:5 char:42+ ... ch-Object { New-ADComputer -Name $_.Computer -Path $OU -Enabled $True ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (CN=C03-ICT-01,O...CHS,DC=internal:String) [New-ADComputer], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:1314,Microsoft.ActiveDirectory.Management.Commands.NewADComputer
snagrat Posted August 22, 2024 Posted August 22, 2024 Are you running as a domain admin? And running powershell as Administrator? 1
Llirrom Posted August 22, 2024 Author Posted August 22, 2024 It works! I had accidentally ran it as my local user. Thanks so much for your help guys! I thought for a minute I was going to have to manually rename, deploy into sccm, push the new image and retire the old folks manually. Many thanks.
snagrat Posted August 22, 2024 Posted August 22, 2024 What was the fix for the computer name bit? This script won’t rename PCs? Just add new computers to AD as a blank object
Davit2005 Posted August 22, 2024 Posted August 22, 2024 It sounds to me it is not reading the CSV too. Can you try putting the full path in the $CSV = Read-Host -Prompt ".\RenamePC.csv" or jus change it to below to keep it as basic as possible $csv = "FULL PATH\RenamePC.csv"
Llirrom Posted August 22, 2024 Author Posted August 22, 2024 The header was plural, the PC creates a new Computer in AD. The csv is just what the previous technician named it, I am unsure why. I plan to go back and refactor these scripts once I am a little more familiar with Powershell. Any good book/tutorial recommendations would be greatly appreciated.
Davit2005 Posted August 22, 2024 Posted August 22, 2024 The header was plural, the PC creates a new Computer in AD. The csv is just what the previous technician named it, I am unsure why. I plan to go back and refactor these scripts once I am a little more familiar with Powershell. Any good book/tutorial recommendations would be greatly appreciated. It is mostly the syntax that catches me out. But I have used powershell with VMWare, Veeam, AD, Exchange, MS 365, file servers, SQL, etc. I find Always a good idea to put remarks in the script so someone coming along and updating in the future knows what bit does what.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now