Wayne
Wayne

Reputation: 11

I am trying to run a batch command from PowerShell

I have run into a character limitation for net.exe

net localgroup administrators "domainname\G-%COMPUTERNAME%-LocalAdmins" /add

I have been told you can run this through PowerShell, but I have been unsuccessful. Here is what I have come up with:

powershell -command "& {([adsi]'LDAP://./localgroup,administrators').Add('LDAP://domainname/G-%COMPUTERNAME%-LocalAdmins,Administrators');}"

Any assistance would be greatly appreciated.

Upvotes: 1

Views: 153

Answers (3)

Ed Palmer
Ed Palmer

Reputation: 55

Per Microsoft:

The NET.EXE command does not support names longer than 20 characters for reasons of backward compatibility with LAN Manager 2.0.

Yes, Windows commands will pass variables to PowerShell. If the member is on the same domain, it does not need to be included. Use the Add-LocalGroupMember cmdlet.

PowerShell Add-LocalGroupMember -Group Administrators -Member 'G-%COMPUTERNAME%-LocalAdmins'

Upvotes: 0

Wayne
Wayne

Reputation: 11

After a day or so of research I found a microsoft forum by Jaap Brasser. He has written a script that I could invoke with my parameters and get to work...

powershell.exe -ExecutionPolicy Bypass -file "\server\pub\DomainAdmin\SetADAccounts.ps1" -Computer %COMPUTERNAME% -Trustee domainname\G-%COMPUTERNAME%-LocalAdmins

Thank you for your input..

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

You can run external commands (like net.exe) from PowerShell practically the same way you can run them from CMD. However, if you want to use (environment) variables you have to use PowerShell variable syntax ($var or $env:var respectively). PowerShell doesn't recognize CMD/batch variable syntax (%var%).

This should work:

net localgroup administrators "domainname\G-${env:COMPUTERNAME}-LocalAdmins" /add

Upvotes: 1

Related Questions