none
Powershell C#- Loop through a csv file and take action RRS feed

  • Question

  • I am not a programmer, just trying to grab code from here and there to enable or disable skypeB users in bulk to make the job a bit easier and faster.

    wrote the code below, but lines 41-43 are not working in the script. They work individually if I run them from the ps> prompt.

    Any help will be appreciated. Script is still not completed do do other things as well. Hopefully when completed, I can post the final script here :?) don't pay any attention to the commented lines, that is just information to keep me calm...

    #sAMAccountName,mail,msRTCSIPLine,ConferencingPolicy,RegistrarPool,LineURI,sipaddress,action

    $UserList = Import-CSV "$PSScriptRoot\Skypebuserlist.csv"

    #$category = "user"
    #$name = "uid"
    #$data = "sharifm*"
    #$attributes = @("givenName", "mail", "manager")
    #$attributes = @("mail","givenName","sn","msRTCSIP-Line","uid")
    #$attribute ="givenName"

    ##$Header =  $attributes -join','
    #Write-Host $Header
    #$lines = @()
    #$lines += $Header

    #$results = @()

    foreach ($user in $UserList) {
        Write-Host ("The User {0} with Phone ({1}) and Action = {2}" -f $user.sAMAccountName, $user.mail, $user.RegistrarPool, $user.sipaddress, $user.ConferencingPolicy, $user.msRTCSIPLine, $user.action)

        $poolName = (Get-CsUser | Group-Object RegistrarPool | sort-object count | select-object -first 1).Name

        if ($user.action -eq "disable")
        {
            Write-Host "disable account"
            Write-Host Get-CsUser $user.sAMAccountName
            if (Get-CsUser $user.sAMAccountName){
                disable-CsUser $user.sAMAccountName
            }
            else
            {
                Write-Host "not found"
            }
        }
        else
        {
            Write-Host ("Enable account for POOL {0} " -f $poolName)
            if ($user.msRTCSIPLine) {
                Write-Host "has phone"
              Enable-CsUser -Identity "$user.sAMAccountName" -RegistrarPool "$poolName" -SipAddress "sip:$user.sipaddress"
              Set-CsUser $user.sAMAccountName -EnterpriseVoiceEnabled $True -Confirm:$True
              Set-csuser $user.sAMAccountName -LineURI $user.msRTCSIPLine
                    
            }
            else{
                Write-Host "does not have phone"
            }

        }
        Write-Host "-----------------------------------------------------------------------------"
    }
    Write-Host
    Write-Host "File Done"
    Write-Host

    Saturday, June 25, 2016 3:28 PM

All replies

  • Hi,

    First of all, you're posting in the wrong forum, but I am not a moderator. :-)

    The root cause should be line 41, as line 42 and 43 only work, when the user is enabled. I think that you problem is, that the variables are not correctly evaluated during script execution. To troubleshoot this, you could use the debugger of PowerShell ISE. 

    Please try the following for line 41:

    Enable-CsUser -Identity "$($user.sAMAccountName)" -RegistrarPool "$($poolName)" -SipAddress "sip:$($user.sipaddress)"


    Best Regards, Sebastian

    Thursday, July 7, 2016 8:45 AM