problem with my script

Welcome Forums General PowerShell Q&A problem with my script

This topic contains 7 replies, has 2 voices, and was last updated by

 
Participant
6 months, 1 week ago.

  • Author
    Posts
  • #98749

    Participant
    Points: 0
    Rank: Member

    Okay I have a bit of a problem, my script wont run.

    The purpose of the script is to check if the user is admin, if its not it wont do anything, but if he is admin it will create a new user according to certain parameters(sAMAccountName, Name, GivenName etc) that exists inside a csv file.

    Please help me understand whats the problem, try fix it for me or write it in another/better way
    Thanks in advance!!

    thats my script:

    function New-ADUsersFromCSV
    {
    PARAM ([STRING]$CSVpath)

    ################## cheking if user is a domain admin ###############################
    $user1=whoami
    $user1=$user1.split("/")[1]
    $admins= Get-ADGroupMember "domain admins" | select name -ExpandProperty name
    $isuserexist =$false
    foreach($a in $admins)
    {
    if($a -eq $user1)
    {
    $isuserexist=true
    break
    }
    }
    if($isuserexist -ne $false)
    {
    Write-Host"sorry you are not a domain admin"
    exit
    }
    ############################################## BODY ####################################
    $users=import-csv -Path $CSVPath
    foreach ($user in $users)
    {
    $sam = $User.samaccount
    $name = $User.name
    $givenname=$user.givenname
    $surname = $User.surname
    $enabled=$user.enabled
    $password=$user.password
    $changePass=$user.changePass
    $displayname = $User.displayname

    $accountpass=ConvertTo-SecureString -AsPlainText $password -Force

    ############## creating an array—–>transfer the string into a bool var. ###########
    $ok =@("YES","Yes","yes","TRUE","True","true")
    if($ok.Contains($enabled)){$enabled=$true} else {$enabled=$false}
    if($ok.Contains($changePass)){$changePass=$true} else {$changePass=$false}

    New-ADUser -SamAccountName $sam -GivenName $givenname -Surname $surname -Enabled `
    $enabled -AccountPassword $accountpass -ChangePasswordAtLogon $changePass -DisplayName $displayname

    }

    }
    # New-ADUsersFromCSV C:\csv\users.csv thats how i ran it through the server

  • #98754

    Participant
    Points: 9
    Rank: Member

    ... I have a bit of a problem, my script wont run.

    Next time you might be a little more precise in your "explanation". Your subject is way too vague as well. It should already tell a lttle bit a hint of your problem.

    This snippet will check if the user who runs the function is member of the group "domain admins" and runs code accordingly.

    If((Get-ADGroupMember "domain admins" | Select-Object -ExpandProperty name) -contains $($ENV:UserName)){
        'do the stuff you need to do'
    }
    Else{
        'No Admin'
    }

    It would be helpful as well if you format your code as code here in the forum, please.

  • #98757

    Participant
    Points: 0
    Rank: Member

    how do I format my code as code here in the forum?
    I'm sorry im pretty new around here πŸ˜€

    • #98760

      Participant
      Points: 9
      Rank: Member

      In the editor windows for your post you click on the "pre" tag you see above the text before your code and again afer your code. That will add the proper tags.

  • #98763

    Participant
    Points: 0
    Rank: Member
    function New-ADUsersFromCSV
    {
    PARAM ([STRING]$CSVpath)
    
    ################## cheking if user is a domain admin ###############################
    $user1=whoami
    $user1=$user1.split("/")[1]
    $admins= Get-ADGroupMember "domain admins" | select name -ExpandProperty name
    $isuserexist =$false
    foreach($a in $admins)
    {
    if($a -eq $user1)
    {
    $isuserexist=true
    break
    }
    }
    if($isuserexist -ne $false)
    {
    Write-Host"sorry you are not a domain admin"
    exit
    }
    ############################################## BODY ####################################
    $users=import-csv -Path $CSVPath
    foreach ($user in $users)
    {
    $sam = $User.samaccount
    $name = $User.name
    $givenname=$user.givenname
    $surname = $User.surname
    $enabled=$user.enabled
    $password=$user.password
    $changePass=$user.changePass
    $displayname = $User.displayname
    
    $accountpass=ConvertTo-SecureString -AsPlainText $password -Force
    
    ############## creating an array—–>transfer the string into a bool var. ###########
    $ok =@("YES","Yes","yes","TRUE","True","true")
    if($ok.Contains($enabled)){$enabled=$true} else {$enabled=$false}
    if($ok.Contains($changePass)){$changePass=$true} else {$changePass=$false}
    
    New-ADUser -SamAccountName $sam -GivenName $givenname -Surname $surname -Enabled `
    $enabled -AccountPassword $accountpass -ChangePasswordAtLogon $changePass -DisplayName $displayname
    
    }
    
    }
    # New-ADUsersFromCSV C:\csv\users.csv thats how i ran it through the server
    • #98764

      Participant
      Points: 9
      Rank: Member

      Great. Now you just have to adapt the snippet I posted before. πŸ˜‰ And you still did not tell what the actual problem is you have. πŸ˜‰

  • #98767

    Participant
    Points: 0
    Rank: Member

    I think im doin something wrong, can u fix the code with the code you made?
    It wouldnt create the user according to the csv file...
    Im writing

    New-ADUsersFromCSV C:\csv\users.csv
    

    and it just wont work

    Id really appreciate it!

    • #98854

      Participant
      Points: 9
      Rank: Member

      Sorry – No, I won't.

      This forum is not meant to debug your code for you. It's meant to help you debugging it by yourself.

      You still did not explain, what is not working? Do you get any error? If yes – please post it here (formatted as code as well please)!

The topic ‘problem with my script’ is closed to new replies.