TFS Run batch script on a remote server with admin permission - batch-file

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'"}

Related

How to run sql script through powershell (Scripts/Commands)

I am trying to run an sql script by writing powershell script(commands). I am using below Invoke-Command for running the sql script.
Invoke-Command -Session $sess -ScriptBlock {D:\000000000041906_JCWJOUYKDB\dm_sql1.sql}
But the script is not executing, but if I am running a bat script it is running perfectly.
Invoke-Command -Session $sess -ScriptBlock {D:\000000000041906_JCWJOUYKDB\dm_os1.bat}
The bat script are just making directories by connecting to remote machine, where as in sql scripts I have define connecting string for sql server and from there create tablespaces.

Using a batch file on a remote computer's powershell scripts

When using a command like
powershell -command "\\%host1%\supportfiles\mypowershellscript"
from my central server to a remote computer, would it be using that powershell on the remote computer or on my own computer when I run it using the batch?
When you execute:
PowerShell -command <path to a script>
The script, whether it is located on the local machine or on a remote machine, will execute locally. If you want to execute some PowerShell script remotely, you need to enable remoting on the remote machine using Enable-PSRemoting -force. Then on the local machine, you have to execute your script as administrator and your account also has to have admin privileges on the remote machine. Inside your script you can execute parts of the script remotely like so:
$session = New-PSSession remoteComputerName
Invoke-Command -Session $session -Scriptblock { ... script to execute on remoteComputerName ...}
...
Remove-PSSession $session

Batch File fails when launched from Outlook

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

How to Acess Remote Computer Folders in PS SQLSERVER:\>

Recently I came to know how to use Powersehll commands for executing SQL queries as mentioned below:
Import-Module “sqlps” -DisableNameChecking
$ds=Invoke-Sqlcmd -Query $Query -Database $Database -ServerInstance $server -ConnectionTimeout $ConnectionTimeout -QueryTimeout $QueryTimeout
At this time Powershell ISE output Console will be in
PS SQLSERVER:\>
I was able to perform Select and Update queries without any error. The problem is faced when I wanted to access one of the file in remote computer. I tried to access the file as below:
PS SQLSERVER:\> Get-Content -Path \\Server\D$\Log\App.log
Even tried, just to list files/folders as
PS SQLSERVER:\> ls \\Server\D$\Log\
Iam getting below errors:
Get-Content : Cannot find path '\Server\D$\Log\App.log' because it does not exist.
ls : Cannot find path '\Server\D$\Log\' because it does not exist.
Need not to say- folder and file exists on that server as I was able to access the same when Powershell is running without SQL module. I mean console was showing just
PS C:\User\abc\
Sorry for this long story-- now, simple question!
How to access remote files/folders when Shell is in SQLSERVER console?
Thanks in Advance!
-Raj
You should be able to use the filesystem provider explicitly like this:
ls filesystem::\\server\d$\log
Just change your directory like so:
cd c:\
then:
ls \\server\d$\log
you can then change back to sql server by doing the same:
cd sqlserver:\
The powershell providers can be used just like you would a normal drive letter, pretty cool.
I believe it's running the commands remotely on the SQL server, which makes accessing data on another server from there a "double hop" scenario, and you'll need CredSSP auth for that to work.....

Powershell RDP Session and CLI Script

What I am looking to do seems fairly simple but I cant figure it out. I am looking to run a Powershell script to launch an RDP session, copy a file to the c:\ directory, then run that file from a command line. I would like it to loop, getting the paramaters froma csv file, such as server IP, username, and password. So in essence the steps would be as follows...
import infor from the csv file to define variables
copy specefied file
(then loop)
launch mstsc.exe
enter server IP, username, password
paste copied file into the c:\ directory
launch cmd.exe
Run the file that was copied to the c:\ directory
log off server
I wanted to see if someone could help me out with this.I am new to power shell and have been able to work through a lot of it. If someone could point me in the right direction, or even provide me the code to fill in the blanks, I would greatly appreaciate it.
I have done remote installs using psexec. psexec \\servername -u domain\usernamr -p password cmd /c "msiexec /i program.msi
PSexec download: https://learn.microsoft.com/en-us/sysinternals/downloads/psexec
This means instead of RDP you will use psexec to run the install remotely.
I have created a small PowerShell script to get you started. So let's assume your CSV file (c:\info.csv) has three columns ServerName, UserName, Password.
Run the below code, and it should work but make sure the change the first 4 lines as per your environment. Start by putting one server to observe the script behavior.
# Set intial variables
$CSVFile = "c:\info.csv"
$MSI = "\\servername\sharename\setup.msi"
$MSILog = "c:\Windows\temp\setup.log"
$Domain = "YourDomain"
# Import info from CSV file
$Servers = import-csv $CSVFile
# loop through each server
foreach ($server in $servers) {
# run psexec on each server to install a program
psexec \\$server.servername -u $Domain\$server.username -p $server.password -h cmd /c "msiexec /i $MSI /quite /l*v $MSILog"
}
I recommend you using this command because I don't exactly what you are trying to do.
Get-help Import-CSV
Get-help about_remoting this will avoid doing the mstsc.exe thing for you.
Enter into the session and Invoke-Command against this session and you can run commands on that server.
$session = New-PSSession -ComputerName Server1 -Credentials Get-Credential
Invoke-Command -Session $session -ScriptBlock {
}
Inside the Script block specify you powershell command to copy files and run them.

Resources