Application cannot be executed when launch from a different folder - powershell

I have a third party application that has following two files in c:\sandbox folder.
SomeApplication.exe
SomeApplication.ini
The executable uses the ini file for various configuration settings. When I run this executable from the powershell command prompt after change my directory to c:\sandbox, all works great.
PS C:\sandbox> SomeApplication.exe
INFO: Its working great
However when I try to run same application without changing my directory to sandbox folder, it does not work.
PS C:\> .\Sandbox\SomeApplication.exe
ERROR: SomeApplication.ini could not be found
I try to do following before running the application
PS C:\> Set-Location -Path "C:\Sandbox"
But still get same error. Any idea what could be wrong?

The program assumes that it will be launched from the current directory and looks for the ini file using windows apis that search the current direction and the path. If you add it to the path it might work.

Related

Error running Metricbeat.exe commands in powershell

I am trying to install Metricbeat on a Windows 10 machine so we can start monitoring it. When I open Powershell and run the following commands:
PS > .\metricbeat.exe modules list
I get the error
I copied that command as is from the Metricbeat documentation. I have seen videos on youtube of people running similar commands successfully. Please, why am I getting that error and what can I do to get my metricbeat.exe powershell commands to work?
You're copying the command TOO literally.
you've entered in the prompt PS > .\metricbeat.exe modules list
where you should have entered .\metricbeat.exe modules list
the latter executes modules list action against an application named "metricbeat.exe" located in .\ which indicates the current directory.
the former executes a redirection > of the output of an application named PS or get-process with input of .\metricbeat.exe modules and an argument of list.
wherever you copied this command from intended "PS >" to represent the beginning of the prompt and you don't need to include it.
Just like the error says... :P

Start-Process : The system cannot find the file specified from TeamCity Build step but works fine locally

i am trying to run Pact broker can i deploy tool with paramaters which is working fine locally but when i add the build step in TeamCity it is throwing below error
Start-Process : This command cannot be run due to the error: The
system cannot find the file specified.
when i run the same powershell script locally, it is working fine .
powershell script:
CanIDeploy.bat code is
Note: the reason i am calling pact-broker.bat from power shell script is, unable to run bat file from Teamcity , that is the reason created powershell script which internally calls pact broker bat file.
any help is appreciated
I see that you use relative path, but what about the working directory? I see a different path in the error message vs what you show where the file is.

PowerShell executing .bat files with in the same session

I am installing a software in silent mode using PowerShell. Installation was done successfully but when I am trying to execute few batch files I am getting some exceptions.
Could not find the files for the given pattern. The system can not find the specified path.
My command inside my PowerShell:
$inst_path = \\My installed drive (c:\program files\mysoftware\)
& $inst_path\start-service.bat install
But when I closed the PowerShell ISE and executing the same command it is working without any exceptions, so can someone help me overcome this?
You've got a double slash in the path to the bat file, both the $inst_path variable and the path you construct.
You're trying to call c:\program files\mysoftware\\start-service.bat - which is going to fail.
Try this instead:
$inst_path = "c:\program files\mysoftware"
& "$inst_path\start-service.bat" install

Azure Cloud Service Startup task that needs to run a PowerShell script

All,
Note: I have updated the question after some feedback.
Thanks to #jisaak for his help so far.
I have the need to run a PowerShell script that adds TCP bindings and some other stuff when I deploy my Cloud Service.
Here is my Cloud Service Project:
Here is my Cloud Service Project and Webrole project:
Here is my task in ServiceDefinition.csdef:
And here is the PowerShell script I want to run:
here is my attempt at the Startup.cmd:
When I deploy I get this in the Azure log:
And this in the powershell log:
Any help would be very much appreciated.
I think I am nearly there but following other people syntax on the web doesn't seem to get me there.
thanks
Russ
I think the issue is that the working directory of the batch command interpreter when it runs Startup.cmd runs is not as expected.
The Startup.cmd is located in the \approot\bin\Startup directory but the working directory is \approot\bin.
Therefore the command .\RoleStartup.ps1 is not able to find the RoleStartup.ps1 as it is looking in the bin directory not in the bin\Startup directory.
Solutions I know to this are:
Solution 1:
Use ..\Startup\RoleStartup.ps1 to call the RoleStartup.ps1 from Startup.cmd.
Soltuion 2:
Change the current working directory in Startup.cmd so that the relative path .\RoleStartup.ps1 is found. I do this by CHDIR %~dp0 (see here) to change into the directory that contains Startup.cmd.
Solution 3:
As Don Lockhart's answer suggested, do not copy the Startup directory to the output, instead leave it set as "Content" in the Visual Studio project. This means the files within it will exist in the \approot\Startup directory on the Azure instance. (You would then want to make sure that the Startup folder is not publically accessible via IIS!). Then update the reference to Startup.cmd in ServiceDefinition.csdef to ..\Startup\Startup.cmd, and update the reference to RoleStartup.ps1 in Startup.cmd to ..\Startup\RoleStartup.ps1. This works on the fact that the working directory is bin and uses ..\Startup to always locate the Startup directory relative to it.
You don't need to set the executionpolicy within your cmd - just call the script. Also, you should use a relative path because you can't rely that there is C disk.
Change your batch to:
powershell -executionpolicy unrestricted -file .\RoleStartup.ps1
Right click on the RoleStartup.ps1 and Startup.cmdin Visual Studio and ensure that the Copy to Output directory is set to copy always.
If this still doesn't work, remove the startup call in your csdef, deploy the service, rdp into it and try to invoke the script by yourself to retrieve any errors.
Edit:
Try to adopt your script as below:
Import-Module WebAdministration
$site = $null
do # gets the first website until the result is not $null
{
$site = Get-WebSite | select -first 1
Sleep 1
}
until ($site)
# get the appcmd path
$appcmd = Join-Path ([System.Environment]::GetFolderPath('System')) 'inetsrv\appcmd.exe'
# ensure the appcmd.exe is present
if (-not (Test-Path $appcmd))
{
throw "appcmd.exe not found in '$appcmd'"
}
# The rest of your script ....
I've found it easier in the past to not copy the content to the output directory. I have approot\bin as the working directory. My startUp task element's commandLine attribute uses a relative reference to the .cmd file like so:
The .cmd file references the PowerShell script relatively from the working directory as well:
PowerShell -ExecutionPolicy Unrestricted -f ..\StartUp\RoleStartup.ps1
Ok,
So I am coming back to this after many different attempts to make it work.
I have tried using:
Startup config in the ServiceDefinition.csdef
I have tried registering a scheduled task on the server that scans the Windows Azure log looking for [System[Provider[#Name='Windows Azure Runtime 2.6.0.0'] and EventID=10004]]
Nothing worked either due to security or the timing of events and IIS not being fully setup yet.
So I finally bit the bullet and used my Webrole.cs => public override bool OnStart() method:
Combined with this in the ServiceDefinition.csdef:
Now it all works. This was not the most satisfying result as some of the other ways to do it felt more elegant. Also, many others posted that they got the other ways of doing it to work. Maybe I would have got there eventually but my time was restricted.
thanks
Russ

lost PATH while trying to set custom winlogon shell in WindowsXP

I have changed the shell key in windows registry to gain custom shell (Kiosk usage):
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Winlogon]
I set shell key to a batch file which runs two applications as below:
start "myFirstAppTitle" "myAppPath\myApp1.exe"
start "mySecondAppTitle" "myAppPath\myApp2.exe"
Each application runs but the second application which needs some files to be excuted throws an error which says could not find dependency files. whereas the dependency files are adjoining to the exe file and the mentioned app works fine, when starts from startup.
Meanwhile when i run the batch file manually it rusn fine.
I added the PATH command to the batch file but it did't work too.
Change the batch file to this:
set PATH=%PATH%;C:\MyAppPath
start "myFirstAppTitle" "myApp1.exe"
start "mySecondAppTitle" "myApp2.exe"
If you start executables without an absolute path, the path is relative to the current working directory. Also, when you specify an executable with a relative path, %PATH% is not searched for a matching subfolder with a matching executable.
Since the script worked when you manually started it, your working directory probably was C:\. However, when run at logon as a replacement shell, the working directory is most likely "%SystemRoot%\system32".
The problem solved strangely, i removed the title parameter of start command and it worked. In fact i used start command this fashion:
set PATH=%PATH%;C:\MyAppPath
start myapp.exe
start myapp2.exe