powershell - New-ADUser :对象名称的语法错误。Powershell Docx 导入错误
问题描述
我正在尝试从 word 文档创建新的 Active Directory 用户。但是我在创建用户的过程中遇到了一个问题,其中脚本说“对象名称有错误的语法”我已将问题缩小到下面代码的“New-ADUser:”部分
# Import active directory module for running AD cmdlets
Import-Module activedirectory
#Create word application object
$word = New-Object -ComObject Word.Application
#Assign document path
$documentPath = Read-host => [Enter Template to use Ex:"C:\Users\username\Desktop\employeeform.docx"]
#open the document
$document = $word.Documents.Open($documentPath)
#list all tables in doc
$document.Tables | ft
#get info from a certain part of the table
$pager = $document.Tables[1].Cell(4,2).range.text
$fname = $document.Tables[1].Cell(6,2).range.text
$lname = $document.Tables[1].Cell(8,2).range.text
$fn1 = $fname.Substring(0,1)
$username = "$fn1$lname"
$jobtitle = $document.Tables[1].Cell(15,2).range.text
$department = $document.Tables[1].Cell(16,2).range.text
$manager = $document.Tables[1].Cell(17,2).range.text
$pagernumber = $pager.Substring(17)
$template = Read-host => [Enter Template to use Ex:MedicalAssistant]
#folder = $User.folder
Write-Output $documentPath
Write-Output $template
#Check to see if the user already exists in AD
if (Get-ADUser -F {SamAccountName -eq $username})
{
#If user does exist, give a warning
Write-Warning "A user account with username $username already exist in Active Directory."
pause
}
else
{
#User does not exist then proceed to create the new user account
New-ADUser `
-SamAccountName $username `
-Name "$fname $lname" `
-GivenName $fname `
-Surname $lname `
-Enabled $True `
-DisplayName "$lname $fname" `
-Company "Companyname" `
-AccountPassword (convertto-securestring "Password" -AsPlainText -Force)`
-HomeDrive "X:" `
-ScriptPath "K32.exe" `
-OtherAttributes @{pager=$pagernumber} `
-Title $jobtitle `
-Department $department `
-Description $jobtitle `
#-Manager $manager `
#-HomeDirectory $folder `
}
#Close the document
$document.close()
#Close Word
$word.Quit()
pause
在搜索谷歌之后,似乎出现了这个问题,因为我如何将文档导入到脚本中,这使得变量对象而不是字符串,但我不确定如何导入文档以便变量作为字符串导入。我试图使用 | Out-String,这并没有改变我的错误信息,所以谷歌可能让我误入歧途。
解决方案
确保您从表中读取的变量已修剪且不为空 ($null)。
最好将该表重新创建为 CSV,您可以在其中更好地控制数据,而不必摆弄 Word.Application COM 对象。
另外,我建议使用将参数喷溅到 New-ADUser,这样您就可以摆脱使用所有那些讨厌的反引号(当它后面有换行符以外的任何内容时,这会给您带来错误..)
$userProps = @{
SamAccountName = $username
Name = "$fname $lname"
GivenName = $fname
Surname = $lname
Enabled = $True
DisplayName = "$lname $fname"
Company = "Companyname"
AccountPassword = (ConvertTo-SecureString "Password" -AsPlainText -Force)
HomeDrive = "X:"
ScriptPath = "K32.exe"
OtherAttributes = @{pager=$pagernumber}
Title = $jobtitle
Department = $department
Description = $jobtitle
# Manager = $manager
# HomeDirectory = $folder
}
New-ADUser @userProps
推荐阅读
- pandas - 如何加入具有相同数据的 2 个数据框?
- javascript - 改变在有效载荷中传递的 Vuex 数组
- python - 如何将文件中的每一行存储在单独的数组中?
- javascript - 未捕获的类型错误:使用 wordpress 激活站点后无法读取未定义的属性“样式”
- json - discord.py 中的 JSON
- sql - 为什么 CHARINDEX 返回 0?
- swift - SwiftUI 切换案例使用 TextField 不同的值
- java - 如何使用数组创建方法?
- javascript - 为什么我的玩家会与整个 y 块而不是 y 块 258 和 282 发生碰撞?
- spring - 通过对象属性搜索