Add-pssnapin Quest.ActiveRoles.ADManagement
# cls # $TelSPR="C:\info2AD\.xls" # (WorkSheet) Excel $SheetName="1" #"" Excel ( COM- Excel.Application) $objExcel=New-Object -comobject Excel.Application # (" ") Excel $objWorkbook=$objExcel.Workbooks.Open($TelSPR) # , $ColumnName=1 # , $ColumnTitle=2 # $ColumnHomePhone=3 $ColumnPhone=4 $ColumnMobPhone=5 # $ColumnOffice=6 $ColumnMail=7 # $ColumnDep=8 # # SpecialCells $xlCellTypeLastCell = 11 # # #$TotalsRow=$objWorkbook.Worksheets.Item($SheetName).UsedRange.SpecialCells($xlCellTypeLastCell).CurrentRegion.Row # Excel for ($Row=1;$Row -le $TotalsRow; $Row++) { # $UserName=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnName).Value() $Title=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnTitle).Value() $HomePhone=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnHomePhone).Value() $Phone=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnPhone).Value() $MobPhone=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnMobPhone).Value() $Office=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnOffice).Value() $Mail=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnMail).Value() $Department=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnDep).Value() # , AD $arrfio =-split $UserName $arrfio[2]=$arrfio[2].Substring(0,1) # if ($Phone -ne $null) { $Phone="(888) "+$Phone } if ($MobPhone -ne $null) { $MobPhone="(888) "+$MobPhone } # AD, (enabled), try { Get-QADUser -DisplayName $UserName -enabled | Set-QADUser -FirstName $arrfio[1] -Initials $arrfio[2] -LastName $arrfio[0] -Department $Department -HomePhone $HomePhone -Phone $Phone -MobilePhone $MobPhone -Office $Office -Title $Title -Company " " -Mail $Mail } catch { $ReportString=("{0,-50} <-> {1,50}" -f $UserName, " ") } write-Host $reportString $reportString=" " } # Excel $objExcel.Workbooks.Close() # Excel ( Excel) $objExcel.Quit() # $objExcel = $null # [gc]::collect() [gc]::WaitForPendingFinalizers()
################################################################################## param([string]$path, [string]$url,[string]$ou,[switch]$help) [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") # function GetHelp() { $HelpText = @" : . ActiveDirectory *.csv . csv SharePoint' : * sn - * givenName - * DisplayName - (...) * mail - E-mail * LogonName - * telephoneNumber - * Office - * Department - * Title - * HomePhone - ( ) * MobilePhone - -------------------------------------------------------------------------------- : -path csv , -url http , -ou Organization unit AD, , : OU=accaunts- . OU=cometa - -------------------------------------------------------------------------------- : PowerShell: .\ImportADUsersToSPList.ps1 -path "Your path" -url "List URL" -ou "OU=cometa" : .\ImportADUsersToSPList.ps1 -help -------------------------------------------------------------------------------- : Windows: powershell .\ImportADUsersToSPList.ps1 -path "Your path" -url "List URL" -ou "OU=Magadan" : powershell .\ImportADUsersToSPList.ps1 -help -------------------------------------------------------------------------------- "@ $HelpText } function nz($value) { if ($value -eq $null) { $value="" } return $value } function UpdateList([string]$path,[string]$url,[string]$ou) { # : $site=New-Object Microsoft.SharePoint.SPSite($url) $web=$site.OpenWeb() $list=$web.GetList($url) $csv_file=Import-Csv -Delimiter ";" $path $listItems=$list.Items $spFiledType=[Microsoft.SharePoint.SPFieldType]::Text ######################################################### # csv : # : "Script started at:" | Out-File -encoding default ".\UpdateUsers.log" -Append Get-Date | Out-File -encoding default ".\UpdateUsers.log" -Append foreach ($line in $csv_file) { $update=$false if (!($item=$list.Items | where {$_["Sid"] -eq $line.Sid})) { # if (!(select-string -pattern "88" -inputobject $line.givenName) -and (!(select-string -pattern "Admin" -inputobject $line.givenName)) -and (!(select-string -pattern "Operator" -inputobject $line.givenName))-and ((select-string -pattern $ou -inputobject $line.DN))) { Write-Output $line $item=$list.Items.Add() $item["Sid"]=$line.Sid $update=$true } else {continue} } [array]$sids+=$line.Sid $t=$line.DisplayName -split " ",3 if ((nz($item[""])) -ne (nz($t[0]))) { $item[""]=$t[0] $update=$true } if ((nz($item[""])) -ne (nz($t[1]))) { $item[""]=$t[1] $update=$true } if ((nz($item[""])) -ne (nz($t[2]))) { $item[""]=$t[2] $update=$true } $tfioname=$t[1] -split "",3 $tfioMidName=$t[2] -split "",3 $fio=$t[0]+" "+$tfioname[1]+"."+$tfioMidName[1]+"." if ((nz($item["..."])) -ne (nz($fio))) { $item["..."]=$fio $update=$true } $fio=$tfioname[1]+"."+$tfioMidName[1]+". "+$t[0] if ((nz($item["..."])) -ne (nz($fio))) { $item["..."]=$fio $update=$true } if ((nz($item["E-mail"])) -ne (nz($line.mail))) { $item["E-mail"]=$line.mail $update=$true } if ((nz($item[""])) -ne (nz($line.LogonName))) { $item[""]=$line.LogonName $update=$true } if ((nz($item[""])) -ne (nz($line.telephoneNumber))) { $item[""]=$line.telephoneNumber $update=$true } if ((nz($item[""])) -ne (nz($line.Office))) { $item[""]=$line.Office $update=$true } if ((nz($item[""])) -ne (nz($line.Department))) { $item[""]=$line.Department $update=$true } if ((nz($item[""])) -ne (nz($line.Title))) { $item[""]=$line.Title $update=$true } if ((nz($item[""])) -ne (nz($line.HomePhone))) { $item[""]=$line.HomePhone $update=$true } if ((nz($item["DECT"])) -ne (nz($line.MobilePhone))) { $item["DECT"]=$line.MobilePhone $update=$true } if ($update -eq $true) { $item.Update() } } $listItems=$list.Items for ($x=$listItems.Count-1; $x -ge 0; $x--) { if (($sids | where {$_ -eq $listItems[$x]["Sid"] }) -eq $null) { $notify=" : "+ $listItems[$x][""].ToString() $notify | Out-File -encoding default ".\UpdateGUUsers.log" -Append $listItems[$x].Recycle() } } "Script finished at:" | Out-File -encoding default ".\UpdateGUUsers.log" -Append Get-Date | Out-File -encoding default ".\UpdateGUUsers.log" -Append "______________________________" | Out-File -encoding default ".\UpdateGUUsers.log" -Append $site.Dispose() } if($help) { GetHelp; Continue } if($path -AND $url -AND $ou) { UpdateList -path $path -url $url -ou $ou}
Source: https://habr.com/ru/post/168997/