We are trying to Start the WILDFLY Server using the powershell script. Here is the code
cmd.exe /c $env:JBOSS_HOME\bin\standalone.bat
write-host "Before Condition Check"
if ($?)
{
write-host "WILDFLY Server STARTED....."
}
else
{
$JBossResult = "FAILED"
write-host "Error While Starting WILDFLY Server"
}
The server is getting started successfully without any issue, but thing is that it is not coming out of the terminal, hence my next part of the code is not getting executed.
Is there anyway to come out of the terminal without stopping the server, so that I continue to my next step.
Replace this:
cmd.exe /c $env:JBOSS_HOME\bin\standalone.bat
with this:
start-process -filepath "$env:JBOSS_HOME\bin\standalone.bat"
Launching cmd.exe directly causes the script to wait for it to exit before continuing.
Related
We have Alteryx on several servers and want to be able to run jobs remotely from either command line or powershell, not through Alteryx API.
I have run alteryx flows locally from command line and powershell locally for years but now with multiple servers we have need to execute certain flows this way but to trigger from a react web app. We tried the API and it works but jobs often queue for too long and we need near realtime response and these jobs run in seconds. Our solution is to trigger outside the Alteryx API for immediate execution on a worker node with excess resources.
Our problem is that when testing the script which works well if run locally it runs all other commands fine except the execution of the alteryxenginecmd.exe program.
if I use Start-Process with passthru it generates a process but no output from the alteryx job and no errors detected.
If I use Call operator & if fails saying it can;t find the workflow, so the alteryxenginecmd.exe is firing but not reading or receiving the full path of the flow to run as the parameter. but it works fine locally.
I have used creds to force a sign on to clear any credential issue with no change in result.
I know Powershell has some oddities and maybe there is some tweak to how a command must be specified if the script is run from a remote server?
This is the script I want to run from remote server which does work fine in any of the 4 scenarios shown
$rundate = (Get-Date -format 'u') -replace "-","" -replace ":","" -replace " ","-"
$Pgm2Run = "\<somepath>\RunScriptRemote\RunRemoteTest2 - NoDB.yxmd"
$Bat2Run = "\\<somepath>\RunScriptRemote\RunRemoteTestJob.bat"
$JobLogFile = "\<somepath>\RunScriptRemote\LogsJob\JobTranscript $rundate.txt"
$ScriptLogFile = "\<somepath>\RunScriptRemote\LogsScript\RunJobTranscript $rundate.txt"
Start-Transcript -Path $ScriptLogFile
echo " "
echo "$rundate Start --> "
Echo "$rundate Before Start-Process"
#1 Start-Process -filepath "AlteryxEngineCmd.exe" -ArgumentList `"$Pgm2Run`" -RedirectStandardOutput $JobLogFile -Wait -PassThru
#2 Start-Process -filepath $Bat2Run -RedirectStandardOutput $JobLogFile -Wait -PassThru
#3 & AlteryxEngineCmd.exe "$Pgm2Run"
AlteryxEngineCmd.exe "$Pgm2Run"
echo "$rundate After Start-Process"
echo "$rundate End --> "
Stop-Transcript
This command run on Server1 to execute the script above on server2
Invoke-Command -ComputerName server2 -FilePath "\\<somepath>\RunScriptRemote\RunAlteryxAndLogIt.ps1"
And this is the result:
20200831-094309Z Before Start-Process
AlteryxEngineCmd.exe Version 2019.4.8.22007 © Alteryx, Inc. - All Rights Reserved.
Started running \\<somepath>\RunRemoteTest2 - NoDB.yxmd
Error - Alteryx Engine: Can't read the file "
Invoke-Command -ComputerName server2 -FilePath "\\<somepath>\RunScriptRemote\RunRemoteTest2 - NoDB.yxmd"
Finished in 0.020 seconds with 1 error
20200831-094309Z After Start-Process
I am not sure it is a powershell thing, it may well be an Alteryx thing because the issue seems to be executing that one exe properly. the script triggers properly from remote server and I tested other commands and all worked, I just can't seem to get the one executing this exe to work when run remotely though it works fine locally when same script is run locally. its like a barrier betweek a script running another script.
Any powershell experts familiar with running exe's remotely from another server?
I'm integrating Jenkins with a bunch of stuff and im using powershell to do this. I have a script on a remote machine that is executed after a build is successful on jenkins. This script does a bunch of stuff and then will restart the machine.
What i need to do is:
Return to jenkins that the script was successful (meaning that it will end the job as SUCCESS)
Then restart the machine
So far i have not managed to send 'EXIT 0' to jenkins and then restart the machine. There's anyway to do this?
Thanks in advance.
Code example:
Write-Host "Code example"
Exit 0 #for jenkins success
Restart-Computer -Force
This will host a seperate command prompt that runs async from the powershell script and restarts the computer in 3 seconds, enough time for powershell to return the exit code to jenkins.
Start-Process -FilePath "cmd.exe" -ArgumentList '/c "timeout /t 3 /nobreak && shutdown -r -f -t 0"' -WindowStyle Hidden
Exit 0
As noted in a comment by #Avshalom, your problem is that the Exit statement will unconditionally exit your script without ever executing the Restart-Computer command placed after it.
Restart-Computer, when executed locally, is invariably asynchronous, so your script will continue to execute, at least for a while.
You can therefore try to call Restart-Computer first, and exit 0 afterwards:
Write-Host "Code example"
Restart-Computer -Force
exit 0 # for Jenkins success
However, there's no guarantee that control will return to Jenkins in time and that Jenkins itself will have time to process the successful exit before it is shut down itself.
You can improve the likelihood of that with a delay, via a separate, asynchronously launched PowerShell instance[1], similar to the approach in Evilcat's answer:
Write-Host "Code example"
# Asynchronously start a separate, hidden PowerShell instance
# that sleeps for 5 seconds before initiating the shutdown.
Start-Process -WindowStyle Hidden powershell.exe -Args '-command',
'Start-Sleep 5; Restart-Computer -Force'
exit 0 # for Jenkins success
This still isn't a fully robust solution, however; a fully robust solution requires changing your approach:
Let your script indicate success only, without initiating a restart.
Make Jenkins test for success and, if so, call another script that unconditionally initiates a shutdown.
[1] As Evilcat points out, using a background job with Start-Job does not work, because on exiting the calling PowerShell session with exit the background jobs are terminated too.
PS Version: 2.0
Hi All,
I am trying to run the batch file from a powershell script using psexec.
The script runs fine while triggering manually or using windows task scheduler; however, powershell get triggered from Control-m but do not complete the part where psexec is used. The rest part of powershell script runs fine.
Below is the function which is not working, besides it do not give any error, it just freezes the script:
function Archive_Logs($Server_Name,$Tool_Path,$Name)
{
foreach($TPath in $Tool_Path){
C:\Windows\System32\PsExec.exe \\$Server_Name "$TPath\ziplogs.bat"
if($LastExitCode -eq 0)
{
"$Name Server logs archive Started successfully at $(Get-Date)" | Out-File $LOGFILE -Append
}
}
}
The account used by Control-M is local admin on the servers.
How are you calling the Powershell? When you create a .bat file and invoke it from there it fixes a lot of issues, e.g, bat file contains 1 line -
powershell -command "& C:\MyPSscripts\archiver.ps1"
See -
https://communities.bmc.com/thread/117415
I have a powershell script where I'm executing a node command which is meant to be executed by a TFS 2013 Build:
node "$Env:TF_BUILD_SOURCESDIRECTORY\app\Proj\App_Build\r.js" -o "$Env:TF_BUILD_SOURCESDIRECTORY\app\Proj\App_Build\build-styles.js"
$success = $?
if (!$success){
exit 1
}
When I run this script manually and the command fails $success is false and the script exits 1, but when the build executes the script and the node command fails, $success (and $?) is true.
What can change the behavior of powershell? I have no idea what else to try. So far I eliminated the following:
Changed the Build Service user to the same Admin user that executes the script manually
Tried executing the command with cmd /c node ...
Tried executing the command with Start-Process node...
Ran the Build Service interactively
Ran the build with both VSO Build Controller and an on premise Build Controller
Executed the script manually with the same command used by TFS (per the Build Log)
Thoughts?
Can we restructure this a bit so we have a better feel for what is happening? I tend to avoid $? because it is harder to debug and test with.
try
{
Write-Host "TF_BUILD_SOURCESDIRECTORY = $Env:TF_BUILD_SOURCESDIRECTORY"
$result = node "$Env:TF_BUILD_SOURCESDIRECTORY\app\Proj\App_Build\r.js" -o "$Env:TF_BUILD_SOURCESDIRECTORY\app\Proj\App_Build\build-styles.js"
Write-Host "Result = $result"
}
catch
{
Write-Error "Command failed"
Exit 1
}
Sometimes I wrap my command in a Start-Process -NoNewWindow -Wait just to see if that generates a different error message.
In your case, I would also try Enter-PSSession to get a non-interactive prompt on the TFS server. I have seen cases where powershell acts diferently when the shell is not interactive.
I've spent the last week developing a Powershell script. It now works ok when I start it from a Powershell window. However, I need the script to be called from TFS build. I've tried using the syntax Powershell & 'script' from a TFS Build InvokeProcess control but nothing seems to happen. So, I've gone back to basics and created the following script:
Write-Host " Write-Host line"
Write-eventlog -logname "Application" -source "BizTalkDeployment" -eventId "01" -entrytype "Information" -message "test"
I've saved the script as c:\temp\ps_test.ps1. I've opened a command window as admin and tried the following:
powershell & 'c:\temp\ps_test.ps1"
This puts the command line into powershell mode and I get:
PS c:\>
But nothing else happens.
At the end of my TFS build I have an InvokeProcess control that uses "Powershell" for its FileName property and then the following arguments:
String.Format(" ""& '{0}' '{1}' '{2}' '{3}' '{4}' '{5}' '{6}' '{7}' '{8}' '{9}' "" ", DeploymentScriptFileName, IO.Path.GetDirectoryName(DeploymentScriptFileName), "ExecuteBizTalkAppMSI.ps1", "bin\debug\x.Int.MIS-3.0.0.msi", "x.Int.MIS.Deployment.btdfproj", TargetServerPath, "d-vasbiz01", "c:\biztalkdeployment", "C:\Program Files (x86)\x.Int.MIS for BizTalk 2010\3.0", "BTSSvc*MIS*")
The build script runs ok but the InvokeProcess control seems to do nothing - just like my experience with the command line.
Can anyone see where I'm going wrong please?
Either
powershell -file C:\temp\ps_test.ps1
or
powershell "&{ <# code here #> }"