Hi @Mohamed Boulaghlem ,
I assume that you have checked the DCOM configurations on both computers and are making sure that the user you are using to connect to Computer B has admin privileges. If you don't insist on using DCOM, there is a better solution within PowerShell, that uses WinRM protocol, which is the more modern equivalent of DCOM. Get-WmiObject is also outdated command and it uses DCOM to fetch data. There are few different ways to do this, and you just have to enable PowerShell remoting. In a PowerShell terminal, run:
Enable-PSRemoting
Do this on both computers, the one you connect to and the one you connect from (you can also enable remoting via group policy for a bunch of computers). This will set your computer up to accept and make remote connections. Wait for PowerShell to complete the setup. You might encounter some sort of error, if your network connection profile is set to "Public" for some reason. If so, change it to "Domain" and run the command again. Once done, check if remoting is indeed enabled by running this command:
Test-WSMan
If the result looks something like this, you are good to go:
wsmid : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd
ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
ProductVendor : Microsoft Corporation
ProductVersion : OS: 0.0.0 SP: 0.0 Stack: 3.0
You only need to do this once (on each computer). Now you can query information directly or run commands remotely. As mentioned, there are few different ways to do either, here is one way to query information (store it in a variable if you'd like):
Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName "Computer B"
Get-CimInstance is the more modern equivalent of Get-WmiObject.
To establish remote connection, you need to open a session to "Computer B", that you can use continuously, or use Invoke-Command for one-time commands or blocks of code:
# One-time command to run a bat file on a remote computer
Invoke-Command -ComputerName "Computer B" -ScriptBlock {Start-Process "C:\mybat.bat"}
Open a persistent session that you can send commands to:
# Authenticate to the remote computer
$creds = Get-Credential -UserName "yourusername"
# Open remote session
$session = New-PSSession -ComputerName "Computer B" -Credential $creds
# Execute command
Invoke-Command -Session $session {Start-Process "C:\mybat.bat"}
I hope this helps. Please accept this answer if it worked for you.