不确定我是否正确行事,因为它只是我写过的第3版powershell脚本,所以为这样一个基本问题道歉。
我有一个需要在两到三个不同区域运行的循环。我不是一遍又一遍地重复代码,而是将它放在一个已定义的函数中并从脚本内部调用。
该函数使用简单的switch
语句来获取本地组的描述,然后返回包含描述的string
变量。
函数似乎处理正常,但是在return语句之后我得到以下错误消息
异常设置“description”:“检索成员时出现以下异常”说明“:”找不到组名。
这是Powershell的一个非常基本的功能,所以我不确定发生了什么。
以下是完整的脚本。 try
块中的代码调用函数createGroupAndUsers
,后者又调用函数addDescription
,这是发生错误的地方。
#HERE IS THE FUNCTION THROWING THE ERROR
Function addDescription($groupName)
{
switch($groupName)
{
"WebDevelopers" {$description = "Developers for this Web Server. They have RDP rights and update rights to the webcontent share." }
"AgentPaymentUpdater" {$description = "Used by OKeeffe WCF Web Service to update DSS"}
"AgencyCloneRequestor" {$description = "Used by OKeeffe WCF Web Service to clone agent data from DSS"}
"WynsureServiceInquiry"{$description = "Used by AgentInformationService WCF Web Service to query DSS"}
"BundleInquiry" {$description = "Used by AgentInformationService WCF Web Service to query DSS"}
"BundleUpdate" {$description = "Used by AgentInformationService WCF Web Service to query DSS"}
"DistributorInquiry" {$description = "Used by AgentInformationService WCF Web Service to query DSS"}
}
#return statement throws exception
return $description
}
#ABOVE FUNCTION CALLED BY THIS ONE
Function createGroupAndUsers($groupName, $computerName)
{
#clear variables from previous run
if($objGroup)
{
Clear-Variable $objGroup
}
if($Members)
{
Clear-Variable $Members
}
if($MemberName)
{
Clear-Variable $MemberName
}
if($tempArray)
{
Clear-Variable $tempArray
}
#create group and add description
$objGroup = [ADSI]"WinNT://localhost/$groupName"
if(-not $objGroup)
{
Write-Host "Creating Local Group $groupName" -ForegroundColor green
$objGroup = $cn.Create("Group", $groupName)
$objGroup.setinfo()
}
Write-Host "Setting Description for Local Group $groupName" -ForegroundColor green
$objGroup.description = addDescription $groupName
$objGroup.setinfo()
#pull group members from remote machine
$Members = Get-ADGroupMember -Identity $groupName -Server $computerName
$Members | ForEach-Object {$MemberNames += $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) + ",";}
$tempArray = $MemberNames -split ","
foreach($member in $tempArray)
{
Write-Host "Adding $member as user for Local Group $groupName" -ForegroundColor green
$objGroup.Add("WinNT://DOMAIN/$member, user")
}
}
#'MAIN' BLOCK OF CODE THAT CALLS ABOVE FUNCTIONS
try{
#Periscope and OKeeffe
$computerName = "DEVWB37"
$groupArray = @("WebDevelopers", "AgentPaymentUpdater", "AgencyCloneRequestor")
foreach($groupName in $groupArray)
{
createGroupAndUsers $groupName $computerName
}
}
catch{
write-host "Caught an exception:" -ForegroundColor Red
write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
}
答案 0 :(得分:1)
错误与您调用它的方式有关:
$objGroup.description = addDescription $groupName
首先尝试使用相同的输入分别调用该函数,并查看输出是什么。
然后尝试将$objGroup.description
设置为静态字符串值,看看结果是什么。
在这两者之间,您应该获得更多信息或回答出现问题。