none
conditional statement not able to execute properly RRS feed

  • Question

  • hello All,

    I have a data csv sheet where information as follows, using below information I need to update AD account attributes based on below conditions  

    samaccountname,Othertelephone,language,employeeId

    abcd                      XXXXXXXXX     EN         SMS

    Now I need to check following conditions:

    Othertelephone =  if this should not be blank ,if so display message " filed is blank " and no changes should allowed in further attributes and  it should abort

    language= this field should only contain  EN or FR value if No display msg " error in language field " and no further changes to  the user attributes and it should abort

    employeeID= this field should only contain OTP or SMS value if Not filled display msg " error in Employee ID field " No further changes to the user attributes and it should abort

    changes to user will permit  when all attributes is filled. I do the testing taking samaccountname , othertelephone and employeeId into consideration but it did not helped. Getting error

    THIS is complete Code Of my Task where you need my focus on conditions

    group=Get-QAdGroup -SearchRoot  "domain/vpn group"

    Import-Csv D:\VPN.csv |

    ForEach-Object{


    if ($_.samaccountname -eq "")
    {

       Write-Host "SAMACCOUNTNAME is blank"   -fore red
    }

    else
    {
     $user=Get-QAduser $_.samaccountname

                
    if ($user.memberof -contains  $group.DN)
    {

                      
     Write-Host "$($_.Samaccountname) user is allready a member" -fore red

    }
    else
    {
     Add-QADGroupMember $group $_.Samaccountname
    }

    If ($_.othertelephone -eq "")

    {     

    Write-Output "$($_.samaccountname) telephone Number is blank"

    {     

    else
     {


    if ($_.EmployeeID -notmatch 'OTP' -and 'SMS')

    {      

    Write-Output "$($_.samaccountname) EmployeeID field is not correctly field")

    }  

    Else
    {

    Set-QADUser $_.SamAccountName -ObjectAttributes @{telephonenumber=$_.othertelephone;EmployeeID=$_.EmployeeID}

    }}
    }

    error

    Set-QADUser : Access is denied.

    At C:\Users\g512263\AppData\Local\Temp\5f8facb6-f942-4c3d-b924-8953d9a706da.ps1:37 char:8

    +                    Set-QADUser $_.SamAccountName -ObjectAttributes @{telephonenumber=$_.othe ...

    +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [Set-QADUser], UnauthorizedAccessException

        + FullyQualifiedErrorId : System.UnauthorizedAccessException,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.SetUserCm

       dlet

     

    Friday, November 7, 2014 2:08 PM

Answers

  • Hello,

    every body's urgent help would be appreciated. I am stuck here.

    many thanks in advance.

    Regards,

    • Marked as answer by STscripter Wednesday, November 12, 2014 3:35 PM
    Monday, November 10, 2014 9:12 AM

All replies

  • I didn't even bother looking at this logic of your script since you're getting an access is denied error message. You should get this resolved before moving on to ensuring your conditional logic is working. Are you running your script in the console or the ISE, and do you right-click and choose Run as administrator (even if you're logged in as a local administrator) to run it? You need to ensure you can run the Set-QADUser cmdlet without error before you use it in a script.
    Friday, November 7, 2014 4:15 PM
  • perhaps this is not right issue , I have right and from same console I can change all ad account attributes perfectly with another script

    in the same script if slightly change the conditions it works fine lets say

    if I put like below same script work like a charm

    If ($_.othertelephone -ne "") 
          {
            Set-QADUser $user.SamAccountName -ObjectAttributes @{telephonenumber=$_.othertelephone}
          } else
              {
              Write-Output "$($_.samaccountname) phonenumber is blank"
              }

    If ($_.EmployeeID -ne "")
          {
            Set-QADUser $_.SamAccountName -ObjectAttributes @{EmployeeID=$_.EmployeeID}
          }else
              {
            Write-Output "$($_.samaccountname) EmployeeID field is blank"
              }

    but whenever I use conditions like my first post it give me access denies error. I am using Quest Power shell

    your inputs are highly appreciated

    Friday, November 7, 2014 4:36 PM
  • You need to re-write the script in a more readable format. Something like:

    $Group = Get-QAdGroup -SearchRoot  "domain/vpn group"
    
    Import-Csv D:\VPN.csv |
    
        ForEach-Object{
    
            if ($_.samaccountname -eq "") {
                Write-Host "SAMACCOUNTNAME is blank"   -fore red 
            } else {
                $user=Get-QAduser $_.samaccountname
            }
                 
            if ($user.memberof -contains  $group.DN) {
                Write-Host "$($_.Samaccountname) user is allready a member" -fore red
            } else {
                Add-QADGroupMember $group $_.Samaccountname
            }
    
            If ($_.othertelephone -eq "") {     
                Write-Output "$($_.samaccountname) telephone Number is blank"
            } else {
                if ($_.EmployeeID -notmatch 'OTP' -and $_.EmployeeID -notmatch 'SMS') {      
                    Write-Output "$($_.samaccountname) EmployeeID field is not correctly field"
                } Else {
                    Set-QADUser $_.SamAccountName -ObjectAttributes @{telephonenumber=$_.othertelephone;EmployeeID=$_.EmployeeID}
                }
            }
        }
    Is this what your script supposed to look like?


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    Friday, November 7, 2014 6:38 PM
  • Hello ,

    same error access denied , I tied all the conations but same error  

    Set-QADUser : Access is denied.

    At C:\Users\g512263\AppData\Local\Temp\5f8facb6-f942-4c3d-b924-8953d9a706da.ps1:37 char:8

    +                    Set-QADUser $users.SamAccountName -ObjectAttributes @{telephonenumber=$_. ...

    +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [Set-QADUser], UnauthorizedAccessException

        + FullyQualifiedErrorId : System.UnauthorizedAccessException,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.SetUserCm

       dlet

    Monday, November 10, 2014 7:17 AM
  • Hello,

    every body's urgent help would be appreciated. I am stuck here.

    many thanks in advance.

    Regards,

    • Marked as answer by STscripter Wednesday, November 12, 2014 3:35 PM
    Monday, November 10, 2014 9:12 AM