DDEAUTO: Invoke powershell with an encoded command - windows

I have a base64 encoding of a command (6475 characters) and I can successfully execute it from the cmd.exe command prompt with powershell -e base64_string, but when I create the following field in Word:
{ DDEAUTO c:\\windows\\system32\\cmd.exe "/k powershell -e base64_string" }
I get an error from powershell that the command is not properly encoded. I re-checked 100 times, copy pasting the same thing in both the cmd command prompt and in Word, I made sure to paste it as plain unformated text, still the error. With single instead of double quotes I get no error, but the command is not being executed: just a blank cmd window. What could I be doing wrong?

Related

shell script capture variable without formatting escape characters

I am running an .sh script from wsl bash.
Inside the script I want capture a file path, into a variable, in order to pass it to cmd.exe like so:
my_win_path=<some_windows_unc_path_to_executable>
cmd.exe /c "$my_win_path"
However when I try to capture the file path, it's backslashes are getting formatted, resulting in an non-valid path.
Example:
~: wslpath -w .
C:\tmp
~: var=$(wslpath -w .); echo $var
C: mp
cmd.exe /c "$var # will error
How do I capture the output of wslpath into a valid variable without formatting the escape characters ?
(I hope I am using the right terminology here. Please correct me if not.)

Open a command file with Windows PowerShell running it directly

I want to make a file having Windows Powershell commands. Then I want to open it with windows powershell directly and without pressing any key I want windows powershell start running those commands directly same as command prompy I can make .cmd or .bat file.
For example:
These are two commands or Powershell, I want to save this file. Then I want directly execute this file by powershell. I have tried to save it as ps1 and ps2 extension as well but not working. Many methods online are not working. Any solution?
PowerShell script files, across all versions, use the .ps1 filename extension.
From within PowerShell, you can invoke them directly, e.g., .\script.ps1
Note that, unlike in cmd.exe, you must use .\ (or a full path) in order to execute a file located in the current directory - just script.ps1 won't work - see this answer for background information.
From cmd.exe, you must use PowerShell's CLI (powershell.exe in Windows PowerShell / pwsh in PowerShell [Core] v6+) in order to execute a script file:
powershell.exe -File script.ps1
pwsh -File script.ps1 (-File may be omitted)
Note that with -File the .\-prefix is not required.
However, if you use -Command (-c) instead (which is the default with powershell.exe, whereas pwsh now defaults to -File), you do need the .\, because the -Command argument(s) are interpreted as a piece of PowerShell code, i.e. as if you had submitted it inside a PowerShell session.
You've discovered this in your own answer, where you pass a PowerShell command directly to the (implied) -Command parameter.
Note, however, that it's better to double-quote such commands, so as to prevent cmd.exe from interpreting certain characters itself, which breaks the call.
For instance, the following call would break, if you didn't enclose the -Command (-c) argument in "...":
# From cmd.exe; "..." required.
C:\>powershell.exe -c "Write-Output 'a & b'"
a & b
Another important consideration is that you need to escape embedded " chars. as \" for the CLI (even though PowerShell-internally you would use `" or ""):
# From cmd.exe; note the inner " escaped as \"
C:\>powershell.exe -c "Write-Output \"hi there\""
hi there
I have found the solution. I use command powershell.exe and can directly execute powershell commands within cmd.
powershell.exe $MyVariable=Get-Content .\Path.txt
is working fine for me

Clear PowerShell console in bash on Windows

I download bash.exe from SourceForge and added it to my path in Powershell, but I can't get it to clear the console. clear.exe is missing from the zipfile that was downloaded, so it makes sense that that command doesn't work. However, using Ctrl+L also does not clear the powershell console.
How can I get the powershell console to clear when I'm using bash in it?
Note: I've tried adding an alias called clear to my .bashrc as alias clear=echo <many enters>, but it doesn't work quite the way I've expected (i.e. only echoes 4 or 5 newlines). Also, echo "\n\n" just prints out literal \n\n.
In the absence of a clear or tput utility, and given that the usual ANSI escape sequences don't work with the (built-in) printf, you must call out to either cmd.exe or PowerShell to effect clearing the screen:
bash$ powershell -noprofile -c cls
Using cmd is faster, but the problem is that the win-bash invokes external programs by double-quoting each argument behind the scenes, which causes a command such as cmd /c cls to malfunction; the following workaround mostly works, but prints the cmd.exe prompt string once after clearing the screen.
# !! Clears the screen, but prints the cmd.exe prompt string once.
bash$ echo cls | cmd

History substitution is messing with command line arguments

I'm running an expect script that takes username, password from command line.
However, password contains !! (eg: pass!!word)
But shell is expanding this to previous command (read this link)
How to escape this?

Escape characters for echo/printf?

So I'm trying to write this script where it will echo out a name of a command, but I don't want it to actually run that command that it's echoing out, in this example I'm using screen
printf "You run this command with screen to run in a background."
but what I find to be having issue with this is that it's running screen command in that printed output, and it shows an error as a result. Is there some escape character I need to enter to prevent this from happening, so that the rest of the script can run properly?
When you write an expression within double quotes in bash it will execute the commands within it. If you don't want it to execute the command enclose your statement within single quotes
'You run this command with screen to run in a background.'

Resources