HI everyone,
I need to join different computers to a domain. I always did it through wmic commands in a bat file, no problem till now. Now the wmic command is deprecated (Win11) and i’m trying to find a way to join.
before i used this:
wmic computersystem where name="%computername%" call joindomainorworkgroup fjoinoptions=3 name=domain username=domain\user Password="password"
Now i’m trying to accomplish it through powershell that looks like this:
$domain = "domain"
$password = "password" | ConvertTo-SecureString -asPlainText -Force
$username = "$domain\user"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential
The script works when launched from an administrative powershell session. But i’d like to run it from my USB. I tried/encountered the following errors:
1-Execution Policy
I don’t want to disable execution policy for each machine, but just temporarily. I worked around this doing a .bat file that calls the .ps1 file bypassing execution policy:
powershell -ExecutionPolicy Bypass -File script.ps1
The script run, but it says “access denied” because it needs an administrative powershell session.
2-Executing Powershell as admin user??
The second and most annoying one is that the script won’t run in an admin console, even if the bat file has run as admin. So i tried doing this bat file:
powershell -noexit -ExecutionPolicy Bypass -Command "Start-Process -Verb RunAs powershell -ArgumentList '-ExecutionPolicy Bypass -File .\joinwsm.ps1'"
But the script just won’t do anything. If I remove the “ArgumentList” part, it correctly opens a powershell terminal window as admin.
I’ve been trying for too much time right now, maybe the thing is simpler than i think
Any help/suggestion would be appreciated, thank you!