cmd exited with error code 1 - cmd

I am trying to search a file on remote PC using PSEXEC, however I am getting the above mentioned error.
Can any please suggest any change in the command or some thing else, that can assist in making it work.
Online search suggests that this isn't the issue of PSEXEC, instead is caused by explorer.exe on remote host.
I have added picture of my result. I have tried the command with quotes on file name and without. both have same error.

This command dir exists with code 1 and message "File Not Found".
Because you run cmd with param /c than it return same error code like dir and it is equal 1.
It is correct behaviour.
If you want to find a file, than use command where /r c:\ d.txt. This command was added in Windows 7.
Or use dir /S /P "d.txt" for older OS

Open Registry Editor by pressing windows+r
then type regedit and press enter
now in the search bar paste the below line
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\
If you can see in the picture using the link
Underlined mark is important don't insert any other value in the data of that particular registry.
Here if you filed many registry values then delete all of them except the default one as they are the main reason for the error code 1 shown by the command prompt.
So after removing all of them.
Edit the default registry value and in 'data' insert cmd and save it
Your problem is solved!!
if not then clear the data of the default registry.

It is also easy to use PowerShell and not need psexec.
Invoke-Command HOST01 { & cmd.exe /C dir D:\Users\lit\d.txt }
It should be written in .ps1 scripts with parameter names specified.
Invoke-Command -ComputerName HOST01 -ScriptBlock { & cmd.exe /C dir D:\Users\lit\d.txt }
See also:
help Enable-PSRemoting
help about_Remote
help about_Remote_FAQ

Related

Change Windows command prompt to show only current folder

Instead of showing
C:\Users\test_user\Documents\Folder\etc
show
\etc
or if possible limit it to a certain number
\Document\Folder\etc
If you check in help prompt /? there are two options that can either show the current drive or full path.
I would suggest to use new line option along with the Drive so that you will get more space to view/type the command using below combination.
prompt $P$_$G
With this you will be able to see the Path in the line above the prompt.
In short, can't see a simple way of doing it.
In order to change the prompt options you can use the prompt command. The configuration you're looking for isn't listed.
The available options can be viewed by
prompt /? in the command window.
https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/prompt.mspx?mfr=true
The following is a simple batch script which can set the prompt to include only the current folder. Note that it does not work on directory names with certain characters such as parenthesis and spaces. I named it cdd.bat.
#echo off
cd %1
for %%i in (%CD%) do set NEWDIR=%%~ni
PROMPT %NEWDIR%$G
Like others pointed out, you can use the command - prompt to set the text that is shown in cmd.
While you cannot dynamically set the path to just the parent folder, you can manually set it using:
prompt {text}
So in your case, you can set it as:
prompt etc\$G
This will result in:
etc\>
$G adds an arrow sign. You can refer the documentation for detailed explanation.
Here is a .ps1 file i use to do this for myself.
<#
FileName: promptPsShort.ps1
To set the prompt to the last folder name in the path:
> function prompt {$l=Get-Location; $p="$l".split("\")[-1]; "PS $p> "}
# works at cmd prompt, BUT NOT DIREECTLY from a .ps1 file.
RESEARCH
1. google: powershell 7 copy text into clipboard
[How to copy text from PowerShell](https://superuser.com/q/302032/236556)
[Set-Clipboard](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/?view=powershell-7)
2. google: powershell escape double quote
[Escaping in PowerShell](http://www.rlmueller.net/PowerShellEscape.htm)
3. google: powershell raw string
[About Quoting Rules](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7)
4. Usage example: powershell
PS C:\flutter_beta\flutter\examples\catalog\android\app\src\main> pwd
Path
----
C:\flutter_beta\flutter\examples\catalog\android\app\src\main
PS C:\flutter_beta\flutter\examples\catalog\android\app\src\main> promptPsShort.ps1
Paste the current Clipboard contents into the Powershell Command Line and press Enter.
PS C:\flutter_beta\flutter\examples\catalog\android\app\src\main> function prompt {$l=Get-Location; $p="$l".split("\")[-1]; "PS $p> "}
PS main>
PS main>
PS main>
#>
$shortPromptCmdStr = #'
function prompt {$l=Get-Location; $p="$l".split("\")[-1]; "PS $p> "}
'#
Set-Clipboard -Value $shortPromptCmdStr
write-host "Paste the current Clipboard contents into the Powershell Command Line and press Enter."
Love and peace,
Joe

Problems Executing Powershell from .cmd file

I am attempting to run a build task from a .cmd file where Powershell extracts a zip file, which helps bypass a problem with Visual Studio's limit on the number of directory characters. However, I am having problems getting the Powershell command to execute correctly. I've tried a number of variations with the quotations, and I either get a termination error, or the Powershell command outputs as a string with the zip file not extracted. Below is an example of my current .cmd file:
//%1% is a passed in command line argument for the absolute path, e.g. C:\path\to\dir
set Source=%1%directory.zip
set Destination=%1%directory
powershell -Command $pscmd = '{Add-Type -assembly "system.io.compression.filesystem";[io.compression.zipfile]::ExtractToDirectory("%Source%", "%Destination%");}'; Write-Host $pscmd;
I'm very open to a number of variations that can get this to work, provided that this task runs on the command line, uses Powershell, and can be executed from a .cmd file, which is triggered by our app's build process. I'll be happy to provide additional information if needed. Thanks!
This was a strange one. Your code above has some sort of hidden character in it. I took the code and opened it in notepad, saved as ANSI, and when you type it to command line or open it again in a new instance of notepad you can see the error.
Neither add-type nor ExtractToDirectory give output, so I removed your pscmd var.
I would open your existing script, save as ansi as a new file name, delete the original, rename the new one back to the original name.
Here is what I came up with to troubleshoot your script, and it works on my machine.
I named my script L:\util\unzip.cmd
setlocal
//%1% is a passed in command line argument for the absolute path, e.g. C:\path\to\dir
set _Source='%1\directory.zip'
set _Destination='%1\directory'
echo _Source=%_Source%
echo _Destination=%_Destination%
set _c1=Add-Type -assembly system.io.compression.filesystem;
set _c2=[io.compression.zipfile]::ExtractToDirectory(%_Source%, %_Destination%)
echo _c1=%_c1%
echo _c2=%_c2%
set _Command=^& {%_c1% %_c2%};
: to echo, use double quotes or cmd thinks text after & is another command
echo _Command="%_Command%"
pause
powershell -Command "%_Command%"
endlocal
I ran it like this, and it worked: unzip.cmd L:\util
I'll bet this this info, you are good to go.

Regedit: Find and export keys from command prompt

I'm wondering is there any option via command line to search for a string and export all found keys in Windows registry?
Ex:: If you want to check whether "HKLM\software\etc" key exists.
reg.exe query "HKLM\Software\etc" will return all the subkeys and values in command prompt if found or an error if not found.
ALso, you can directly do
reg.exe export "HKLM\software\etc" "C:\etc.reg"
This will export the registry key and subkeys if found otherwise error if not found.
Powershell has registry iteration capabilities. Start here: http://technet.microsoft.com/en-us/library/ee176841.aspx
export key (with all sub-keys), from CMD (or RUN) i.e.:
regedit /e c:\output.reg "HKEY_LOCAL_MACHINE\System\YourLocation"
p.s. you should run this in CMD with ADMIN PRIVILEGES. for that, right click on START>Run CMD (as Admin)

Lua programming - os.execute() is not working in Windows

I'm creating a function in pure-Lua to scan the files from a directory and put they on a another file.
The command I tryed was:
os.execute( "dir /B C:\\Users\\Fernando\\workspace\\Organizator2\\s1 >
C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt" )
but... dont works! I did many tests with others simpler commands, like "start notepad" or "mkdir C:\test", and they dont worked too! The worse part is that I tryed this same commands directly in the Prompt, and there is all correct.
I tryed use tooo the io.popen(), but it the system returned "illegal operation" for any command i passed (even a empty string!).
here is the all code:
function ScanDirectory(source, str)
local str = str or "temp.txt"
os.execute("dir /B "..source.." > "..str)
directory = io.open(str,"r")
return directory
end
-- main script
do
local source = "C:\\Users\\Fernando\\workspace\\Organizator2\\s1"
local directory = ScanDirectory(source, "C:\\Users\\Fernando\
\workspace\\Organizator2\\temp.txt")
end
I'm using windows 7 and the Luaforwindows, 5.1, and the LuaEclipse
Have someone ever seen a problem like this?
Please try it with this syntax:
os.execute [["dir /B C:\Users\Fernando\workspace\Organizator2\s1 >
C:\Users\Fernando\workspace\Organizator2\temp.txt"]]
Please note that the backslash (\) is not a special character in this case.
(Lua uses cstrings internally, sometimes it leads to some weird and amazing results :P)
Most of the commands you listed appear to be shell commands that only work within a command prompt. Try running cmd.exe directly to see if you get a prompt, and if so, you can try passing commands to cmd.exe via the /c option. You could also try notepad without the start to see if that runs.
os.execute('cmd.exe /c dir /B C:\\> C:\\test.txt')
That works. Useing Linux-style commands in win is a bad idea at all =)
I just tested your code on my computer and it works correct (with my directories, of course). Maybe you are not getting the expected result because your directory string is broken with an newline char, resulting in:
dir /B C:\Users\Fernando\workspace\Organizator2\s1 > C:\Users\Fernando\
workspace\Organizator2\temp.txt
The correct should be:
dir /B C:\Users\Fernando\workspace\Organizator2\s1 > C:\Users\Fernando\workspace\Organizator2\temp.txt
Please try changing the do end to:
local source = "C:\\Users\\Fernando\\workspace\\Organizator2\\s1"
local directory = ScanDirectory(source, "C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt")

Administrator's shortcut to batch file with double quoted parameters

Take an excruciatingly simple batch file:
echo hi
pause
Save that as test.bat. Now, make a shortcut to test.bat. The shortcut runs the batch file, which prints "hi" and then waits for a keypress as expected. Now, add some argument to the target of the shortcut. Now you have a shortcut to:
%path%\test.bat some args
The shortcut runs the batch file as before.
Now, run the shortcut as administrator. (This is on Windows 7 by the way.) You can use either right-click -> Run as Administrator, or go to the shortcut's properties and check the box in the advanced section. Tell UAC that it's okay and once again the shortcut runs the batch file as expected.
Now, change the arguments in the target of the shortcut to add double quotes:
%path%\test.bat "some args"
Now try the shortcut as administrator. It doesn't work this time! A command window pops up and and disappears too fast to see any error. I tried adding > test.log 2>&1 to the shortcut, but no log is created in this case.
Try running the same shortcut (with the double quotes) but not as Administrator. It runs the batch file fine. So, it seems the behavior is not because of the double quoted parameters, and it's not because it's run as Administrator. It's some weird combination of the two.
I also tried running the same command from an administrator's command window. This ran the batch file as expected without error. Running the shortcut from the command window spawned a new command window which flashed and went away. So apparently the issue is caused by a combination of administrator, the shortcut, and the double quotes.
I'm totally stumped, does anyone have any idea what's going on? I'd also like a workaround.
I just ran Process Monitor on this and here is what I saw:
Run as User:
cmd /c ""C:\Users\Sunbelt\Desktop\test.bat" "some args""
Run as Admin:
"C:\Windows\System32\cmd.exe" /C "C:\Users\Sunbelt\Desktop\test.bat" "some args"
For some reason, the Run as Admin case is not quoting the entire command. It seems it is trying to run the command:
C:\Users\Sunbelt\Desktop\test.bat" "some args
I would guess that since the first space is quoted it actually trying to run the following command:
"C:\Users\Sunbelt\Desktop\test.bat some" args
And in Process Monitor logs there is a file system entry of "NO SUCH FILE" for "C:\Users\Sunbelt\Desktop\test.bat some". I don't know why it is different when run as Admin, but that's what appears to be happening.
To work around this, create another bat file on a path without spaces, and with a filename without spaces, that simply does this:
call "Long path to original bat file\Original bat file.bat"
This secondary bat file can be run as admin.
You can now create a shortcut to this secondary bat file and check run as admin in the shortcut's advanced options. The shortcut can be placed on a path with spaces and it can have a filename containing spaces.
In my case I just want to pass one filename as parameter, but the path has spaces.
I found a solution that worked for this case (if that's okay to truncate the file name).
Create another bat file (input_gate.bat) to remove the spaces in the path using the syntax of CALL.exe.
Assuming that the shortcut is named test.lnk and is on the same route as the input_gate.bat:
call %~sdp0test.lnk %~sf1
This pass as a parameter to test.bat the full file name in short format, with administrator privileges.
%~sdp0 -> Is the current path (for the input_gate.bat) in short format.
%~sf1 -> Is the first parameter passed to input_gate.bat (in my case the full filename with spaces)
This worked for me in Windows 7:
ShortcutTarget: C:\Windows\System32\cmd.exe /C myscript.bat Param1 "Param Two with spaces"
StartIn: "C:\Path containing\my script"
Not tried it yet as Admin. I don't think it would work if myscript.bat contained spaces
I finally figured it out, in a way that allows the use of long filenames (short filenames weren't adequate for my use case). My solution works on Win 7, 8, and 10. I think it was inspired by Luke's mention of the missing double-quote.
Here it is:
1.) For the shortcut you create to the batch file, which you set to run as admin, use the following command line:
cmd /s /c ""path_to_batch_file"
Note that there are 2 double-quote characters at the beginning of the command, and only 1 at the end, even though there should normally be 2 at the end also. But that is the trick in getting it to work!
2.) In your batch file, add back the missing double-quote character:
set user_file=%1"
That's it! Here's an example to let you see it in action:
1.) On your desktop, create "test.bat" with the following content:
#echo off
set user_file=%1"
echo The file is: %user_file%
pause
3.) Create a shortcut to the batch file. Set it to run as admin, and give it the following command line:
cmd /s /c ""%userprofile%\desktop\test.bat"
4.) Drag a file onto the shortcut. Success! (I hope...)
Answer here worked for me: https://superuser.com/questions/931003/passing-variables-with-space-to-the-command-line-as-admin
Which is...
Adding cmd /C before the target.
I also had to make sure my script's name and path didn't have spaces, and not quote the script's path in target.

Resources