2012-05-17 89 views
19

我剛剛燒了幾個小時尋找解決方案,通過活動的PSSession發送文件。結果是nada,niente。我試圖通過活動會話在遠程計算機上調用命令,該命令應從網絡存儲中複製某些內容。所以,基本上,這是它:通過PSSession發送文件

icm -Session $s { 
Copy-Item $networkLocation $PCLocation } 

因爲「第二跳」的問題,我不能直接這樣做,因爲我跑贏服務器2003年,我不能啓用的CredSSP。我可以先將這些文件複製到我的電腦,然後發送/推送到遠程機器,但是怎麼做?我試過PModem,但正如我所看到的,它只能拉數據而不推。

任何幫助appreaciated。

+1

爲什麼你不使用網絡共享tu複製你的文件? – JPBlanc

+1

不錯,但上級主管部門不贊同:) –

+1

如果您可以在AD中啓用遠程計算機爲「可信代理」,那麼您可以在沒有CredSSP的情況下執行第二跳。 –

回答

17

如果它是一個小文件,您可以發送文件的內容和文件名作爲參數。

$f="the filename" 
$c=Get-Content $f 
invoke-command -session $s -script {param($filename,$contents) ` 
    set-content -path $filename -value $contents} -argumentlist $f,$c 

如果文件太長,無法在任何會話的限制是,你可以閱讀的塊中的文件,並在目標位置使用類似的技術來追加在一起

+1

'$ f == $ filename'和'$ c == $ contents'? –

+2

$ filename和$ contents是腳本塊中參數的名稱。 $ f和$ c是傳遞給scriptblock的變量。 –

2

我前一陣面臨同樣的問題,並且通過PS Remoting會話發送文件的概念驗證。你會發現這裏的腳本:

https://gist.github.com/791112

#requires -version 2.0 

[CmdletBinding()] 
param (
    [Parameter(Mandatory=$true)] 
    [string] 
    $ComputerName, 

    [Parameter(Mandatory=$true)] 
    [string] 
    $Path, 

    [Parameter(Mandatory=$true)] 
    [string] 
    $Destination, 

    [int] 
    $TransferChunkSize = 0x10000 
) 

function Initialize-TempScript ($Path) { 
    "<# DATA" | Set-Content -Path $Path 
} 

function Complete-Chunk() { 
@" 
DATA #> 
`$TransferPath = `$Env:TEMP | Join-Path -ChildPath '$TransferId' 
`$InData = `$false 
`$WriteStream = [IO.File]::OpenWrite(`$TransferPath) 
try { 
    `$WriteStream.Seek(0, 'End') | Out-Null 
    `$MyInvocation.MyCommand.Definition -split "``n" | ForEach-Object { 
     if (`$InData) { 
      `$InData = -not `$_.StartsWith('DATA #>') 
      if (`$InData) { 
       `$WriteBuffer = [Convert]::FromBase64String(`$_) 
       `$WriteStream.Write(`$WriteBuffer, 0, `$WriteBuffer.Length) 
      } 
     } else { 
      `$InData = `$_.StartsWith('<# DATA') 
     } 
    } 
} finally { 
    `$WriteStream.Close() 
} 
"@ 
} 

function Complete-FinalChunk ($Destination) { 
@" 
`$TransferPath | Move-Item -Destination '$Destination' -Force 
"@ 
} 

$ErrorActionPreference = 'Stop' 
Set-StrictMode -Version Latest 

$EncodingChunkSize = 57 * 100 
if ($EncodingChunkSize % 57 -ne 0) { 
    throw "EncodingChunkSize must be a multiple of 57" 
} 

$TransferId = [Guid]::NewGuid().ToString() 


$Path = ($Path | Resolve-Path).ProviderPath 
$ReadBuffer = New-Object -TypeName byte[] -ArgumentList $EncodingChunkSize 

$TempPath = ([IO.Path]::GetTempFileName() | % { $_ | Move-Item -Destination "$_.ps1" -PassThru}).FullName 
$Session = New-PSSession -ComputerName $ComputerName 
$ReadStream = [IO.File]::OpenRead($Path) 

$ChunkCount = 0 
Initialize-TempScript -Path $TempPath 

try { 
    do { 
     $ReadCount = $ReadStream.Read($ReadBuffer, 0, $EncodingChunkSize) 
     if ($ReadCount -gt 0) { 
      [Convert]::ToBase64String($ReadBuffer, 0, $ReadCount, 'InsertLineBreaks') | 
       Add-Content -Path $TempPath 
     } 
     $ChunkCount += $ReadCount 
     if ($ChunkCount -ge $TransferChunkSize -or $ReadCount -eq 0) { 
      # send 
      Write-Verbose "Sending chunk $TransferIndex" 
      Complete-Chunk | Add-Content -Path $TempPath 
      if ($ReadCount -eq 0) { 
       Complete-FinalChunk -Destination $Destination | Add-Content -Path $TempPath 
       Write-Verbose "Sending final chunk" 
      } 
      Invoke-Command -Session $Session -FilePath $TempPath 

      # reset 
      $ChunkCount = 0 
      Initialize-TempScript -Path $TempPath 
     } 
    } while ($ReadCount -gt 0) 
} finally { 
    if ($ReadStream) { $ReadStream.Close() } 
    $Session | Remove-PSSession 
    $TempPath | Remove-Item 
} 

一些小的改動將允許它接受一個會話開始一個新的參數,而不是它。我發現在傳輸大文件時,目標計算機上Remoting服務的內存消耗可能會變得非常大。我懷疑PS Remoting的設計並不是真正用於這種方式。

31

這是現在可以在PowerShell中/ WMF 5.0

Copy-Item具有-FromSession-toSession參數。您可以使用其中之一併傳入會話變量。

例如。

$cs = New-PSSession -ComputerName 169.254.44.14 -Credential (Get-Credential) -Name SQL 
Copy-Item Northwind.* -Destination "C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008R2\MSSQL\DATA\" -ToSession $cs 

查看更多的例子在https://richardspowershellblog.wordpress.com/2015/05/28/copy-files-over-ps-remoting-sessions/

0
$data = Get-Content 'C:\file.exe' -Raw 
Invoke-Command -ComputerName 'server' -ScriptBlock { $using:data | Set-Content -Path 'D:\filecopy.exe' } 

真的不知道,最大文件大小限制是什麼。

+0

什麼是get-data? – Mark

+0

這將是我打字我的頭頂powershell。本應該是'Get-Content' – mtnielsen

1

NET USE允許你添加一個本地驅動器號遠程系統,然後再允許你使用你的PSSession中的驅動器盤符,或者即使沒有PSSession的。如果您沒有Powershell v5,這很有用。0,即使你做,

您可以使用遠程計算機的名稱或IP地址作爲遠程UNC路徑的一部分,您可以指定在同一行上的用戶名和密碼憑據:

NET USE Z: \\192.168.1.50\ShareName /USER:192.168.1.50\UserName UserPassword 

又如:

NET USE Z: \\RemoteSystem\ShareName /USER:RemoteSystem\UserName UserPassword 

OR

NET USE Z: \\RemoteSystem\ShareName /USER:Domain\UserName UserPassword 

如果不提供用戶CR在同一行edentials,系統將提示您爲他們:

>NET USE Z: \\192.168.1.50\ShareName 
Enter the user name for '192.168.1.50': 192.168.1.50\UserName 
Enter the password for 192.168.1.50: ***** 
The command completed successfully. 

您可以刪除驅動器號,當你用下面的完成:

NET USE Z: /delete 

你可以用NET完整的語法使用 /?

>net use /? 
The syntax of this command is: 

NET USE 
[devicename | *] [\\computername\sharename[\volume] [password | *]] 
     [/USER:[domainname\]username] 
     [/USER:[dotted domain name\]username] 
     [/USER:[[email protected] domain name] 
     [/SMARTCARD] 
     [/SAVECRED] 
     [[/DELETE] | [/PERSISTENT:{YES | NO}]] 

NET USE {devicename | *} [password | *] /HOME 

NET USE [/PERSISTENT:{YES | NO}] 

NET是系統文件夾中的標準外部的.exe命令,並在PowerShell中工作得很好。

+0

將PowerShell升級到v5 +不是很容易嗎? – Liam