0

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...

  1. import infor from the csv file to define variables
  2. copy specefied file (then loop)
  3. launch mstsc.exe
  4. enter server IP, username, password
  5. paste copied file into the c:\ directory
  6. launch cmd.exe
  7. Run the file that was copied to the c:\ directory
  8. 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.

user1462832
  • 11
  • 3
  • 6

2 Answers2

1

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"

}
LT-
  • 673
  • 2
  • 7
  • 19
0

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.

Mitul
  • 9,734
  • 4
  • 43
  • 60
  • To be more spacific as to what I am trying to do is install an agent onto a machine. The process would entail logging onto a server via rdp, running a silent install (msi) package from a command line and thats it. What I would like to do is provide a spreadsheet to my client and have him fill out the details, such as server address, username, and password. The I and turn to a csv, run the powershell, and install many of the agents via that script, sort of an automated process in essence. Hope that explains it a little better. – user1462832 Jun 20 '12 at 14:35
  • Any help on how I can run and launch the mstsc.exe and try to do what I am looking to do? I have tried the pssession, and it would be a great alternative, however, it would require additional config on my clients side so it wouldnt be an option' – user1462832 Jun 21 '12 at 01:07
  • You can launch mstsc.exe using powershell but then you will have to manually do the task from there on. With PSSessions you go into that server and run commands on that server. – Mitul Jun 21 '12 at 13:06
  • dont you have to enable PSSessions on all servers that I am wanting to establish a connection with? If so that would eliminate the need as I would need to only access them once to run a single command for a silent install. – user1462832 Jun 22 '12 at 00:53
  • You have enable remoting on those all servers. yes. Enable-PSRemoting – Mitul Jun 22 '12 at 13:28