2012-06-21 56 views
0

我期望做的事情看起來相當簡單,但我無法弄清楚。我正在尋找運行Powershell腳本來啓動RDP會話,將文件複製到c:\目錄,然後從命令行運行該文件。我希望它循環,從csv文件獲取參數,例如服務器IP,用戶名和密碼。因此,在本質的步驟將是如下...使用powershell運行mstsc.exe

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 

我想看看是否有人可以幫助我走出this.I是新來的電源外殼,並已能夠通過它的很多工作。如果有人能指引我正確的方向,或者甚至給我提供代碼來填補空白,我會大大地appreaciate它。

爲了更加具體,我試圖做的是在機器上安裝代理。該過程需要通過rdp登錄到服務器,從命令行運行靜默安裝(msi)程序包並將其提供。我想要做的是爲我的客戶提供一個電子表格,讓他填寫詳細信息,例如服務器地址,用戶名和密碼。我轉向csv,運行powershell,並通過該腳本安裝許多代理,本質上是一種自動化過程。希望能夠更好地解釋它。

這裏是我迄今爲止...

的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}") 

這隻會推出MSTSC會議,所以它只是一塊拼圖,我需要什麼那麼要將文件複製到該設備,然後運行cmd.exe和一個命令並註銷。

對於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}") 
} 

幾乎同樣的事情,但我可以直接從膩子運行命令,使這項工作並沒有複製文件,我可以得到它的另一種方式運行一些命令。

任何與此有關的幫助或想法是appreaciated,謝謝。

+0

psexec怎麼樣? –

+0

我已經編輯了我迄今爲止的原始文章。 PSEXEC並沒有真正發揮作用,因爲從yunderstandings中,yo yo不得不在所有我希望訪問的設備上啓用它。如果這樣可以消除這種需求,那麼一旦我到達設備,我只需要將文件複製到它並運行單個命令進行靜默安裝。用mstsc,我可以開始,但我無法找到一種方法來輸入一個命令到一個Windows會話。使用unix相當簡單,我只是使用putty和SendKeys函數,並且未經測試,但似乎可行。 – user1462832

+1

如果所有服務器上都安裝了powershell 2,那麼請進行WS管理:安裝:http://technet.microsoft.com/en-us/magazine/ff700227.aspx&Use:http://technet.microsoft。 COM/EN-US /庫/ dd819505.aspx –

回答

0

我看起來像你選擇錯誤的方式。 PowerShell有很大的遠程能力,因此,如果您所有的客戶端有Windows管理(包含默認在Vista中,七,八包),你可以嘗試以下的事情:

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

請讓我們知道,如果它的工作原理在你的環境中。

0

MSTSC採取$計算機作爲一個參數使用MSTSC -v $計算機 添加用戶可以通過cmdkey做,你可以保存在遠程計算機

0

PSSession中和(可能)的Plink的組合的憑據。 exe從Putty的下載頁面,應該可以幫助你實現你的目標。

New-PSSession和Enter-PSSession cmdlet將允許您在Windows目標中使用遠程PowerShell會話(假定啓用WinRM)。從那裏你可以在你的目標上執行PShell命令。

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

或者你可以使用Invoke-Command

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

對於Linux系統,你也許可以利用砰砰。exe通過SSH運行遠程腳本。不是我以前試過的東西,但它應該可以正常工作。

此鏈接可能有一些幫助:http://the.earth.li/~sgtatham/putty/0.53b/htmldoc/Chapter7.html