Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Adding Local Group memberships through Powershell Script failing

$
0
0

Hi,

I have powershell script to add Domain User to Local Group on a remote computer. Script fails with unfamiliar error for Windows 2016 servers. Same script works for win 2008 and win 2012 servers.

I am failing to understand what is different in case of windows 2016 servers that this script failes to add a domain user and gives below error. Can you please point out if something is not right here.

Error Message is - 'You cannot call a method on a null-valued expression..Exception.Message'

Here is the script -

try{

(

$admgroup = New-ObjectSystem.DirectoryServices.DirectoryEntry("WinNT://HOSTNAME, computer","USERNAME","PASSWORD")).Children.Find("Remote Desktop Users","group")

$admgroup.psbase.Invoke("Add",("DOMAIN USERNAME"))

}catch{

Write-Warning"Exception in Adding AD Account to group `'$_.Exception.GetType().FullName`', `'$_.Exception.Message`'"

echo$_.Exception|format-list-force 

echo$_.Exception.GetType().FullName,$_.Exception.Message

}


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>