Import-GPO by using folders named after its DisplayName

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 4 months ago.

  • Author
    Posts
  • #6078

    by JMeterX at 2013-04-22 12:09:07

    I am using a powershell script to import GPOs which I have saved as GUIDs (the default).

    I wish to import the GPOs on a different system, the GPOs are saved to a CD at this point. So a simple way to import a single GPO would be:

    $domain=[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name

    $import_array = get-childitem ($current_drive + $gpo_backup_location) | Select name
    foreach ($GPO in $import_array) {
    Import-GPO |
    -backupgponame $GPO.name -path ($Current_Drive + $gpo_backup_location) |
    -domain $domain |
    -Targetname $GPO.name |
    -migrationtable ($Current_drive + $mig_table_path) |
    -Createifneeded;
    }
    }

    the $import_array command gives me my folder names

    Name
    ----------
    GPO-1
    GPO-2
    GPO-3
    GPO-4
    and so on...

    —————-
    EDITS

    Here is an example of the backup script that I am using to complete this action

    Function GPO_Backup_DisplayName ($MenuChoice) {
    write 'Backing Up GPOs by Display Name'
    Foreach ($GPO in $GPO_Temp_Backup) {
    $GPOBackup = Backup-GPO `
    -name $GPO.DisplayName `
    -path ($Current_drive + $GPO_Backup_Location) `
    -Domain $domain;
    Get-GPOReport $GPO.DisplayName `
    -reporttype HTML `
    -Domain $domain `
    -path ($Current_drive + $GPO_Backup_HTML_Report + $GPO.DisplayName + ".html");
    Rename-item
    -path ($Current_drive + $GPO_Backup_Location + "{" + $GPOBackup.Id + "}") `
    -newname $GPO.DisplayName
    }
    }

    This script saves all the GPO's as their DisplayName instead of the ID it is typically saved by. I want to be able to run an import-gpo block of code such as the one at the very beginning that will grab each one of those GPOs and import them. The issue I am having is it cannot find the names of the folders. I tried the code for the import (at the very top of this post) where my $import_array gives me the folder names (example shown below) but I cannot get it to run through this folder list. I did not want to have to use text files but I might have too.

    Name
    ----------
    GPO-1
    GPO-2
    GPO-3
    GPO-4
    and so on...

    by poshoholic at 2013-04-23 21:06:53

    Could you rephrase the question? I'm not sure what folder name you are talking about.

    by JMeterX at 2013-04-29 06:14:48

    I have edited the question

    by poshoholic at 2013-04-29 06:42:51

    Let's try a few things to help work this problem out.

    First, when you are import an array, unless you are using PowerShell 3 you should explicitly make sure that is an array (see http://poshoholic.com/2009/07/03/powers ... -an-array/). Also, if all you want from the imported contents of the file is the Name parameter, then you should use Select-Object -ExpandProperty instead of Select-Object with the Property parameter. This forces PowerShell to pull the property from the object rather than return an object with one property.

    Put those two together, and that means this line:
    $import_array = get-childitem ($current_drive + $gpo_backup_location) | Select name
    Should be written like this:
    $import_array = @(get-childitem ($current_drive + $gpo_backup_location) | Select-Object -ExpandProperty name)
    Now, with that line you are guaranteed in PowerShell 2 or 3 to have an array of strings (which may be empty if you don't have any files in the folder you specify).

    Once that is done, let's look inside of your foreach loop. In order to make debugging easier, it can help to put the parameters together as a collection so that you can output them, make sure that they are what you think they should be. Splatting helps with this. To leverage splatting, plus the change we made above which makes the $GPO variable be a string instead of an object with a name parameter, we can change this (which I think you typed wrong because the syntax with the pipelines is incorrect):
    Import-GPO |
    -backupgponame $GPO.name -path ($Current_Drive + $gpo_backup_location) |
    -domain $domain |
    -Targetname $GPO.name |
    -migrationtable ($Current_drive + $mig_table_path) |
    -Createifneeded;

    to this:
    $importGPOParameters = @{
    BackupGpoName = $GPO
    Path = $Current_Drive + $gpo_backup_location
    Domain = $domain
    TargetName = $GPO
    MigrationTable = $Current_drive + $mig_table_path
    CreateIfNeeded = $true
    )
    Import-GPO @importGPOParameters

    One nice thing about that approach is that you can see the parameter values before they go into Import-GPO when debugging, and you could optionally output them using one of the Write-* commands if you wanted some visual cue as to what parameters were going into your Import-GPO command. I strongly recommend you do this. Adding more Write-* commands to your scripts (Write-Verbose and Write-Debug are helpful here because they are not shown by default) really helps when troubleshooting. Also, don't be afraid to use the debugger (either in PowerShell ISE or one of the other editors that are available). Both really help to see what is going on. For PowerShell 2, I recommend PowerGUI or PowerSE, both of which are free. If you are in PowerShell 3 though, PowerShell ISE is definitely the way to go.

    Give some of those changes a try and see if brings you any closer to solving your problem. If not, if you have errors share those errors here. Or if you're stuck, share script up to the point where you are stuck, and I can suggest additional debugging tips to help you work it out.

    by JMeterX at 2013-04-29 08:26:21

    Thanks for the quick reply. I am pretty new to Powershell so I am still wrapping my head around it. One thing I don't understand is why when I use a " | " to separate my lines it does not work, but if I use a " ` " it does work. I have seen many times people say not to use the tick mark but I can't seem to get anything else to actually work. But that's another problem for a different day. I attempted some of your write outs, most of this is on a system in which I cannot copy and paste to the forum so pasting error codes will not be helpful. However, I see your point with the arrays, I sorta questioned if I was really doing that correctly or not. Though, both ways were storing the same results. I also had at one point the select-object in my array phase but didn't think there would be a difference to make it select name. I see your point on this, thanks! At any rate, I did change my code to:

    Function GPO_Import_W/MigrationTable ($MenuChoice) {
    write 'Importing GPOs with Migration Table'
    $import_array = @(get-childitem ($Current_drive + $GPO_Backup_Location) + Select-Object -ExpandProperty name)
    write $import-array
    Foreach ($GPO2 in $import_array) {
    $importgpoparameters = @(
    Import-GPO `
    -BackupGPOName $GPO.name -path ($Current_drive + $GPO_Backup_Location) `
    -Domain $domain `
    -TargetName $GPO.name `
    -MigrationTable ($Current_drive + $Mig_Table_Path) `
    -CreateIfNeeded;
    )
    write $importparamters
    import-gpo @importgpoparameters
    }
    }

    The code does function but it never gets past -backupgponame and -path . Instead I receive an error about the GPO not being able to import because no backup that is named "GPO-1" was found at domain.com. This is the original error I have been receiving with my code prior to the changes. It simply cannot look at the folder names and import that GPO. This is where I am stuck...Not sure if its even fundamentally possible. But to move on, following the error, I receive a prompt to input the ID of the GPO in which I want to import followed by its path. I never get to the point where I can investigate the write $importgpoparamters section

    by JMeterX at 2013-04-29 08:51:32

    I am guessing that it will not work because it inherently looks for the file to be saved as its ID name and since I am saving them by their displayname it will never find the correct place.

    by poshoholic at 2013-04-30 06:06:30

    That could definitely be possible. If you are saving the files using the display name, then trying later look them up by name, that would only work if display name and name were one and the same. Based on that comment I wasn't sure if you needed more help right now or not. If you have additional questions, please let me know.

You must be logged in to reply to this topic.