New-ADUser:对象名称语法错误。 Powershell Docx 导入错误
New-ADUser : The object name has bad syntax. Powershell Docx import error
我正在尝试从 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
搜索后 google 似乎是因为我将文档导入脚本的方式导致了变量对象而不是字符串,但我不确定如何导入文档以便变量作为字符串导入。我试图使用 | Out-String 并且这并没有改变我的错误信息,所以 google 可能让我误入歧途。
确保您从 table 中读取的变量被修剪并且不为空($null)。
最好将 table 重新创建为 CSV,这样您可以更好地控制数据,而不必 fiddle 使用 Word.Application COM 对象。
我还建议使用 Splatting 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
我正在尝试从 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
搜索后 google 似乎是因为我将文档导入脚本的方式导致了变量对象而不是字符串,但我不确定如何导入文档以便变量作为字符串导入。我试图使用 | Out-String 并且这并没有改变我的错误信息,所以 google 可能让我误入歧途。
确保您从 table 中读取的变量被修剪并且不为空($null)。
最好将 table 重新创建为 CSV,这样您可以更好地控制数据,而不必 fiddle 使用 Word.Application COM 对象。
我还建议使用 Splatting 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