Runs in shell but not as exe?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
proshot78
Posts: 14
Last visit: Wed Aug 09, 2023 3:39 pm
Answers: 1
Has voted: 3 times

Runs in shell but not as exe?

Post by proshot78 »

I've been pulling my hair out over this one!
I have a chunk of code:

Code: Select all

$UserADObject = Get-ADUser $UserID -properties *
$UserADObject.memberof | ForEach-Object {
	$Group = Get-ADGroup $_
	Remove-ADGroupMember -Identity $Group.name -Members $UserID -Confirm:$False
}
$MemberList | ForEach-Object {
	$Group = Get-ADGroup $_
	Add-ADGroupMember -Identity $Group.name -Members $UserID -Confirm:$False
}
The goal is to remove all groups a user is a member of, and then add the user to a list of other groups. If you copy and paste that block of text, and define $UserID and [System.Array]$Memberlist, it runs just fine in shell. That same chunk of code throws errors in my compiled project:

Code: Select all

Get-ADGroup: Cannot bind parameter 'Identity'. Cannot convert value "CN=Account Operators,CN=Builtin,DC=my,DC=domain,DC=name" to type "Microsoft.ActiveDirectory.Management.ADGroup". Error: "Cannot convert the "CN=Account Operators,CN=Builtin,DC=my,DC=domain,DC=name" value of type "Deserialized.Microsoft.ActiveDirectory.Management.ADGroup" to type "Microsoft.ActiveDirectory.Management.ADGroup"."
Add-ADGroupMember: Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
This topic is 1 year and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked