How to save cmd output as file name - windows

I'm running a command line script on multiple PC's and i'm trying to save username as a file name so i can see who's information i'm viewing later on.
In the command line script i run Whoami and i'd like to save it as "user"."file type". I'm trying to do this in a command line script because I always do it manually in command line and am trying to automate this process so I can do it faster.
If you know how to do it in a better way do share.

whoami > test.txt
tells it to go to a file and "test.txt" will be generated wherever your CMD CurDir is.

You may use Windows Environment variables %USERNAME% and possibly %USERDOMAIN% if the domain is needed.
%USERNAME% does not return the domain by itself.
Full list of standard environment variables: How-to: Windows Environment Variables
Use these in the command as needed. For example:
dir > %USERNAME%.txt
If you need the domain in there:
dir > %USERDOMAIN%_%USERNAME%.txt
(using _ to separate domain and username instead of \ since filename cannot contain \)
Remember to use >> instead of > if you don't want the file to be overwritten each time the command is run.
You may want to direct errors and standard output as needed: Redirecting error messages from Command Prompt: STDERR/STDOUT

Related

Proper syntax for psexec.exe hostname with filename to start

I'm trying to use psexec.exe to fire up Excel and then use Excel to open a specific .xlsx file.
I've just started learning psexec and I have written commands that work 'incrementally' in order to be sure my initial building blocks were correct along the way. Decided to actually start with NOTEPAD and a specific TEXT FILE to start:
(Please note, my goal is to execute this process on the local machine, not a remote machine, and for it to be Interactive, thus the -i switch)
This Works (just opens a blank Notepad window)
"C:\Users\Username\OneDrive - OrgName\Desktop\RunAs Test\psexec.exe" -i -u domain\username -p Password C:\Windows\system32\notepad.exe
This does not work - and I am positive the exe path and the file path are correct, I've tried them separately
"C:\Users\Username\OneDrive - OrgName\Desktop\RunAs Test\psexec.exe" -i -u domain\username -p Password C:\Windows\system32\notepad.exe \\server\data\mpsc-users\UserName\Test\sadf.txt
(It just returns the boilerplate 3 lines copyright, but with no mention of Error Code or results at all. Nothing else happens)
PsTools.chm is very clear on why it wont work
"Arguments to pass (note that file paths must be absolute paths on the
target system)"
"psexec.exe" -i \\somewhere.... C:\Windows\system32\notepad.exe c:\users\Isaac\desktop\hello.txt
should work (if the file exists on the remote system)

short path of current directory in cmd windows

I wish there's a way to show a shortened directory path in windows CMD, I searched the internet and found this command:
for %I in (.) do %~nxI
if you enter this command in cmd and press enter it would show the current dir name. suppose I was standing in my desktop it will print:
Desktop
thats enough for me. but not yet, I tried to save this command in a environment system variable then pass that variable to cmd line, but this way it would print the command itself instead of the result.
created a variable named PROMPT then value is:
%username%$s$p$s$d$t$_-$g$s
result is:
my_username full_path_to_current_dir date time newline dash greater_than_mark(>)
Mahdi c:\Users\Mahdi\Desktop 23/02/2020 19:27:38.93
->this is an screenshot of what Ihave: (https://i.stack.imgur.com/Auicf.png)
This is not a feature avaible in the standard prompt.
You potentially could hack it using echo off and doskeys,
but I would instead suggest checking out one of the many alternatives such as cmder or Zoc
Is this what you wanted?
For /F "Tokens=1*Delims=|" %I In (""%UserName%"|"%CD%"") Do #Prompt %~I$s%~nxJ$s$d$t$_-$g$s
Alternatively, if you wanted it setting to a variable, run the prompt command inside another for loop, and capture the output of that with the Set command.

Run a remote bash script on a Mac using PuTTy

I want to run a bash script on a mac remotely from a batch script on a windows machine. On Windows I have this:
#echo off
echo bash /applications/snowflake/table-updater/test2.sh; exit>tmp_file
putty -ssh User#remote_machine -pw password -m tmp_file
And here is test2.sh on the remote machine
#!/bin/bash
# test2.sh
#
#
7za x table-apps.zip -y -o/Applications/snowflake/applications
When the batch file runs it logs in successfully, but for some reason fails to run the bash file. However the bash file runs fine from mac terminal, where it unzips the files perfectly. What could be happening here?
Please note test2.sh is actually in Applications/snowflake/table-updater as specified in the batch file. And the tmp file does write fine as well. My aim is to have a script to access a further 10 remote machines with the same directory structure.
Thanks in advance
The standard program which resembles the scriptable Unix command ssh in the PuTTy suite is called plink, and would probably be the recommended tool here. The putty program adds a substantial terminal emulation layer which is unnecessary for noninteractive scripting (drawing of terminal windows, managing their layout, cursor addressing, fonts, etc) and it lacks the simple feature to specify a command directly as an argument.
plink user#remote_machine -pw password /Applications/snowflake/table-updater/test2.sh
From your comments, it appears that the problem is actually in your script, not in how you are connecting. If you get 7za: command not found your script is being executed successfully, but fails because of a PATH problem.
At the prompt, any command you execute will receive a copy of your interactive environment. A self-sufficient script should take care to set up the environment for itself if it needs resources from non-standard locations. In your case, I would add the following before the 7za invocation:
PATH=$PATH:/Applications/snowflake/table-updater
which augments the standard PATH with the location where you apparently have 7za installed. (Any standard installation will take precedence, because we are adding the nonstandard directory at the end of the PATH -- add in front if you want the opposite behavior.)
In the general case, if there are other settings in your interactive .bashrc (or similar shell startup file) which needs to be set up in order for the script to work, the script needs to set this up one way or another. For troubleshooting, a quick and dirty fix is to add . /Users/you/.bashrc at the top of the script (where /Users/you should obviously be replaced with the real path to your home directory); but for proper operation, the script itself should contain the code it needs, and mustn't depend on an individual user's personal settings file (which could change without notice anyway).

Rerouting the SAS logs to a specific location in shell scripting

I have created a shell script which runs a SAS program which is created log in the same folder where I'm running Shell script. But, I'm trying to save the logs to a specific folder on LINUX. I used -log option and it is throwing me error...I'm running following command in my shell script...
/saspath/sas /homesas/test.sas -log home/sasu1/log/test.log.$rundatetime \
I'm getting this error... -log: command not found
It is normal Unix convention to put the options (-log) before the parameters (filename) to a command.
sas -log xxx.log xxx.sas
Your real problem might be that you need to construct your log filename first.
pgm=test
log=${pgm}.${rundatetime}.log
sas -log $log &pgm
Another thing to check is that some sites have build scripts to launch SAS and they do not properly pass the command line arguments through to the actual command that launches SAS. Check whether /saspath/sas is the actual command provided by SAS or something your local IT group created.

Can output file directory be specified via command-line?

I have an .nsi file which I call via command-line to output the installer, like this:
makensis fullPathToNsiScript
This creates the installer in the folder where my nsi script is. I need to output it to a specific folder, say desktop. Is it possible to do that via command line?
I know the correct way is to specify it in the script itself, like
OutFile "outputFileFullPath"
But is it possible to do via command-line assuming I'm only providing the base-name of the output file in the nsi script? Something like:
In script,
OutFile "outputFilename"
and then in command-line,
makensis fullPathToNsiScript "outputFileFullPath"
? The above obviously doesn't work.
makensis "/XOutFile $%temp%\test.exe" setup.nsi
(Running Makensis /? will show a similar example, /X can be used to execute any NSIS command)
The makensis.nsi example uses another tactic, specifying the outfile on the commandline is optional (/DOutFile=foo.exe makensis.nsi) and the script uses a !ifdef with a hard-coded fallback...

Resources