none
import-gpo with csvfile RRS feed

  • General discussion

  • Hy,

    I am a simple question i think.

    csv:

    BackupGpoName,BackupId,GPOName,path
    "GPO1",{11111111-2222-3333-444444444444},"GPO1","C:\BackupGPO"
    "GPO2",{78888888-1222-4555-8777-123456789123},"GPO1","C:\BackupGPO"

    Script:

    $csv = import-csv -Path "C:\gpotest.csv" -Header BackupGpoName, BackupId, gponame, Path
      $csv | Foreach-Object {
      $params = @{
        BackupGpoName=$_.BackupGpoName
        BackupId=$_.backupid
        TargetName=$_.gponame
        Path=$_.path
                }
      $params.$type = $true
      import-gpo @params -CreateIfNeeded
    }

    But i get the error:

    Foreach-Object : Cannot bind parameter 'BackupId'. Cannot convert value "BackupId" to type "System.Guid". Error: "Guid
    should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."
    At line:2 char:10
    +   $csv | Foreach-Object {
    +          ~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand

    • Changed type Bill_Stewart Friday, July 27, 2018 6:16 PM
    • Moved by Bill_Stewart Friday, July 27, 2018 6:17 PM This is not "debug/fix/rewrite my script for me" forum
    Monday, April 23, 2018 4:00 PM

All replies

  • Don't use -Header with your Import-Csv command. Your CSV file already contains a header.

    Because you are specifying a header, your Import-Gpo command is trying to import a GPO for the first line in the CSV file (because you are specifying a header).


    -- Bill Stewart [Bill_Stewart]

    Monday, April 23, 2018 5:19 PM
  • in really my csv is like this, no with {}:

    BackupGpoName,BackupId,GPOName,path
    "GPO1",11111111-2222-3333-444444444444,"GPO1","C:\BackupGPO"
    "GPO2",78888888-1222-4555-8777-123456789123,"GPO1","C:\BackupGPO"

    Without -Header i get this error:

    Foreach-Object : Cannot process argument because the value of argument "name" is not valid. Change the value of the "name"
    argument and run the operation again.
    At line:2 char:10
    +   $csv | Foreach-Object {
    +          ~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], RuntimeException
        + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ForEachObjectCommand

    Monday, April 23, 2018 6:18 PM
  • Simplify your code first:

    import-csv C:\gpotest.csv |
        Foreach-Object {
           import-gpo -BackupGpoName $_.BackupGpoName -BackupId $_.backupid -Path $_.path
        }


    \_(ツ)_/


    • Edited by jrv Monday, April 23, 2018 6:48 PM
    Monday, April 23, 2018 6:26 PM
  • Foreach-Object : Parameter set cannot be resolved using the specified named parameters.
    At line:2 char:5
    +     Foreach-Object {
    +     ~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
        + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.ForEachObjectCommand
     
    Monday, April 23, 2018 6:39 PM
  • It seems to me you are making changes without understanding.

    I would recommend going through some PowerShell tutorials and start actually learning how it works.


    -- Bill Stewart [Bill_Stewart]

    Monday, April 23, 2018 6:47 PM
  • Yes you're allright, i need to study more, english include. lol.

    But i find a solution for me problem with another approach. I used a function  like this below:

    Function importGPO() {

            [CmdletBinding()]

            Param(
                
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$BackupId,
                
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$GPOName,
                            
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$path


           )


     
        Process {
         
        import-gpo -BackupId $BackupId -TargetName $GPOName -path $path -CreateIfNeeded
        
        }
    }


    import-csv -Path "C:\gpotest.csv" | importGPO

    Thanks. If you can say me where i find the tutorial for my issues with foreach?

    Tuesday, April 24, 2018 11:54 AM