This is a bizzare issue. For whatever reason xcopy is not working under these conditions:
- Windows Server 2022, EC2Launch v2
- Run from Userdata
When I log in and run it manually it works just fine. But I am kind of stumped as to the reason why it doesn't work and want to know why, and if there is a way to get to work.
I have found out that:
- Copy-Item and Robocopy work fine, just xcopy does not
- checking C:\ProgramData\Amazon\EC2Launch\log\ reveals nothing
- checking the C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch346124587\ also nothing
my test userdata code is as below
<powershell>
PowerShell "C:\test\testConfiguration\test1.ps1"
PowerShell -ExecutionPolicy Bypass "C:\test\testConfiguration\test2.ps1"
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
Invoke-Expression "C:\test\testConfiguration\test3.ps1"
PowerShell "C:\test\testConfiguration\test4.ps1"
</powershell>
<persist>true</persist>
this is my code for the test1.ps1 files (file prefix a becomes b for test2 etc)
$scriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Set-Location -Path $scriptDirectory
echo hiworld *>> ahelloworld.txt
echo $scriptDirectory *>> ahelloworld.txt
$currentUser = whoami
$executionPolicy = Get-ExecutionPolicy
$processInfo = Get-Process -Id $PID
$scriptPath = $PSCommandPath
Write-Output "Current User: $currentUser" *>> ahelloworld.txt
Write-Output "Execution Policy: $executionPolicy" *>> ahelloworld.txt
Write-Output "Process Info: $processInfo" *>> ahelloworld.txt
Write-Output "Script Path: $scriptPath" *>> ahelloworld.txt
xcopy "C:\test\testConfiguration\files\OpenSSH-Win32" "C:\aOpenSSH-Win32\" *>> axcopyCheck.txt
xcopy "C:\test\testConfiguration\files\OpenSSH-Win32" "C:\aOpenSSH-Win322\" /Y /D /S /c *>> axcopyCheck2.txt
xcopy "C:\test\testConfiguration\files\OpenSSH-Win32" "C:\test\testConfiguration\aOpenSSH-Win32\" *>> axcopyCheck3.txt
xcopy "C:\test\testConfiguration\files\OpenSSH-Win32" "C:\test\testConfiguration\aOpenSSH-Win322\" /Y /D /S /c *>> axcopyCheck4.txt
this is the result of the helloworld.txt files
hiworld
C:\test\testConfiguration
Current User: nt authority\system
Execution Policy: Unrestricted (or Bypass starting from test2/b)
Process Info: System.Diagnostics.Process (powershell)
Script Path: C:\test\testConfiguration\test1.ps1
all of the xcopyCheck files come back with 0 bytes empty files. (*>> supposedly forwards all stdout/stderr/debug/info)
I am told that "nt authority\system" has higher rights than admin and can copy local files around. But even before that, it seems as though xcopy is not throwing any sort of output or error at all. It just... returns... what is going on? I cant event tell if this a userdata or aws issue? or is it a run as system issue? Could this affect other commands?
I do have a few guesses:
- System user is missing environmental settings. if so which one(s) when missing cause xcopy to do nothing
- permissions issue, doesnt make sense since Copy-Item and Robocopy work
- needs a certain running service, if so which one?
- based on XCopy does not work with UseShellExecute = false stdin needs to redirected. But i cant seem to recreate this issue, even using sysinternals and making a powershell to run a process from nt authority/system xcopy is working as normal.
UPDATE: Added powershell tags to the example given code
This is a bizzare issue. For whatever reason xcopy is not working under these conditions:
- Windows Server 2022, EC2Launch v2
- Run from Userdata
When I log in and run it manually it works just fine. But I am kind of stumped as to the reason why it doesn't work and want to know why, and if there is a way to get to work.
I have found out that:
- Copy-Item and Robocopy work fine, just xcopy does not
- checking C:\ProgramData\Amazon\EC2Launch\log\ reveals nothing
- checking the C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch346124587\ also nothing
my test userdata code is as below
<powershell>
PowerShell "C:\test\testConfiguration\test1.ps1"
PowerShell -ExecutionPolicy Bypass "C:\test\testConfiguration\test2.ps1"
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
Invoke-Expression "C:\test\testConfiguration\test3.ps1"
PowerShell "C:\test\testConfiguration\test4.ps1"
</powershell>
<persist>true</persist>
this is my code for the test1.ps1 files (file prefix a becomes b for test2 etc)
$scriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Set-Location -Path $scriptDirectory
echo hiworld *>> ahelloworld.txt
echo $scriptDirectory *>> ahelloworld.txt
$currentUser = whoami
$executionPolicy = Get-ExecutionPolicy
$processInfo = Get-Process -Id $PID
$scriptPath = $PSCommandPath
Write-Output "Current User: $currentUser" *>> ahelloworld.txt
Write-Output "Execution Policy: $executionPolicy" *>> ahelloworld.txt
Write-Output "Process Info: $processInfo" *>> ahelloworld.txt
Write-Output "Script Path: $scriptPath" *>> ahelloworld.txt
xcopy "C:\test\testConfiguration\files\OpenSSH-Win32" "C:\aOpenSSH-Win32\" *>> axcopyCheck.txt
xcopy "C:\test\testConfiguration\files\OpenSSH-Win32" "C:\aOpenSSH-Win322\" /Y /D /S /c *>> axcopyCheck2.txt
xcopy "C:\test\testConfiguration\files\OpenSSH-Win32" "C:\test\testConfiguration\aOpenSSH-Win32\" *>> axcopyCheck3.txt
xcopy "C:\test\testConfiguration\files\OpenSSH-Win32" "C:\test\testConfiguration\aOpenSSH-Win322\" /Y /D /S /c *>> axcopyCheck4.txt
this is the result of the helloworld.txt files
hiworld
C:\test\testConfiguration
Current User: nt authority\system
Execution Policy: Unrestricted (or Bypass starting from test2/b)
Process Info: System.Diagnostics.Process (powershell)
Script Path: C:\test\testConfiguration\test1.ps1
all of the xcopyCheck files come back with 0 bytes empty files. (*>> supposedly forwards all stdout/stderr/debug/info)
I am told that "nt authority\system" has higher rights than admin and can copy local files around. But even before that, it seems as though xcopy is not throwing any sort of output or error at all. It just... returns... what is going on? I cant event tell if this a userdata or aws issue? or is it a run as system issue? Could this affect other commands?
I do have a few guesses:
- System user is missing environmental settings. if so which one(s) when missing cause xcopy to do nothing
- permissions issue, doesnt make sense since Copy-Item and Robocopy work
- needs a certain running service, if so which one?
- based on XCopy does not work with UseShellExecute = false stdin needs to redirected. But i cant seem to recreate this issue, even using sysinternals and making a powershell to run a process from nt authority/system xcopy is working as normal.
UPDATE: Added powershell tags to the example given code
Share Improve this question edited Mar 29 at 0:04 John Rotenstein 270k28 gold badges447 silver badges531 bronze badges Recognized by AWS Collective asked Mar 28 at 1:58 Ranald FongRanald Fong 4295 silver badges14 bronze badges1 Answer
Reset to default 0Answering my own question, but only as much as I have figured out. After googling and testing out things as much as I can I think it is related to the bug/glitch found in the following articles.
XCopy does not work with UseShellExecute = false
https://www.visualcron/forum.aspx?g=Posts&t=4453
Why would C# ProcessStartInfoRedirectStandardOutput cause xcopy process to fail
The useshellexecute site, says XCopy is special in that both stdout and stdin must be redirected not just stdout -Tried to replicate this locally using Powershell ISE and couldnt.
Visual Cron site, has the same symptoms of nothing being done not even any output, but no explanation.
says in C# they needed to redirect stdin to get it to work with Ruby. -Tried to replicate using C# winforms and couldnt.
However, in taking those lessons, I changed my test.ps1 scripts from
xcopy "C:\...
to
echo a | xcopy "C:\... #also tried y, z and x
And xcopy worked being run from userdata, which I assume the echo pipe into the xcopy gave it the stdin to bind to. This is all conjecture so I wont mark my own answer as correct, but it seems as though xcopy has a sort of workaround of going up the chain looking for a stdin but if it cannot find it it just returns, and for the most part if you run it manually it will almost always find something to bind its stdin to. But under certain circumstances, for example being run from ec2 userdata powershell, or visual cron? or C# to ruby? or other obscure methods it fails looking for the stdin and just exits, no error no output.
On the other side it is depreciated and robocopy is its successor according to MS and doesn't suffer from this issue and can also copy from the network using UNC. I still hope anyone can give a more concrete evidence proof explained answer, as this is a very commonly used command. But maybe impossible given that its source code is propriety.