Script flows not like expected

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Глеб Боушев Глеб Боушев 5 months, 3 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #37405

    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...

    #37436
    Profile photo of SeanQuinlan
    SeanQuinlan
    Participant

    When you declare the $args hashtable, one of the entries is Path = "OU=$path"

    However you only set $path after you have already declared the hashtable.

    Therefore, for the first user, $args.Path will be just "OU=". When you try use that on New-ADUser, it will give you an error. However, when it hits the 2nd user, $path will be set (to the value from the previous user), and so on.

    So this is probably the cause of your "script going crazy".

    You want to set the .Path property after you have set the $path variable.

    Adding a line like:

    $args.Path = "OU=$path"

    after the

    [string]$path=...

    line should do it.

    #37446

    Sorry, I forgot to edit this thing. That is not the problem anyway, since test-path was erroring out weirdly! Right now I'm doing it like this and it seem to work more consistently.

    # Create OU's and dept's
    foreach ($_ in $csv) {
        $dept += $_.department
        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"}
        if (!(test-path "AD:\OU=$($_.City),OU=$($_.DepTrue),OU=StaffRegions,OU=SibUsers,DC=xxx,DC=xxx"))
            {New-ADOrganizationalUnit -Name $_.City -Path "OU=$($_.DepTrue),OU=StaffRegions,OU=SibUsers,DC=xxx,DC=xxx"}
        if (!(test-path "AD:\OU=$($_.Department),OU=$($_.City),OU=$($_.DepTrue),OU=StaffRegions,OU=SibUsers,DC=xxx,DC=xxx"))
            {New-ADOrganizationalUnit -Name $_.Department -Path "OU=$($_.City),OU=$($_.DepTrue),OU=StaffRegions,OU=SibUsers,DC=xxx,DC=xxx"}
    }
    
    # Create User
    foreach ($_ in $csv) {
    
        [string]$path = "OU=$($_.Department),OU=$($_.City),OU=$($_.DepTrue),OU=StaffRegions,OU=SibUsers,DC=xxx,DC=xxx"
        $ADuser = Get-ADUser -Filter "sAMAccountName -eq '$($_.SamAccountName)'"
        $pwd = [System.Web.Security.Membership]::GeneratePassword(12,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 = "xxx"
            Path = "$path"
        }
    
        if ($ADuser) {
            "$(Get-Date -Format hh:mm:ss): User exists $ADUser" | Out-File c:\log.txt -Append
        }
    
        else {
            $usr = New-ADUser @args -PassThru | ForEach-Object {
                $_ | Set-ADAccountPassword -PassThru -Reset -NewPassword (ConvertTo-SecureString -Force `
                        -AsPlainText $pwd )
                $_ | Enable-ADAccount
                "$(Get-Date -Format hh:mm:ss): $($_.samaccountname) with   $pwd   created" | Out-File c:\log.txt -Append
    }}}
    
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.