locked
Updating AD Groups with CSV RRS feed

  • Question

  • Hi all

    I think im on the right path but need a little help please. I am trying to update managedby and descriptions but I would like to display and error message if either successful or failure. I have managed to get the AD properties to update but cant control the error or success messages. So far I have done the following. What would be nice is if I could update the csv with a new column with "success" or "failed" as a result.

    Import-Csv C:\validation.csv | ForEach-Object {
    
                        If (Set-ADGroup -identity $_.name -ManagedBy $_.Managedby -Description $_.description){
    
                                Write-host -ForegroundColor Green "Group $($_.name) has been successfully Updated"
    
                        }else{
    
                                Write-Warning "Group $($_.Name) was not updated - Please check and try again!"
                        
    
                        }
                        }



    • Edited by ktitchard Thursday, March 31, 2016 11:08 AM
    Thursday, March 31, 2016 11:03 AM

Answers

  • you can use try{}catch{} for such things
    • Proposed as answer by jrv Thursday, March 31, 2016 1:49 PM
    • Marked as answer by Elaine Jing Tuesday, April 5, 2016 2:54 AM
    Thursday, March 31, 2016 11:13 AM

All replies

  • you can use try{}catch{} for such things
    • Proposed as answer by jrv Thursday, March 31, 2016 1:49 PM
    • Marked as answer by Elaine Jing Tuesday, April 5, 2016 2:54 AM
    Thursday, March 31, 2016 11:13 AM
  • You can use something like this :

    function Update-CSV ($statusupdate)
    {
    Import-Csv C:\validation.csv | Select-Object *,@{Name='Status';Expression={$statusupdate}} | Export-Csv C:\validation1.csv -NoTypeInformation
    }
    Import-Csv C:\validation.csv | ForEach-Object {
    
                        If (Set-ADGroup -identity $_.name -ManagedBy $_.Managedby -Description $_.description ){
    
                                #Write-host -ForegroundColor Green "Group $($_.name) has been successfully Updated"
    							$Value="Success"
    							Update-CSV($Value)
    
    
                        }else{
    
                                #Write-Warning "Group $($_.Name) was not updated - Please check and try again!"
                        		$Value="failed"
    							Update-CSV($Value)
    
                        }
                        }

    For each object in the CSV file, it add success or failed . The Update-CSV function imports the original CSV file and adds a new column status to it. The result is then exported to validation1 file.






    Thursday, March 31, 2016 12:03 PM
  • You can use something like this :

    function Update-CSV ($statusupdate)
    {
    Import-Csv C:\validation.csv | Select-Object *,@{Name='Status';Expression={$statusupdate}} | Export-Csv C:\validation1.csv -NoTypeInformation
    }
    Import-Csv C:\validation.csv | ForEach-Object {
    
                        If (Set-ADGroup -identity $_.name -ManagedBy $_.Managedby -Description $_.description ){
    
                                #Write-host -ForegroundColor Green "Group $($_.name) has been successfully Updated"
    							$Value="Success"
    							Update-CSV($Value)
    
    
                        }else{
    
                                #Write-Warning "Group $($_.Name) was not updated - Please check and try again!"
                        		$Value="failed"
    							Update-CSV($Value)
    
                        }
                        }

    For each object in the CSV file, it add success or failed . The Update-CSV function imports the original CSV file and adds a new column status to it. The result is then exported to validation1 file.






    Thanks for the help. I have run this as a test and I get the same issue as what I was experiencing. The status is always failed regardless if it was successful. I think it has something to with this section

    If (Set-ADGroup -identity $_.name -ManagedBy $_.Managedby -Description $_.description ){

    Thursday, March 31, 2016 3:37 PM
  • If is a condition that evaluates based on true/false value. Set-ADGroup cmdlet does not return a boolean value. So you have to check whether the command succeeded or not using $?

    function Update-CSV ($statusupdate)
    {
    Import-Csv C:\validation.csv | Select-Object *,@{Name='Status';Expression={$statusupdate}} | Export-Csv C:\validation1.csv -NoTypeInformation
    }
    Import-Csv C:\validation.csv | ForEach-Object {
    
    					Set-ADGroup -identity $_.name -ManagedBy $_.Managedby -Description $_.description
                        If ($? ){
    
                                #Write-host -ForegroundColor Green "Group $($_.name) has been successfully Updated"
    							$Value="Success"
    							Update-CSV($Value)
    
    
                        }else{
    
                                #Write-Warning "Group $($_.Name) was not updated - Please check and try again!"
                        		$Value="failed"
    							Update-CSV($Value)
    
                        }
                        }


    • Proposed as answer by Elaine Jing Friday, April 1, 2016 10:29 AM
    • Marked as answer by ktitchard Monday, April 4, 2016 10:28 AM
    • Unmarked as answer by ktitchard Monday, April 4, 2016 3:11 PM
    Thursday, March 31, 2016 5:49 PM
  • Me again

    I have managed to get it working but have 1 problem. I have used the debugger and line break to find there has to be an issue with updating the CSV. Here is my test using 3 groups and Test Group 2 and 3 do not exist.

    Test group 1

    Test groupe 2

    Test groupe 3

    When I run the script each group ends up with the correct $value and when it returns to the 4th line, $statusupdate shows the correct value. However my csv looks like this

    name                  status

    Test Group1         success
    Test Groupe 2      success
    Test Groupe 3      success

    If I change the export csv to ft groupname, status I get this.

    Group Test Group 1 was updated successfully

    groupname                 Status

    --------------------------------------

    Test Group1               success
    Test Groupe 2            success
    Test Groupe 3            success

    Failed to update group Test Groupe 2 Check name and try again!

    groupname                 Status

    --------------------------------------

    Test Group1               failed
    Test Groupe 2            failed
    Test Groupe 3            failed

    Failed to update group Test Groupe 3 Check name and try again!

    groupname                 Status

    --------------------------------------

    Test Group1               failed
    Test Groupe 2            failed
    Test Groupe 3            failed

    This reflects in the CSV

    My code to date below

    function Update-CSV ($statusupdate)
    {
    
    import-csv C:\ps\Validation.csv | Select-Object *,@{Name='Status';Expression={$statusupdate}} | Export-Csv C:\ps\results.csv -NoTypeInformation
    
    }
    
    $csv = import-csv C:\ps\Validation.csv
    
    
    ForEach ($row in $csv)
    
    {
    
           try { Set-Adgroup -Identity $row.groupname -ManagedBy $row.Managedby -ErrorAction Stop
    
           Write-host -ForegroundColor Green "Group "$row.groupname"was updated Successfully"
                                        
                                        $Value="Success"
    				    Update-CSV($Value)
    
    
           }Catch { Write-host -ForegroundColor red "Failed to update group"$row.groupname"Check the name and try again!"
    
            			    $Value="Failed"
    				    Update-CSV($Value) }



    • Edited by ktitchard Monday, April 4, 2016 3:30 PM
    Monday, April 4, 2016 2:33 PM
  • anyone?
    Tuesday, April 5, 2016 2:15 PM