Hello folks, I’m stuck again ![]()
foreach ($_ in $csv) {
if (!(Test-Path "AD://OU=$($_.depTrue),OU=StaffRegions,OU=SibUsers,DC=xxx,DC=xxx")) {
New-ADOrganizationalUnit -Name $_.DepTrue -Path "OU=StaffRegions,OU=SibUsers,DC=xxx,DC=xxx"
}
[string]$path = "$($_.City),OU=$($_.DepTrue),OU=StaffRegions,OU=SibUsers,DC=xxx,DC=xxx"
$ADuser = Get-ADUser -Filter "sAMAccountName -eq '$($_.SamAccountName)'"
$pwd = [System.Web.Security.Membership]::GeneratePassword(10,2)
$args = @{
DisplayName = $_.DisplayName
GivenName = $_.GivenName
Surname = $_.sn
SamAccountName = $_.SamAccountName
UserPrincipalName = $($_.SamAccountName + "@xxx.xxx")
Department = $_.DepTrue
Title = $_.TitleTrue
City = $_.City
Office = $_.Office
MobilePhone = $_.MobilePhone
OfficePhone = $_.telephoneNumber
Name = $_.DisplayName
Company = "whatever"
Path = "OU=$path"
}
if ($ADuser) {
"$(Get-Date -Format hh:mm:ss): User exists $ADUser" | Out-File c:\log.txt -Append
}
else {
if (Test-Path "AD://OU=$path") {
New-ADOrganizationalUnit -Name $_.Department -Path "OU=$($_.DepTrue),OU=StaffRegions,OU=SibUsers,DC=xxx,DC=xxx"
}
New-ADUser @args -PassThru |
Omitted Part of the script, since it doesn't run anyway.
}
And it works just fine, sometimes
Sometimes it never reaches New-ADUser and generates weird errors:
The PowerShell instance is not in the correct state for creating a nested PowerShell instance. Nested PowerShell instances should only be created in a running PowerShell instance.
At C:\Users\testo\Desktop_.ps1:43 char:13
-
if (Test-Path "AD://OU=$path")
While debugging it seems that script reaches test-path (second one), but after that it goes crazy. Oh and ise won’t let me do anything after that. Ise shows script as running, nothing happens and I can’t stop it with ctrc+c or shift+f5 or with the red button in ise.
If I do Test-Path “AD://OU=$path” in debugger right before script “dies” it correctly returns false.
Also spotted this error sometimes:
Specified argument was out of the range of valid values.
Parameter name: length
At C:\Users\testo\Desktop_.ps1:47 char:9
that’s new-aduser after test-path
forgot to mention, that sometimes script does indeed work as expected, sometimes only half of the script works…