Answer the question
In order to leave comments, you need to log in
Why does the script swear on EXEL?
Good day, I'm trying to write a script that would fill in data on users in AD from an EXEL file that was prepared in advance. In the course of the first attempts, I still somehow worked out, but confused the positions in places (I figured it out), now he simply does not work out.
The script itself looks like:
cls
Import-Module activedirectory
Add-pssnapin Quest.ActiveRoles.ADManagement
$TelSPR="C:\PS_TEST\list1.xlsx"
#Имя листа (WorkSheet) рабочей книги Excel
$SheetName="EXAMPLE"
#"Запускаем" Excel (создаем COM-объект Excel.Application)
$objExcel=New-Object -comobject Excel.Application
#выполняем открытие файла ("Рабочей книги") в Excel
$objWorkbook=$objExcel.Workbooks.Open($TelSPR)
$ColumnUserLogonName=1
$ColumnFirstName=3
#Номер колонки, содержащей Имя
$ColumnSecondName=2
#номер колонки с фамилией
$ColumnHomePhone=7
#номер телефона личный
$ColumnCity=5
#номер колонки с городом
$ColumnRegion=6
#номер колонки с регионом
$ColumnMobilePhone=8
#номер телефона рабочий
$ColumnRoom=9
#Комната
$ColumnDep=4
#Департамент
#Константа для использования с методом SpecialCells
$xlCellTypeLastCell=12
$TotalsRow=$objWorkbook.Worksheets.Item($SheetName).UsedRange.SpecialCells($xlCellTypeLastCell).CurrentRegion.Row
#Выполняем перебор строк в открытом файле Excel
for ($Row=1;$Row -le $TotalsRow; $Row++) {
#Сохраняем в переменных значения соответствующих ячеек
$UserLogonName=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnUserLogonName).Value()
$UserFirstName=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnFirstName).Value()
$UserSecondName=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnSecondName).Value()
$HomePhone=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnHomePhone).Value()
$MobilePhone=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnMobilePhone).Value()
$Region=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnRegion).Value()
$Department=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnDep).Value()
$Room=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnRoom).Value()
$City=$objWorkbook.ActiveSheet.Cells.Item($Row, $ColumnCity).Value()
#Пишем данные в AD, если пользователь включен (enabled), включаем обработку ошибок
try {
Get-QADUser -DisplayName $UserLogonName -enabled | Set-QADUser -FirstName $FirstName -SecondName $SecondName -Department $Department -City $City -Region $Region -HomePhone $HomePhone -MobilePhone $MobilePhone -Room $Room -Company "NAME"
}
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()
Answer the question
In order to leave comments, you need to log in
I don't think it's a turnaround. This error is often complained about on Excel forums. and if you do the same, but use a csv file instead of an excel file?
please provide the full text of the error. there will be indicated the line number on which the error occurs. The image loaded finally, I see an error.
It will be problematic for me to run this code at home, so the reasoning will be purely theoretical.
judging by the text of errors you gave not all code of a script.
I've had trouble with activesheet a couple of times. try explicitly specifying which sheet you are referring to.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question