Modifying Active Directory Users, only modification is scrip

by GeorginaHowland at 2012-12-06 06:16:40

Modifying Active Directory Users, only modification is scriptpath. I retreive 182 users from active directory wth -property sciptpath ‘xxx.bat’, I export it to CSV file. I remove 152 users from csv file, save it, Import.CSV - My goal is to modify 30 of 182 users in Active Directory

get-aduser -filter ‘scriptpath -eq "cbsbdc002.bat"’| Export-csv C:\temp\Users.CSV

import-csv c:\temp\halifax.csv (I ran this a viewed command to ensure the users to import were ok - they were ok)

import-csv c:\temp\halifax.csv | set-aduser -replace @{Scriptpath=‘YYY.bat’} (DID NOT WORK)

get-aduser (Import-csv c:\temp\halifax.csv) | set-aduser -replace @ {Scriptpath=‘yyy.bat’} (DID NOT WORK)

Ultimately I am attempting to modify the Scriptpath for 30 Users, The 30 users are random selection from the 182 users, If I were to modify all 182 users, I could do it - I am testing this in our lab…I would appreciate a detailed description of what I am doing wrong for I am not a scriptor. Thank you.
by ArtB0514 at 2012-12-06 07:05:02
As you might have guessed, reading in the CSV file only gives you the information about a user account and not a copy of the account itself. The Get-ADUser cmdlet is what will do that for you.

Since you only want a specific collection of users, you will have to search them out one-by-one to perform the update. For example:
Import-Csv c:\temp\halifax.csv | foreach {Get-ADUser -Identity $.samAccountName | Set-ADUser -Replace @{ScriptPath='yyy.bat'}}

(NOTE: It’s good that you’re trying this in the lab. I don’t have a lab or a way to test this, so it might not work perfectly and may need a bit more debugging. If you do get more errors, would you post the exact error message?)
by GeorginaHowland at 2012-12-06 08:27:27
Thanks I will try in the lab
by GeorginaHowland at 2012-12-06 08:45:36
I ran the command
PS C:> Import-Csv c:\temp\halifax.csv | foreach {Get-ADUser -Identity $
.samAccountName | Set-ADUser -Replace @{ScriptPath=‘yyy.bat’}
>>
>>
>>

After entrying the last curly bracket I pressed enter I was still in the pipeline, pressed enter again, and then one last time. I am missing something however I don’t know what, Any constructive help would greatly be appreciated. Thank you
by MRiston at 2012-12-06 12:35:27
Hope this helps

#Set Array for desired users
$arrADUsers = ( Get-ADUser -Filter * | ? ScriptPath -Match cbsbdc002.bat )

#Print & pause to confirm users
$arrADUsers
pause

#set ScriptPath to new .bat
$arrADUsers | % { Set-ADUser $.SamAccountName -ScriptPath "yyy.bat" }
by MRiston at 2012-12-06 12:44:06
Sorry - just noticed you want to parse out 150 some odd users…

In that case - I’d suggest –>

#SeOutput to file
Get-ADUser -Filter * | ? ScriptPath -Match cbsbdc002.bat | Out-File C]

Now Edit your .csv and re-save.

#Inject content of CSV into array
$arrADUsers_Edited = ( Get-Content C:\adusers.csv )

#Print & pause to confirm users
$arrADUsers_Edited
pause

#set ScriptPath to new .bat
$arrADUsers_Edited | % { Set-ADUser $
.SamAccountName -ScriptPath "yyy.bat" }
by ArtB0514 at 2012-12-07 11:38:48
Georgina: If you look closely at the script segment I entered, it ends with 2 "}" characters. For some reason, there’s a link to the script highlighter partially covering the second one. Here’s another try:

Import-Csv c:\temp\halifax.csv | foreach {
Get-ADUser -Identity $_.samAccountName | Set-ADUser -Replace @{ScriptPath='yyy.bat'}
}