Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Cannot import OUs & Users with Powershell ; Object not Found error?

$
0
0

Hello,

This is my first time working with PowerShell so bear with me.

I use Server 2008 R2 with AD.

I got 2 scripts the first one to add the OUs i need and the second one to add the users to the OUs from a CSV file.

Oulist.csv consists of Name,Path and Description.

Here is the PS script for it;

Import-Module ActiveDirectory 
$OUs = Import-Csv -Delimiter ";" -Path ".\OUslist.csv"  

foreach ($OU in $OUs)  
{  
    $Name = $OU.Name
	$Path = $OU.Path
	$Description = $OU.Description

	New-ADOrganizationalUnit -Name $Name -Path $Path -Description $Description
} 
  
 

The Userlist.csv consists of Name,Surname,SamName,Password,Group paths etc. all the good stuff.

Import-Module ActiveDirectory 
$Users = Import-Csv -Delimiter ";" -Path ".\userslist.csv"  
$GroupPath = "OU=Groups,OU=LAB,DC=lab,DC=org"

foreach ($User in $Users)  
{  
    $GroupEx0=$Null
	$GroupEx1=$Null
	$GroupEx2=$Null
	$GroupEx3=$Null
	
	$GivenName = $User.GivenName
	$Surename = $User.Surename
	$SamAccountName = $User.SamAccountName
	$DisplayName = $User.DisplayName
	$AccountPassword = (ConvertTo-SecureString $User.AccountPassword -AsPlainText -Force)
	$Path = $User.Path
	$Description=$User.Description
	$Office=$User.Office
	$OfficePhone=$User.OfficePhone
	$EmailAddress=$User.EmailAddress
	$HomePage=$User.HomePage
	$UserPrincipalName=$User.UserPrincipalName
	$PasswordNeverExpires=$true
	$ChangePasswordAtLogon=$false
	$MobilePhone=$User.MobilePhone
	$Title=$User.Title
	$Department=$User.Department
	$Company=$User.Company
	$EmployeeID=$User.EmployeeID
	$Group0=$User.Group0
	$Group1=$User.Group1
	$Group2=$User.Group2
	$Group3=$User.Group3
	
    New-ADUser -Name $DisplayName -DisplayName $DisplayName -SamAccountName $SamAccountName -GivenName $GivenName -Surname $Surename -AccountPassword $AccountPassword -Path $Path -Description $Description -Office $Office -Officephone $OfficePhone -EmailAddress $EmailAddress -HomePage $HomePage -UserPrincipalName $UserPrincipalName -PasswordNeverExpires $PasswordNeverExpires -ChangePasswordAtLogon $ChangePasswordAtLogon -MobilePhone $MobilePhone -Title $Title -Department $Department -Company $Company -EmployeeID $EmployeeID -Enabled $true


	If ($Group0 -ne "NONE")
	{
		$GroupEx0 = Get-ADGroup -Filter {SAMAccountName -eq $Group0}
		If ($GroupEx0 -eq $Null)
		{
			New-ADGroup -Name $Group0 -DisplayName $Group0 -GroupScope 1 -GroupCategory Security -SamAccountName $Group0 -Path $GroupPath
			Add-ADGroupMember -Identity $Group0 -Members $SamAccountName 
		}
		Else
		{
			Add-ADGroupMember -Identity $Group0 -Members $SamAccountName 
		}
	}
	If ($Group1 -ne "NONE")
	{
		$GroupEx1 = Get-ADGroup -Filter {SAMAccountName -eq $Group1}
		If ($GroupEx1 -eq $Null)
		{
			New-ADGroup -Name $Group1 -DisplayName $Group1 -GroupScope 1 -GroupCategory Security -SamAccountName $Group1 -Path $GroupPath
			Add-ADGroupMember -Identity $Group1 -Members $SamAccountName 
		}
		Else
		{
			Add-ADGroupMember -Identity $Group1 -Members $SamAccountName 
		}
	}
	If ($Group2 -ne "NONE")
	{
		$GroupEx2 = Get-ADGroup -Filter {SAMAccountName -eq $Group2}
		If ($GroupEx2 -eq $Null)
		{
			New-ADGroup -Name $Group2 -DisplayName $Group2 -GroupScope 1 -GroupCategory Security -SamAccountName $Group2 -Path $GroupPath
			Add-ADGroupMember -Identity $Group2 -Members $SamAccountName 
		}
		Else
		{
			Add-ADGroupMember -Identity $Group2 -Members $SamAccountName 
		}
	}

	If ($Group3 -ne "NONE")
	{
		$GroupEx3 = Get-ADGroup -Filter {SAMAccountName -eq $Group3}
		If ($GroupEx3 -eq $Null)
		{ 
			New-ADGroup -Name $Group3 -DisplayName $Group3 -GroupScope 1 -GroupCategory Security -SamAccountName $Group3 -Path $GroupPath
			Add-ADGroupMember -Identity $Group3 -Members $SamAccountName 
		}
		Else
		{
			Add-ADGroupMember -Identity $Group3 -Members $SamAccountName 
		}
	}

	
} 

When is run them under my Server i get Red Text with the Error; Object Not found

Thanks



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>