I have .bat files that execute PowerShell scripts. They run fine from Task Scheduler or when running the batch files myself, but when I run them from Outlook (either from VBA script or using a Rule) - they just exit immediately without completing.
they look like this:
*PowerShell -file C:\Users\tenba1\Documents\Scripts\Account_Recon.ps1*
I also tried this:
*Call PowerShell -file C:\Users\tenba1\Documents\Scripts\Account_Recon.ps1*
Any idea why this happens?
You need to unrestrict your execution policy in the script call.
Powershell.exe -ExecutionPolicy Unrestricted -File filedir\filename.ps1
Related
I am trying to run a batch script from paramiko ssh.
the batch script is like this:
start cmd.exe /k "cd C:\\Users\\Administrator\\iometer\\ && C:\\Users\\Administrator\\iometer\\IOmeter.exe /c C:\\Users\\Administrator\\iometer\\new_config.icf /r C:\\Users\\Administrator\\iometer\\iometer_result_v.csv"
If I run the batch script locally it works, but when I trigger the same batch script from paramiko exec_command() it doesn't start the IOMeter.exe application.
I tried giving relative path and then with absolute path. It did not help.
I tried running the whole batch script command from exec_command() itself and even that did not start the application in remote machine.
The issue is, if we run any batch script with exec_command in paramiko python, it executes command and immediately kills the session and all dependent sessions (or child sessions) opened by that exec_command sessions.
To resolve this issue, you can trigger the same batch script from powershell and add wait at the end. Or create a PowerShell script (temp.ps1) and add the command to run the batch script as a process.
sample batch file looks like this:(I am using notepad here for security concerns, but actually I am running a tool which runs IO stress on different disks parallelly and logs output to the file)
start cmd.exe /k \"cd C:\\Users\\Administrator\\automate && C:\\Users\\Administrator\\automate\\notepad.exe &exit"
start cmd.exe /k \"cd C:\\Users\\Administrator\\automate && C:\\Users\\Administrator\\automate\\notepad.exe &exit"
start cmd.exe /k \"cd C:\\Users\\Administrator\\automate && C:\\Users\\Administrator\\automate\\notepad.exe &exit"
Sample powershell script looks like this:
start-process 'cmd.exe' -ArgumentList "/c C:\\Users\\Administrator\\automate\\temp_batch.bat &exit" -Wait
So, total we have a batch script and a powershell run the batch script as a process.
In python just run this powershell script using paramiko exec_command.
cmd = "powershell -file C:\\Users\\Administrator\\automate\\temp3.ps1"
Sample python script would look something like this:
import paramiko
hostname = 'x.x.x.x'
port = 22
username = 'user'
password = 'pwd'
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname,port,username,password)
cmd = "powershell -file C:\\Users\\Administrator\\diskio\\temp3.ps1"
stdin,stdout,stderr=ssh.exec_command(cmd)
outlines=stdout.readlines()
result=''.join(outlines)
print (result)
print(stderr.readlines())
ssh.close()
I currently have Server A which is where my TFS and Build Agent is located. I then have Server B which is when my source code site. I am trying to set up a build definition and copies file from on location in server B to another and then build the solution.
However when I run this batch file as part of a build definition it is not creating folders where it need to be. I believe due to the agent not having correct permissions.
Is there a way to run the following batch script to run with Admin permission from a build definition.
You can try below workarounds:
Convert the batch script to PowerShell script, then copy the
PowerShell script to target machine and use the PowerShell on Target
Machines task to run the script. You can enter your admin user
and password using the task. Reference below screenshot.
Add a PowerShell task and run below script to call the cmd.exe to
run the batch script with an admin user account on target machine
(Copy the batch script to target machine first, in below sample I
copied the batch script to C:\Scripts\Test.bat):
Param(
[string]$computerName = "v-tinmo-12r2",
)
$Username = "Domain\user"
$Password = ConvertTo-SecureString "password-here" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($Username,$password)
Invoke-Command -ComputerName $computerName -Credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\Scripts\Test.bat'"}
I am trying to launch an Android emulator from a batch file inside my build definition, with the next command:
start /WAIT "Start Emulator" "C:\Program Files (x86)\Microsoft Emulator Manager\1.0\emulatorcmd.exe" launch /sku:Android /id:97522427-7A5E-4F3B-96A8-B9F9F0C0423A
I tried to add the build step as a command line, and a batch script.
Problem: The script is working right, and opening the emulator and wait for it to fully open, but once script finishes executing and console closes, the emulator is closing as well.
I tried to run the script directly on build server, and it works fine without closing emulator, but when queued as a build step, I am facing the above problem.
Question: How can I force the emulator to stay open after batch file finishes executing?
EDIT:
It looks like the build definition task terminates all processes it created in the defined step, I have tried multiple script, tried cmd /k and tried the /b and tried to create another batch file that actually calls this one or start it, yet no results. I am still waiting for any possible solution.
Alright, I tried a lot of scripts in batch files, and I tried to run it from command line, after a lot of time waste and getting tired, I decided to give PowerShell task a try to fix my problem. I ended up with this:
Start-Process -FilePath "C:\Program Files (x86)\Microsoft Emulator Manager\1.0\emulatorcmd.exe" -ArgumentList "launch /sku:Android /id:97522427-7A5E-4F3B-96A8-B9F9F0C0423A" -Verb runas
Start-Sleep -s 60
This made the emulator start, and stays running even after PowerShell script ends.
I'm a little confused how do I configure a new task in ConEmu to run launch powershell console with -File parameter. if I add -file "filename.ps1" then that file is executed and then console is closed, I'd like to console to rename open.
I am running a Powershell script from a bat file. These 2 files are placed on a network server and I am trying to run these on network server from my machine by accessing network server folder. I get following error:
CMD doesn't support UNC paths as current directories.
When I execute bat file , it tries to read script file from path
C:\Windows\System32
not from current directory which is set in bat file.
This script and bat file work fine when I run on my local machine.
I tried finding this on Google and possible solutions need to change some settings on network server which is not possible in my case.
What could be possible solution for this?
I am using PowerShell 2.0
Here is my bat file to run Powershell script
setlocal & pushd .
:getting current directory
cd /d %~dp0
Powershell.exe Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Powershell.exe . '%CD%\Hotfix-Automation-Installer.ps1' crpt
exit /B
It's like the message says: CMD doesn't support UNC paths as current directories, so you shouldn't use %CD% or . when you're working with UNC paths.
However, what's actually preventing your PowerShell script from being run is the single quotes around the path:
Powershell.exe . '%CD%\Hotfix-Automation-Installer.ps1' crpt
Single quotes are not valid quoting characters in CMD, so your script is actually trying to run the PowerShell script from a subfolder ' in the current directory. Which doesn't exist.
Change your script to this, and it should work just fine:
#echo off
powershell -ExecutionPolicy ByPass -File "%~dp0Hotfix-Automation-Installer.ps1" crpt