I need help adding a exception to my code, I’m imported from csv to update active directory attributes. It works, but I need to put in a exception for example if one of the attributes from the csv is blank or do not have any values \" \".<\/p>\n
Import-CSV $importfile | ForEach-Object{\n $samaccountname = $_.sAMAccountName.ToLower()\n $department = $_.department\n $phone = $_.Phone\n $emailAddress = $_.emailAddress\n $manager = $_.manager.ToLower() \n\nTry { \n $exists = Get-ADUser -LDAPFilter \"(sAMAccountName=$samaccountname)\" \n }\n Catch \n { \n write-host \"Users did not exist.\"\n }\n If ($exists)\n {\n \n If ($department) \n { \n Set-ADUser -Identity $samaccountname -department $department\n }\n\n<\/code><\/pre>","upvoteCount":5,"answerCount":4,"datePublished":"2014-10-23T14:07:54.000Z","author":{"@type":"Person","name":"cvue","url":"https://community.spiceworks.com/u/cvue"},"acceptedAnswer":{"@type":"Answer","text":"ForEach($user in (Import-CSV $importfile)) {\n If(!($user.samaccountname.trim())){\n Continue\n }\n \n $splat = @{ 'Identity'=$user.sAMAccountName.trim() }\n \n If($user.department.Trim()){\n $splat.Add('Department',$user.department.trim())\n }\n If($user.phone.Trim()){\n $splat.Add('OfficePhone',$user.phone.trim())\n }\n If($user.emailaddress.Trim()){\n $splat.Add('EmailAddress',$user.emailaddress.trim())\n }\n If($user.manager.Trim()){\n $splat.Add('Manager',$user.manager.trim())\n }\n\n If(($splat.keys | Measure-Object).count -ge 2) {\n Set-ADUser @splat\n }\n}\n<\/code><\/pre>\n
Advertisement
Somewhat improved. Still untested.<\/p>","upvoteCount":1,"datePublished":"2014-10-23T14:23:14.000Z","url":"https://community.spiceworks.com/t/help-with-active-directory-exception/350712/3","author":{"@type":"Person","name":"craigduff","url":"https://community.spiceworks.com/u/craigduff"}},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
I need help adding a exception to my code, I’m imported from csv to update active directory attributes. It works, but I need to put in a exception for example if one of the attributes from the csv is blank or do not have any values \" \".<\/p>\n
Import-CSV $importfile | ForEach-Object{\n $samaccountname = $_.sAMAccountName.ToLower()\n $department = $_.department\n $phone = $_.Phone\n $emailAddress = $_.emailAddress\n $manager = $_.manager.ToLower() \n\nTry { \n $exists = Get-ADUser -LDAPFilter \"(sAMAccountName=$samaccountname)\" \n }\n Catch \n { \n write-host \"Users did not exist.\"\n }\n If ($exists)\n {\n \n If ($department) \n { \n Set-ADUser -Identity $samaccountname -department $department\n }\n\n<\/code><\/pre>","upvoteCount":5,"datePublished":"2014-10-23T14:07:54.000Z","url":"https://community.spiceworks.com/t/help-with-active-directory-exception/350712/1","author":{"@type":"Person","name":"cvue","url":"https://community.spiceworks.com/u/cvue"}},{"@type":"Answer","text":"Import-CSV $importfile | ForEach-Object{\n $splat = @{ 'Identity'=$_.sAMAccountName }\n \n If($_.department.Trim()){\n $splat.Add('Department',$_.department)\n }\n If($_.phone.Trim()){\n $splat.Add('OfficePhone',$_.phone)\n }\n If($_.emailaddress.Trim()){\n $splat.Add('EmailAddress',$_.emailaddress)\n }\n If($_.manager.Trim()){\n $splat.Add('Manager',$_.manager)\n }\n \n Set-ADUser @splat\n}\n<\/code><\/pre>\nThis is untested, but that is the general idea.<\/p>","upvoteCount":1,"datePublished":"2014-10-23T14:15:17.000Z","url":"https://community.spiceworks.com/t/help-with-active-directory-exception/350712/2","author":{"@type":"Person","name":"craigduff","url":"https://community.spiceworks.com/u/craigduff"}},{"@type":"Answer","text":"