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.

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.

Here is what I have so far...

Windows....

$computers= gc "C:\Users\Desktop\Powershell\DeviceList.txt"
foreach ($computername in $computers) 
{
mstsc
Start-Sleep -s 2 
[System.Windows.Forms.SendKeys]::SendWait("$computername{ENTER}")

That will only launch the mstsc session, so its only a piece of the puzzle, what I need to do then is copy a file to that device and then run cmd.exe and a single command and log out.

For UNIX>>>

$computers= gc "C:\Users\Desktop\Powershell\DeviceList.txt"
foreach ($computername in $computers) 
{
c:\putty.exe
Start-Sleep -s 2
[System.Windows.Forms.SendKeys]::SendWait("$computername{ENTER}")
}

Pretty much the same thing, but I can run commands directly from putty to make this work and do not have to copy a file as I can get it another way by running a few commands.

Any help or ideas with this is appreaciated, thanks.

user1462832
  • 11
  • 3
  • 6
  • I have edited my orig post with what I have so far. PSEXEC doesnt really work so well, as from yunderstandings, dont yuo have to enable it on all the devices I am looking to access? If so that would eliminate the need, once I get to the device I just ned to copy a file to it and run a single command for a silent install. With mstsc, I can get it started, but I cant figure out a way to input an commands into a windows session. With unix its fairly easy, I just use putty and the SendKeys function and, untested, but seems to work. – user1462832 Jun 22 '12 at 00:43
  • 1
    If powershell 2 is installed on all your servers, then go for WS-management: Setup: http://technet.microsoft.com/en-us/magazine/ff700227.aspx & Use: http://technet.microsoft.com/en-us/library/dd819505.aspx – Mat M Sep 24 '12 at 00:23

3 Answers3

0

I looks like you choose wrong way. PowerShell has great remote abilities, so if all your clients have Windows Management (which is included to default packages in Vista, Seven and Eight) you can try following thing:

# Comma separated list:
$listRaw = @"
Computer,User,Password
computer1,"Domain\user1","passw1"
computer2,"Domain\user2","passw2 with spaces"
"@

# Parse CSV list
$list = $listRaw | ConvertFrom-CSV -Delimiter ','

# Iterate targets
foreach ($target in $list) {
    Write-Host "Connecting to $($target.Computer) as $($target.User)"

    # Creating credential from username and password
    $SecurePassword = $target.Password | ConvertTo-SecureString -AsPlainText -Force
    $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $target.User, $SecurePassword

    # Creating remote session to that computer
    # Using given credentials
    $session = New-PSSession -ComputerName $target.Computer -Credential $Credentials

    # Invoking actions on that remote session
    Invoke-Command -Session $session -ArgumentList $target.Computer -ScriptBlock {
        Param($computerName)

        # this code executes on remote computer
        "Hello from $computerName"
    }
}

Please, let us know, if it works on your environment.

Anton
  • 10,890
  • 8
  • 45
  • 54
0

mstsc take $Computer as an argument using mstsc -v $Computer Adding a user can be done via cmdkey and you can save the credentials of the remote computer

0

A combination of PSSession and (possibly) Plink.exe from Putty's download page, should help you get to your goal.

The New-PSSession and Enter-PSSession cmdlets will allow you to use a remote PowerShell session with your Windows targets (Assuming WinRM is enabled). From there you can execute PShell commands on your target.

$session - New-PSSession -computerName [targetName/ip] -Credential (get-credential)
$session | Enter-PSSession
{ Execute Silent install }
Exit-PSSession
$session | Disconnect-PSSession

Or you can use Invoke-Command

Invoke-Command -ScriptBlock { install.exe /s } -computerName [target] -credential (Get-Credential)

For the Linux boxes, you might be able to utilize PLink.exe to run remote scripts via SSH. Not something I've tried before, but it should work fine.

This link might be of some help: http://the.earth.li/~sgtatham/putty/0.53b/htmldoc/Chapter7.html

Scarecrow
  • 23
  • 3