Please help with this script

by danielson1 at 2013-02-26 18:34:33

Found some code from Shay Levy that I’m trying to use to generate an OU structure for a SharePoint environment:
Get-Content 'OrgUnits.csv' | Foreach-Object{
$ParentContainer = Split-Path $
$Name = Split-Path $
-Leaf
New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentContainer -WhatIf
}




getting the following errors as I iterate through the objects:
New-QADObject : Cannot resolve directory object for the given identity: ‘’.
At line:4 char:2
+ New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentCont …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:slight_smile: [New-QADObject], ObjectNotFoundException
+ FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException,Quest.A
ctiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.NewObjectCmdlet


OrgUnits.csv structure is as follows (The RootOU already Exists)]
Domain RootOU Enclave Classification Environment Type
a.com Sharepoint External Moderate Production Service
a.com Sharepoint External Moderate Production Computer
a.com Sharepoint External Moderate Staging Service
a.com Sharepoint External Moderate Staging Computer
a.com Sharepoint External Moderate Development Service
a.com Sharepoint External Moderate Development Computer
a.com Sharepoint External Low Production Service
a.com Sharepoint External Low Production Computer
a.com Sharepoint External Low Staging Service
a.com Sharepoint External Low Staging Computer
a.com Sharepoint External Low Development Service
a.com Sharepoint External Low Development Computer
a.com Sharepoint Internal Moderate Production Service
a.com Sharepoint Internal Moderate Production Computer
a.com Sharepoint Internal Moderate Staging Service
a.com Sharepoint Internal Moderate Staging Computer
a.com Sharepoint Internal Moderate Development Service
a.com Sharepoint Internal Moderate Development Computer
a.com Sharepoint Internal Low Production Service
a.com Sharepoint Internal Low Production Computer
a.com Sharepoint Internal Low Staging Service
a.com Sharepoint Internal Low Staging Computer
a.com Sharepoint Internal Low Development Service
a.com Sharepoint Internal Low Development Computer

Thanks in advance for any input!
by compugab at 2013-02-27 03:34:41
Hi,

I was wondering what¸happen if you show the value of $Name and $ParentContainer. Might be where the problem is. Could you execute you script like this :

Get-Content 'OrgUnits.csv' | Foreach-Object{
$ParentContainer = Split-Path $
$Name = Split-Path $
-Leaf
"Name : $Name"
"ParentContainer : $ParentContainer"
}


Thanks
by MaxMad at 2013-02-27 04:55:10
Why you don’t you use import-csv and work with objects and properties?

import-csv .\test.csv -Delimiter "t&quot; | Foreach-Object {<br> $ParentContainer = $_.RootOU<br> $Name = $_.Enclave<br>...</code></blockquote>by danielson1 at 2013-02-27 05:55:01<blockquote><strong>Added -Delimeter &quot;t", used import-csv instead of get-object, and commented the New-QADObject cmdlet

Import-Csv 'OrgUnits.csv' -Delimiter "t&quot; | Foreach-Object{<br> $ParentContainer = Split-Path $_<br> $Name = Split-Path $_ -Leaf<br><br>&quot;Name = $Name&quot;<br>&quot;ParentContrainer : $ParentContainer&quot; <br><br> #New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentContainer -WhatIf<br>}</code><br><br><strong>The output is as follows when I run the script as @compugab recommends]<br>Name = @{Type,Environment,Classification,Enclave=Service,Production,Moderate,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Production,Moderate,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Staging,Moderate,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Staging,Moderate,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Development,Moderate,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Development,Moderate,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Production,Low,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Production,Low,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Staging,Low,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Staging,Low,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Development,Low,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Development,Low,External}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Production,Moderate,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Production,Moderate,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Staging,Moderate,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Staging,Moderate,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Development,Moderate,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Development,Moderate,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Production,Low,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Production,Low,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Staging,Low,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Staging,Low,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Service,Development,Low,Internal}<br>ParentContrainer : <br>Name = @{Type,Environment,Classification,Enclave=Computer,Development,Low,Internal}<br>ParentContrainer :<br><strong>Changed Delimiter to &quot;,&quot; - read in csv with Get-Content again</strong><br><code>Get-Content &#39;OrgUnits.csv&#39; -Delimiter &quot;,&quot; | Foreach-Object{<br> $ParentContainer = Split-Path $_<br> $Name = Split-Path $_ -Leaf<br><br>&quot;Name = $Name&quot;<br>&quot;ParentContrainer : $ParentContainer&quot; <br><br> New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentContainer -WhatIf<br>}</code><br><br><strong>Output: </strong><br>Name = Type,<br>ParentContrainer : <br>New-QADObject : Cannot resolve directory object for the given identity: ''.<br>At line:8 char:2<br>+ New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentCont ...<br>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br> + CategoryInfo : NotSpecified: (:) [New-QADObject], ObjectNotFoundException<br> + FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException,Quest.A <br> ctiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.NewObjectCmdlet<br> <br>Name = Environment,<br>ParentContrainer : <br>New-QADObject : Cannot resolve directory object for the given identity: ''.<br>At line:8 char:2<br>+ New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentCont ...<br>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br> + CategoryInfo : NotSpecified: (:) [New-QADObject], ObjectNotFoundException<br> + FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException,Quest.A <br> ctiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.NewObjectCmdlet<br> <br>Name = Classification,<br>ParentContrainer : <br>New-QADObject : Cannot resolve directory object for the given identity: ''.<br>At line:8 char:2<br>+ New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentCont ...<br>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br> + CategoryInfo : NotSpecified: (:) [New-QADObject], ObjectNotFoundException<br> + FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException,Quest.A <br> ctiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.NewObjectCmdlet<br> <br>Name = Enclave<br>Service,<br>ParentContrainer : <br>New-QADObject : Cannot resolve directory object for the given identity: ''.<br>At line:8 char:2<br>+ New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentCont ...<br>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br> + CategoryInfo : NotSpecified: (:) [New-QADObject], ObjectNotFoundException<br> + FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException,Quest.A <br> ctiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.NewObjectCmdlet<br> <br>Name = Production,<br>ParentContrainer : <br>New-QADObject : Cannot resolve directory object for the given identity: ''.<br>At line:8 char:2<br>+ New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentCont ...<br>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br> + CategoryInfo : NotSpecified: (:) [New-QADObject], ObjectNotFoundException<br> + FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException,Quest.A <br> ctiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.NewObjectCmdlet<br> <br>Name = Moderate,<br>ParentContrainer : <br>New-QADObject : Cannot resolve directory object for the given identity: ''.<br>At line:8 char:2<br>+ New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentCont ...<br>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br> + CategoryInfo : NotSpecified: (:) [New-QADObject], ObjectNotFoundException<br> + FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException,Quest.A <br> ctiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.NewObjectCmdlet<br> <br>Name = External<br>Computer,<br>ParentContrainer : <br>New-QADObject : Cannot resolve directory object for the given identity: ''.<br>At line:8 char:2<br>+ New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentCont ...<br>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br> + CategoryInfo : NotSpecified: (:) [New-QADObject], ObjectNotFoundException<br> + FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException,Quest.A <br> ctiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.NewObjectCmdlet<br> <br>Name = Production,<br>ParentContrainer : <br>New-QADObject : Cannot resolve directory object for the given identity: ''.<br>At line:8 char:2<br>+ New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentCont ...<br>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br> + CategoryInfo : NotSpecified: (:) [New-QADObject], ObjectNotFoundException<br> + FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException,Quest.A <br> ctiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.NewObjectCmdlet</strong></blockquote>by MaxMad at 2013-02-27 06:11:26<blockquote>Stop working with split-path and use the properties directly. First example with the delimter &quot;Tab&quot; (t) worked.
Build your Variables with the properties of the objects after importing with import-csv. After using import-csv with the correct delimiter you can access each property by the property name you specified in the .csv header!

Import-Csv 'OrgUnits.csv' -Delimiter "`t" | Foreach-Object{
$ParentContainer = "ou=" + $.RootOU + ",ou=" + $.Environment
"ParentContrainer : $ParentContainer"

#New-QADObject -Name $Name -Type OrganizationalUnit -ParentContainer $ParentContainer -WhatIf
}
by danielson1 at 2013-02-27 06:47:25
Thanks for the tip on using the tab delim. Forgive me for being a bit dense here, but the $parentcontainer variable needs to change with each iteration correct? For instance the parent container for the "computer" ou in a.com/sharepoint/external/low will be ‘ou=low,…’

The problem for the script is that only the ‘Sharepoint’ ou has thus far been created. I need to create each ‘ou’ (External, Internal – then Moderate and Low for each, then Production, Staging, Dev and finally Computer and Service)