通过PSSession发送文件

21

我刚刚花了几个小时搜索如何在活动的 PSSession 中发送文件的解决方案。结果是什么也没有。我正在尝试在一个活动的会话上调用一个命令,这个命令应该从网络存储中复制一些东西。所以,基本上就是这样:

icm -Session $s {
Copy-Item $networkLocation $PCLocation }
由于“第二跳”问题,我不能直接这样做,而且因为我正在运行win server 2003,我无法启用CredSSP。我可以先将文件复制到我的计算机上,然后再发送/推送它们到远程机器,但是该怎么做呢?我尝试过PModem,但据我所见它只能拉取数据而无法推送。
任何帮助表示感谢。

1
为什么不使用网络共享来复制文件呢? - JPBlanc
1
不错,但是上级机构不批准那个 :) - kalbsschnitzel
1
如果您可以在AD中启用远程计算机的“信任委派”,那么您就可以在没有CredSSP的情况下执行第二次跳转。 - Jason Stangroome
5个回答

44

现在在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

请查看此处获取更多示例,或者您可以查看官方文档


我如何使用计算机名称列表来完成这个任务? - readytotaste
你需要为每个目标会话多次调用Copy-Item。 - Tamir Daniely

21

如果是一个小文件,你可以将文件的内容和文件名作为参数发送。

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

如果文件太长而无法适应会话的限制,您可以将其分块读入,并使用类似的技术将它们附加在目标位置。


PowerShell 5+内置支持此功能,详见David's answer


1
$f == $filename$c == $contents - Jay Sullivan
2
$filename和$contents是脚本块中的参数名称。$f和$c是传递给脚本块的变量。 - Mike Shepard

4

我之前也遇到了同样的问题,并发布了一个证明概念,用于在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
}
一些小的修改就可以让它接受一个会话作为参数,而不是开始一个新的。当传输大文件时,我发现目标计算机上远程服务的内存消耗可能会变得非常大。我怀疑 PS Remoting 并不是真正设计成这种方式使用的。

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

实际上不知道最大文件大小限制是多少。


什么是 get-data - Mark
那是我凭记忆打出的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  

如果您没有在同一行上提供用户凭据,系统将提示您输入凭据:
>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 USE /? 命令获取完整的语法。
>net use /?
The syntax of this command is:

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

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

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

NET是系统文件夹中的标准外部.exe命令,在Powershell中可以正常工作。


升级您的PowerShell到v5+不是更容易吗? - Liam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接