# CMD not working within Invoke-Command

**URL:** <https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884>\
**Category:** PowerShell Help\
**Created:** [June 21, 2021, 8:45pm UTC](https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884 "2021-06-21T20:45:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwhitmore](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/cwhitmore/32/14_2.png) [@cwhitmore](https://forums.powershell.org/u/cwhitmore)\
**Post date:** [June 21, 2021, 8:45pm UTC](https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884/1 "2021-06-21T20:45:47Z")

</div>

I’m trying to run this command within a script, but it won’t run the DJOIN command.

> Invoke-Command -ComputerName $DirectServer -ScriptBlock {  
> New-Item -ItemType directory -Path “c:\OfflineJoinDomain” -ErrorAction SilentlyContinue  
> cmd /c Djoin.exe /provision /domain $using:Domain /dcname $using:DomainController /machine $using:NewComputer /policynames “DirectAccess Client Settings” /rootcacerts /savefile “c:\OfflineJoinDomain$using:ComputerFile” /reuse  
> Copy-Item “c:\offlineJoinDomain$using:ComputerFile” -Destination “\server1\c$\PSDeployment\OfflineJoinDomain$using:ComputerFile”

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 21, 2021, 9:21pm UTC](https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884/2 "2021-06-21T21:21:23Z")

</div>

> [@cwhitmore](#):
>
> but it won’t run the DJOIN command

That description is actually not that helpful. 😉 What exactly happens? Do you get any error messages?  
Have you considered using a native PowerShell command instead? As there is

> **[Add-Computer (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/add-computer?view=powershell-5.1)**
>
> The Add-Computer cmdlet adds the local computer or remote computers to a domain or workgroup, or moves them from one domain to another. It also creates a domain account if the computer is added to the domain without an account. You can use the...

… you don’t need the external tool `djoin.exe`.

Regardless of that … PowerShell is - just like CMD - a command line shell. So at the moment you’re using one command line shell to call another command line shell to call an external executable. I urgently recommend to re-consider that process.

And last but not least - when you post code, error messages, console output or sample data please format it as code using the “_preformatted text_” button ( **\</\>** ).

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![cwhitmore](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/cwhitmore/32/14_2.png) [@cwhitmore](https://forums.powershell.org/u/cwhitmore)\
**Post date:** [June 21, 2021, 10:39pm UTC](https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884/3 "2021-06-21T22:39:02Z")

</div>

Thanks for the response Olaf. I’m adding this computer via the Internet so after the file is generated from the djoin command I run another djoin from the remote computer which talks to my DirectAccess server over https. I don’t think Add-Computer will work in this instance.  
I don’t get an error when the djoin command is run. It appears that it’s just not running at all.  
Should I use invoke-command multiple times, one for each command?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 22, 2021, 12:35am UTC](https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884/4 "2021-06-22T00:35:19Z")

</div>

Hmmm … I don’t have experiences with the offline domain join but I assume you know the following documentation …

> **[DirectAccess Offline Domain Join](https://docs.microsoft.com/en-us/windows-server/remote/remote-access/directaccess/directaccess-offline-domain-join)**
>
> This guide explains the steps to perform an offline domain join with DirectAccess in Windows Server 2016.

I’d recommend instead of running the external command via CMD you should run it via

> **[Start-Process (Microsoft.PowerShell.Management) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.2&viewFallbackFrom=powershell-7.1)**
>
> The Start-Process cmdlet starts one or more processes on the local computer. By default, Start-Process creates a new process that inherits all the environment variables that are defined in the current process. To specify the program that runs in the...

and catch the potentially occuring output in files via `-RedirectStandardOutput` and `-RedirectStandardError`.

For debugging and testing you should try to run the commands by hand and see what happens.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [June 22, 2021, 4:22am UTC](https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884/5 "2021-06-22T04:22:26Z")

</div>

I would ensure you’re not running into the common double hop issue. To even query AD from a remote machine via invoke-command, you’ll not be able to make that second hop without passing creds, using credssp, or other work arounds.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [June 22, 2021, 2:39pm UTC](https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884/6 "2021-06-22T14:39:53Z")

</div>

> [@cwhitmore](#):
>
> “\server1\c$\PSDeployment\OfflineJoinDomain$using:ComputerFile”

Doug has a great point, and this is likely a typo, but your UNC is missing a “\” as in “\\server…”

---

<div class="post-metadata">

**Author:** ![cwhitmore](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/cwhitmore/32/14_2.png) [@cwhitmore](https://forums.powershell.org/u/cwhitmore)\
**Post date:** [June 22, 2021, 3:00pm UTC](https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884/7 "2021-06-22T15:00:08Z")

</div>

Good catch Tonyd. I changed the server name so probably fat fingered it when I was posting.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [June 22, 2021, 4:03pm UTC](https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884/8 "2021-06-22T16:03:07Z")

</div>

One thing you might do to avoid the double hop is move Copy-Item outside of the Inovke-Command, something like:

```auto
Copy-Item “\\$DirectServer\c$\offlineJoinDomain$ComputerFile” -Destination “\\server1\c$\PSDeployment\OfflineJoinDomain$ComputerFile”

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:24pm UTC](https://forums.powershell.org/t/cmd-not-working-within-invoke-command/16884/9 "2024-05-16T20:24:14Z")

</div>

Closed on May 16, 2024, 8:24pm UTC


