The member does not exist? Powershell script

1

I have been reading some questions and can find information on creating scripts but none seemingly using the object type "Computer".

Apologies if this is more suited to superuser. But this is still at the script level and as such I thought it would be best placed here.

Here is my script. I want to add a domain registered Server (Computer) to the performance monitor users group on a range of servers.

$ComputerName = Read-Host "Remote Computer name:"
$PmuGroup = [ADSI]"WinNT://$ComputerName/Performance Monitor Users,group"
$User = [ADSI]"WinNT://DOMAIN/ServerName,computer"
$PmuGroup.Add($User.Path) 

The following error is displayed:

Exception calling "Add" with "1" argument(s): "A member could not be added to 
or removed from the local group because
the member does not exist.
"
At line:1 char:1
+ $AdminGroup.Add($User.Path)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

If I enter $user into PS, it returns the Path and seemingly finds the machine.

Tried without .path as suggested in comments;

PS C:\WINDOWS> $PmuGroup.Add($User)
Exception calling "Add" with "1" argument(s): "Type mismatch. (Exception 
from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))"
At line:1 char:1
+ $PmuGroup.Add($User)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

Is this an error with my script / can anyone please advise on any errors in it? For info : Domain/ServerName is not what is being used. I have removed the actual name from here.

powershell
adsi
asked on Stack Overflow Feb 9, 2018 by user9338709 • edited Feb 9, 2018 by user9338709

1 Answer

1

The samAccountName of a computer account always ends with a $:

$ComputerName = Read-Host "Remote Computer name:"
$PmuGroup = [ADSI]"WinNT://$ComputerName/Performance Monitor Users,group"
$Computer = [ADSI]"WinNT://DOMAIN/ServerName$"
$PmuGroup.Add($Computer.Path) 
answered on Stack Overflow Feb 9, 2018 by Bacon Bits

User contributions licensed under CC BY-SA 3.0