Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ public static Collection<PSObject> InvokeOnRunspace(PSCommand command, Runspace
$filePathName = $_.FullName

# Get file contents
$contentBytes = Get-Content -Path $filePathName -Raw -Encoding Byte
$contentBytes = if ($PSVersionTable.PSEdition -eq 'Desktop') {
Get-Content -Path $filePathName -Raw -Encoding Byte
} else {
Get-Content -Path $filePathName -Raw -AsByteStream
}

# Notify client for file open.
New-Event -SourceIdentifier PSISERemoteSessionOpenFile -EventArguments @($filePathName, $contentBytes) > $null
Expand Down
26 changes: 13 additions & 13 deletions src/System.Management.Automation/namespaces/FileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8420,7 +8420,6 @@ internal static class CopyFileRemoteUtils
[Parameter(ParameterSetName=""PSCopyFileToRemoteSession"")]
[Parameter(ParameterSetName=""PSCopyAlternateStreamToRemoteSession"")]
{0}

[string] $copyToFilePath,

[Parameter(ParameterSetName=""PSCopyFileToRemoteSession"", Mandatory=$false)]
Expand All @@ -8440,12 +8439,10 @@ internal static class CopyFileRemoteUtils

[Parameter(ParameterSetName=""PSTargetSupportsAlternateStreams"", Mandatory=$true)]
{0}

[string] $supportAltStreamPath,

[Parameter(ParameterSetName=""PSSetFileMetadata"", Mandatory=$true)]
{0}

[string] $metaDataFilePath,

[Parameter(ParameterSetName=""PSSetFileMetadata"", Mandatory=$true)]
Expand All @@ -8454,17 +8451,14 @@ internal static class CopyFileRemoteUtils

[Parameter(ParameterSetName=""PSRemoteDestinationPathIsFile"", Mandatory=$true)]
{0}

[string] $isFilePath,

[Parameter(ParameterSetName=""PSGetRemotePathInfo"", Mandatory=$true)]
{0}

[string] $remotePath,

[Parameter(ParameterSetName=""PSCreateDirectoryOnRemoteSession"", Mandatory=$true)]
{0}

[string] $createDirectoryPath,

[Parameter(ParameterSetName=""PSCreateDirectoryOnRemoteSession"")]
Expand Down Expand Up @@ -8601,7 +8595,13 @@ function PSCopyFileAlternateStreamToRemoteSession
CheckPSDriveSize $resolvedPath $fragment.Length

# Write the stream
Microsoft.PowerShell.Management\Add-Content -Path ($resolvedPath.ProviderPath) -Value $fragment -Encoding Byte -Stream $streamName -ErrorAction Stop
if ($PSVersionTable.PSEdition -eq 'Desktop') {{
Microsoft.PowerShell.Management\Add-Content -Path ($resolvedPath.ProviderPath) -Value $fragment -Encoding Byte -Stream $streamName -ErrorAction Stop
}}
else {{
Microsoft.PowerShell.Management\Add-Content -Path ($resolvedPath.ProviderPath) -Value $fragment -AsByteStream -Stream $streamName -ErrorAction Stop
}}

$op['BytesWritten'] = $fragment.Length
}}
catch
Expand Down Expand Up @@ -8886,7 +8886,6 @@ function PSCreateDirectoryOnRemoteSession
param (
[Parameter(ParameterSetName=""PSCopyFileFromRemoteSession"", Mandatory=$true)]
{0}

[string] $copyFromFilePath,

[Parameter(ParameterSetName=""PSCopyFileFromRemoteSession"", Mandatory=$true)]
Expand All @@ -8909,22 +8908,18 @@ function PSCreateDirectoryOnRemoteSession

[Parameter(ParameterSetName=""PSSourceSupportsAlternateStreams"", Mandatory=$true)]
{0}

[string] $supportAltStreamPath,

[Parameter(ParameterSetName=""PSGetFileMetadata"", Mandatory=$true)]
{0}

[string] $getMetaFilePath,

[Parameter(ParameterSetName=""PSGetPathItems"", Mandatory=$true)]
{0}

[string] $getPathItems,

[Parameter(ParameterSetName=""PSGetPathDirAndFiles"", Mandatory=$true)]
{0}

[string] $getPathDir
)

Expand Down Expand Up @@ -8973,7 +8968,12 @@ function PerformCopyFileFromRemoteSession
{{
if ($isAlternateStream)
{{
$content = Microsoft.PowerShell.Management\Get-Content $filePath -stream $streamName -Encoding Byte -Raw
$content = if ($PSVersionTable.PSEdition -eq 'Desktop') {{
Microsoft.PowerShell.Management\Get-Content $filePath -stream $streamName -Encoding Byte -Raw
}}
else {{
Microsoft.PowerShell.Management\Get-Content $filePath -stream $streamName -AsByteStream -Raw
}}
$rstream = [System.IO.MemoryStream]::new($content)
}}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@ Describe "Validate Copy-Item locally" -Tags "CI" {
}

# This is a Pester test suite to validate Copy-Item remotely using a remote session.

# If PS Remoting is not available, do not run the suite.
function ShouldRun
{
if ( $IsCoreCLR ) { return $false }
$result = Invoke-Command -ComputerName . -ScriptBlock {1} -ErrorAction SilentlyContinue
return ($result -eq 1)
}

if (-not (ShouldRun))
# We cannot create a PS Remoting session on non-Windows, so do not run the suite on those platforms.
if (-not $IsWindows)
{
Write-Host "PS Remoting is not available, skipping tests..." -ForegroundColor Cyan
Write-Host "The 'HelpersRemoting' module works on Windows only, skipping remote tests for Copy-Item ..." -ForegroundColor Cyan
return
}

Describe "Validate Copy-Item Remotely" -Tags "CI" {
Describe "Validate Copy-Item Remotely" -Tags @('CI', 'RequireAdminOnWindows') {

# Validate a copy item operation.
# $filePath is the source file path
Expand Down Expand Up @@ -83,7 +75,7 @@ Describe "Validate Copy-Item Remotely" -Tags "CI" {
}

BeforeAll {
$s = New-PSSession -ComputerName . -ErrorAction SilentlyContinue
$s = New-RemoteSession
if (-not $s)
{
throw "Failed to create PSSession for remote copy operations."
Expand Down Expand Up @@ -495,18 +487,9 @@ Describe "Validate Copy-Item Remotely" -Tags "CI" {
}

It "Copy-Item parameters -FromSession and -ToSession are mutually exclusive." {
try
{
$s1 = New-PSSession -ComputerName . -ErrorAction SilentlyContinue
$s1 | Should -Not -BeNullOrEmpty
$filePath = CreateTestFile
$destinationFolderPath = GetDestinationFolderPath
{ Copy-Item -Path $filePath -Destination $destinationFolderPath -FromSession $s -ToSession $s1 -ErrorAction Stop } | Should -Throw -ErrorId "InvalidInput,Microsoft.PowerShell.Commands.CopyItemCommand"
}
finally
{
Remove-PSSession -Session $s1 -ErrorAction SilentlyContinue
}
$filePath = CreateTestFile
$destinationFolderPath = GetDestinationFolderPath
{ Copy-Item -Path $filePath -Destination $destinationFolderPath -FromSession $s -ToSession $s -ErrorAction Stop } | Should -Throw -ErrorId "InvalidInput,Microsoft.PowerShell.Commands.CopyItemCommand"
}
}

Expand Down Expand Up @@ -570,7 +553,7 @@ Describe "Validate Copy-Item Remotely" -Tags "CI" {
}
else
{
It "Copy-Item ToSession -Destination '$path' throws $expectedFullyQualifiedErrorId" {
It "Copy-Item ToSession -Destination '$destination' throws $expectedFullyQualifiedErrorId" {
{ Copy-Item -Path $path -ToSession $s -Destination $destination -ErrorAction Stop } |
Should -Throw -ErrorId $expectedFullyQualifiedErrorId
}
Expand Down Expand Up @@ -654,9 +637,13 @@ Describe "Validate Copy-Item Remotely" -Tags "CI" {
}
}

Describe "Validate Copy-Item error for target sessions not in FullLanguageMode." -Tags "Feature" {
Describe "Validate Copy-Item error for target sessions not in FullLanguageMode." -Tags @('Feature', 'RequireAdminOnWindows') {

BeforeAll {
# Keep track of the sessions.
$testSessions = @{}
# Keep track of the session names to be unregistered.
$sessionToUnregister = @()

$testDirectory = "TestDrive:\"

Expand All @@ -670,12 +657,6 @@ Describe "Validate Copy-Item error for target sessions not in FullLanguageMode."
$testFilePath = Join-Path $source "testfile.txt"
"File test content" | Out-File $testFilePath -Force

# Keep track of the sessions.
$testSessions = @{}

# Keep track of the session names to be unregistered.
$sessionToUnregister = @()

$languageModes = @("ConstrainedLanguage", "NoLanguage", "RestrictedLanguage")
$id = (Get-Random).ToString()

Expand All @@ -688,8 +669,8 @@ Describe "Validate Copy-Item error for target sessions not in FullLanguageMode."
# Create the session.
Write-Host "Creating pssession with '$languageMode' ..."
New-PSSessionConfigurationFile -Path $configFilePath -SessionType Default -LanguageMode $languageMode
Register-PSSessionConfiguration -Name $sessionName -Path $configFilePath -Force | Out-Null
$testSession = New-PSSession -ConfigurationName $sessionName
Register-PSSessionConfiguration -Name $sessionName -Path $configFilePath -Force -ErrorAction Stop | Out-Null
$testSession = New-RemoteSession -ConfigurationName $sessionName

# Validate that the session is opened.
$testSession.State | Should -Be "Opened"
Expand All @@ -703,7 +684,6 @@ Describe "Validate Copy-Item error for target sessions not in FullLanguageMode."
}

AfterAll {

$testSessions.Values | Remove-PSSession -ErrorAction SilentlyContinue

$sessionToUnregister | ForEach-Object {
Expand All @@ -728,12 +708,12 @@ Describe "Validate Copy-Item error for target sessions not in FullLanguageMode."
}
}

Describe "Copy-Item can use Recurse and Exclude together" -Tags "Feature" {
Describe "Copy-Item can use Recurse and Exclude together" -Tags @('CI', 'RequireAdminOnWindows') {

Context "Local and Remote Tests" {

BeforeAll {
$s = New-PSSession -ComputerName . -ErrorAction SilentlyContinue
$s = New-RemoteSession
if (-not $s)
{
throw "Failed to create PSSession for remote copy operations."
Expand Down Expand Up @@ -773,10 +753,10 @@ Describe "Copy-Item can use Recurse and Exclude together" -Tags "Feature" {
}
}

Describe "Copy-Item remotely bug fixes" -Tags "Feature" {
Describe "Copy-Item remotely bug fixes" -Tags @('CI', 'RequireAdminOnWindows') {

BeforeAll {
$s = New-PSSession -ComputerName . -ErrorAction SilentlyContinue
$s = New-RemoteSession
if (-not $s)
{
throw "Failed to create PSSession for remote copy operations."
Expand Down
Loading