Reputation: 10679
Not sure if I'm doing this right, as it's only the 3rd powershell script I've ever written, so apologize for such a basic question.
I have a loop that needs to be run in two to three different areas. Rather than repeat the code over and over, I'd like to place it in a defined function and call from inside the script.
The function uses a simple switch
statement to get the description of a local group, then returns the string
variable holding the description.
The function seems to process fine, however after the return statement I get the following error message
Exception setting "description": "The following exception occurred while retrieving member "description": "The group name could not be found.
This is a pretty basic feature of Powershell, so I'm not sure what's going on.
Below is the complete script. The code in the try
block calls the function createGroupAndUsers
, which in turn calls the function addDescription
, which is where the error is occurring.
#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
}
Upvotes: 1
Views: 607
Reputation: 47792
The error is related to the way that you're calling it:
$objGroup.description = addDescription $groupName
Try instead to first call the function separately with the same input, and see what the output is.
Then try to set $objGroup.description
to a static string value and see what the result is.
Between those two you should get more information or an answer to what's going wrong.
Upvotes: 1