Call batch file with wildcard pattern - powershell

I'm relatively new to batch scripts.
The below command with * displays all matching files
dir deploy*.bat
But the same does not work with call command
call deploy*.bat
It results in
'deploy*bat' is not recognized as an internal or external command,
operable program or batch file.
How can work this around?
Background:
On a different groovy script, based on the parameter passed, I'm either deploying deploy_sit.bat or deploy_uat.bat. Irrespective of the file deployed, I want to be able to call the file deploy*.bat

You could use the Resolve-Path cmdlet to do that:
call (Resolve-Path deploy*.bat).Path

Related

Why a Powershell script executes on a previous version of a file that has been changed?

I am trying to modify several values of a PowerShell script template prior to execution. The approach I took was to use the Get-Content command to read the template file, then to use the replace operand to replace the content with a content of my choosing, and then the Set-Content command to update the file, then execute the script. However, according to the error messages that I encounter, it seems that the modified file is not running, but the template one.
The thing I find hard to understand is why, as I use the Get-Content once again and print the result prior to execution, where I witness that the file did change.
I use simple PowerShell scripting with no threads or so ever, the script is being executed on an Azure VM via the Run-Command feature. I wonder why it happens.
Can anyone please explain?

Unable to call script function from command line

I am trying to call a function in a PowerShell script in the same directory. However, when I call it, I get this error:
The term 'functionName' is not recognized as the name of a cmdlet, function, script...
Any idea why this occurs? I also tried dot loading the script first like this before calling the function:
.\Script.psm1
.\Script.psm1 would run the script (script module, actually) in a child context. To be able to use functions from it in the current context you need to run/load it in the current context. That can be done via dot-sourcing for regular scripts, or via Import-Module (for modules).

Powershell Function Not Recognised

I've been putting my first powershell scripts together for the last couple of hours and I keep seeing an error that I can't seem to get to the bottom of.
I'm using the Powershell ISE tool to write and run the scripts.
To see if its something in my script I've created a super simple test script and I'm seeing the same problem. The entire test script is:
Test;
function Test
{
New-Item C:\Users\jgreen\Desktop\jammer\ -type directory
}
When I hit the Run Script button the error produced is:
Test : The term 'Test' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
If I simply hit the Run Script button again, it work and does the job. I simply do not understand what is wrong. I simply don't get it. Is there a problem with my script or not?
Why would a script that works bomb out the first time after opening the script in PS ISE?
You are calling the function before it is defined. The reason it works the second time, is a result of the first run. When it is ran the first time it's defining the function, so when you run the script the second time it knows what the function is.
You need to declare your function before you invoke it. It works the second time because then it's been declared. Think of how this would work if you were just at a powershell command line and you typed: "Test;" What would you expect to happen?

TeamCity running Powershell script, but failing to execute a batch file

I am currently in the process of implementing a deployment method using Teamcity, which runs a Powershell script on my Build Agent, which then configures my Production environment etc.
I have a problem with the Powershell script though, in that it can't seem to run the batch file from it.
The script runs perfectly if I run it manually, it only fails when run via TeamCity.
In the build log I am getting the error:
'myBatchFile.bat' is not recognized as an internal or external command, operable program or batch file.
The batch file and the powershell script are in the same directory and the batch file is called as such:
cmd /c Deploy.bat
I have my TeamCity configuration set up to have the build step for this as:
Script: File
ScriptExecutionMode: Execute script with -File argument
Script Arguments: None
Additional CMD line params: None
I had originally not used the cmd to try to execute the batch file, but executing the batch file like .\Deploy.bat did not seem to work either.
Is there an additional thing I need to set up in order to get the batch file to run? The rest of the script runs fine, just the call to the batch that doesn't.
This is a bit of a wild stab as it's difficult to predict what's happening, but from the description it seems like the path is been altered in the script and it's also dynamic as TeamCity creates temp directories, but if you replace:
cmd /c Deploy.bat
with
cmd /c "$(Split-Path $myinvocation.MyCommand.Path)\Deploy.bat"
then I think this will be able to located the deploy script.
Let me know how it goes.

Execute batch file(in turn executes exe) from powershell

I am trying to execute a install activeMq service from powershell, for which I am trying to call a batch file (which inturn calls the wrapper.exe) using:
& "C:\apache-activemq-5.6.0\bin\win64\InstallService.bat"
I am getting '"wrapper.exe"' is not recognized as an internal or external command,or batch file'
But when I execute InstallService.bat from command prompt I am able to run the service
Any help would be appreciated
Thanks,
Anandh
try this
& "cmd.exe /c C:\apache-activemq-5.6.0\bin\win64\InstallService.bat"
It sounds like you're using a relative path to call "wrapper.exe" within the batch script. If this is the case, replace it with the full path to the executable.