param( [Parameter(Position=0)] [string[]] $Name ) foreach ($item in $Name) { microsoft.powershell.utility\remove-variable -name $item -scope global } Set-StrictMode -Off }function Get-PSCallStack { [CmdletBinding()] param() if ($PSWorkflowDebugger -ne $null) { foreach ($frame in $PSWorkflowDebugger.GetCallStack()) { Write-Output $frame } } Set-StrictMode -Off }runspaceInfoSet-PSDebugModeMClientRunspacePoolDataStructureHandlercomputerName#PSNonVirtualCall_#pipelineProcessor!commandProcessor#PipelineProcessorreadFromCommandZone.Identifier[ZoneTransfer]ZoneId={0}{ param ( [Parameter(ParameterSetName="PSCopyFileToRemoteSession")] [Parameter(ParameterSetName="PSCopyAlternateStreamToRemoteSession")] {0} [string] $copyToFilePath, [Parameter(ParameterSetName="PSCopyFileToRemoteSession", Mandatory=$false)] [Parameter(ParameterSetName="PSCopyAlternateStreamToRemoteSession")] [ValidateNotNullOrEmpty()] [string] $b64Fragment, [Parameter(ParameterSetName="PSCopyFileToRemoteSession")] [switch] $createFile = $false, [Parameter(ParameterSetName="PSCopyFileToRemoteSession")] [switch] $emptyFile = $false, [Parameter(ParameterSetName="PSCopyAlternateStreamToRemoteSession", Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $streamName, [Parameter(ParameterSetName="PSTargetSupportsAlternateStreams", Mandatory=$true)] {0} [string] $supportAltStreamPath, [Parameter(ParameterSetName="PSSetFileMetadata", Mandatory=$true)] {0} [string] $metaDataFilePath, [Parameter(ParameterSetName="PSSetFileMetadata", Mandatory=$true)] [ValidateNotNull()] [hashtable] $metaDataToSet, [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")] [switch] $force ) # Checks if path drive specifies max size and if max size is exceeded # function CheckPSDriveSize {{ param ( [System.Management.Automation.PathInfo] $resolvedPath, [int] $fragmentLength ) if (($resolvedPath.Drive -ne $null) -and ($resolvedPath.Drive.MaximumSize -ne $null)) {{ $maxUserSize = $resolvedPath.Drive.MaximumSize $dirSize = 0 Microsoft.PowerShell.Management\Get-ChildItem -LiteralPath ($resolvedPath.Drive.Name + ":") -Recurse | Foreach {{ Microsoft.PowerShell.Management\Get-Item -LiteralPath $_.FullName -Stream * | % {{ $dirSize += $_.Length }} }} if (($dirSize + $fragmentLength) -gt $maxUserSize) {{ $msg = "{1}" -f $maxUserSize throw $msg }} }} }} # Return a hashtable with the following members: # BytesWritten - the number of bytes written to a file # function PSCopyFileToRemoteSession {{ param( [string] $copyToFilePath, [string] $b64Fragment, [switch] $createFile = $false, [switch] $emptyFile = $false ) $op = @{{ BytesWritten = $null }} $wstream = $null try {{ $filePathExists = Microsoft.PowerShell.Management\Test-Path -Path $copyToFilePath if ($createFile -or (! $filePathExists)) {{ # If the file already exists, try to delete it. if ($filePathExists) {{ Microsoft.PowerShell.Management\Remove-Item -Path $copyToFilePath -Force -ea SilentlyContinue }} # Create the new file. $fileInfo = Microsoft.PowerShell.Management\New-Item -Path $copyToFilePath -Type File -Force if ($emptyFile) {{ # Handle the empty file scenario. $op['BytesWritten'] = 0 return $op }} }} # Resolve path in case it is a PSDrive $resolvedPath = Microsoft.PowerShell.Management\Resolve-Path -literal $copyToFilePath # Decode $fragment = [System.Convert]::FromBase64String($b64Fragment) # Check if drive specifies max size and if max size is exceeded CheckPSDriveSize $resolvedPath $fragment.Length # Write fragment $wstream = Microsoft.PowerShell.Utility\New-Object -TypeName IO.FileStream -ArgumentList ($resolvedPath.ProviderPath), ([System.IO.FileMode]::Append) $wstream.Write($fragment, 0, $fragment.Length) $op['BytesWritten'] = $fragment.Length }} catch {{ if ($_.Exception.InnerException) {{ Microsoft.PowerShell.Utility\Write-Error -Exception $_.Exception.InnerException }} else {{ Microsoft.PowerShell.Utility\Write-Error -Exception $_.Exception }} }} finally {{ if ($wstream -ne $null) {{ $wstream.Dispose() }} }} return $op }} # Retuns a hashtable with the following members: # BytesWritten - number of bytes written to an alternate file stream # function PSCopyFileAlternateStreamToRemoteSession {{ param ( [string] $copyToFilePath, [string] $b64Fragment, [string] $streamName ) $op = @{{ BytesWritten = $null }} try {{ # Resolve path in case it is a PSDrive $resolvedPath = Microsoft.PowerShell.Management\Resolve-Path -literal $copyToFilePath # Decode $fragment = [System.Convert]::FromBase64String($b64Fragment) # Check if drive specifies max size and if max size is exceeded CheckPSDriveSize $resolvedPath $fragment.Length # Write the stream Microsoft.PowerShell.Management\Add-Content -Path ($resolvedPath.ProviderPath) -Value $fragment -Encoding Byte -Stream $streamName -ErrorAction Stop $op['BytesWritten'] = $fragment.Length }} catch {{ Microsoft.PowerShell.Utility\Write-Error -Exception $_.Exception }} return $op }} # Returns a hashtable with the following member: # TargetSupportsAlternateStreams - boolean to keep track of whether the target supports Alternate data streams. # function PSTargetSupportsAlternateStreams {{ param ( [string] $supportAltStreamPath ) $result = @{{ TargetSupportsAlternateStreams = $false }} # Resolve path in case it is a PSDrive $resolvedPath = Microsoft.PowerShell.Management\Resolve-Path -literal $supportAltStreamPath $targetDrive = [IO.Path]::GetPathRoot($resolvedPath.ProviderPath) if (-not $targetDrive) {{ return $result }} # Check if the target drive is NTFS $driveFormat = 'NTFS' foreach ($drive in [System.IO.DriveInfo]::GetDrives()) {{ if (($drive.Name -eq $targetDrive) -and ($drive.DriveFormat -eq $driveFormat)) {{ # Now, check if the target supports Add-Command -Stream. This functionality was introduced in version 3.0. $addContentCmdlet = Microsoft.PowerShell.Core\Get-Command Microsoft.PowerShell.Management\Add-Content -ErrorAction SilentlyContinue if ($addContentCmdlet.Parameters.Keys -contains 'Stream') {{ $result['TargetSupportsAlternateStreams'] = $true break }} }} }} return $result }} # Sets the metadata for the given file. # function PSSetFileMetadata {{ param ( [string] $metaDataFilePath, [hashtable] $metaDataToSet ) $item = Microsoft.PowerShell.Management\get-item $metaDataFilePath -ea SilentlyContinue -Force if ($item) {{ # LastWriteTime if ($metaDataToSet['LastWriteTimeUtc']) {{ $item.LastWriteTimeUtc = $metaDataToSet['LastWriteTimeUtc'] }} if ($metaDataToSet['LastWriteTime']) {{ $item.LastWriteTime = $metaDataToSet['LastWriteTime'] }} # Attributes if ($metaDataToSet['Attributes']) {{ $item.Attributes = $metaDataToSet['Attributes'] }} }} }} # Returns a hashtable with the following member: # IsFileInfo - boolean to keep track of whether the given path is a remote file. # IsDirectoryInfo - boolean to keep track of whether the given path is a remote directory. # ParentIsDirectoryInfo - boolean to keep track of whether the given parent path is a remote directory. # function PSGetRemotePathInfo {{ param ( [string] $remotePath ) try {{ try {{ $parentPath = Microsoft.PowerShell.Management\Split-Path $remotePath }} # catch everything and ignore the error. catch {{}} $result = @{{ IsFileInfo = (Microsoft.PowerShell.Management\Test-Path $remotePath -PathType Leaf) IsDirectoryInfo = (Microsoft.PowerShell.Management\Test-Path $remotePath -PathType Container) }} if ($parentPath) {{ $result['ParentIsDirectoryInfo'] = (Microsoft.PowerShell.Management\Test-Path $parentPath -PathType Container) }} }} catch {{ if ($_.Exception.InnerException) {{ Microsoft.PowerShell.Utility\Write-Error -Exception $_.Exception.InnerException }} else {{ Microsoft.PowerShel