Differences
This shows you the differences between the selected revisions of the page.
library_powershell 2021-08-26 | library_powershell 2023-10-30 (current) | ||
Line 17: | Line 17: | ||
powershell.exe -File upload.ps1 | powershell.exe -File upload.ps1 | ||
</code> | </code> | ||
+ | |||
+ | PowerShell (Core)'s ''pwsh.exe'' installs into ''C:\Program Files\PowerShell\<version>''. | ||
Note that by default, executing PowerShell scripts is disabled. To override that, you can either lift the restriction by typing using ''[[ps>microsoft.powershell.security/set-executionpolicy|Set-ExecutionPolicy]]'' cmdlet on PowerShell administrator console:((Run ''powershell.exe'' as Administrator to get PowerShell console.)) | Note that by default, executing PowerShell scripts is disabled. To override that, you can either lift the restriction by typing using ''[[ps>microsoft.powershell.security/set-executionpolicy|Set-ExecutionPolicy]]'' cmdlet on PowerShell administrator console:((Run ''powershell.exe'' as Administrator to get PowerShell console.)) | ||
Line 29: | Line 31: | ||
powershell.exe -ExecutionPolicy Unrestricted -File upload.ps1 | powershell.exe -ExecutionPolicy Unrestricted -File upload.ps1 | ||
</code> | </code> | ||
- | |||
- | PowerShell (Core)'s ''pwsh.exe'' installs into ''C:\Program Files\PowerShell\<version>''. | ||
===== [[install]] Installing the Assembly ===== | ===== [[install]] Installing the Assembly ===== | ||
- | First, you need to install the WinSCP .NET assembly. In most cases, all you need to do is [[library_install#downloading|download]] the ''WinSCP-X.X.X-Automation.zip'' package and extract it along with your PowerShell script.·Generally you only need ''WinSCPnet.dll'' and ''WinSCP.exe''. | + | First, you need to install the WinSCP .NET assembly. In most cases, all you need to do is [[library_install#downloading|download]] the ''WinSCP-X.X.X-Automation.zip'' package((In some cases, the downloaded [[message_net_operation_not_supported|executables need to be unblocked]].)) and extract it along with your PowerShell script.((Generally you only need ''WinSCPnet.dll'' and ''WinSCP.exe''.)) |
- | + | ||
- | Since the files are coming from an external source, you also have to **unblock** them (just once, unless you reinstall). You can do this manually, using the File Explorer to modify the file properties (on the General tab). Or you can do this from PowerShell, with the ''[[ps>microsoft.powershell.utility/unblock-file|Unblock-File]]'' cmdlet. But after unblocking, you must restart the PowerShell console. | + | |
- | The version of ''WinSCPnet.dll'' in the root of the Zip package is the .NET Framework build of the assembly. It can be used with Windows PowerShell only. With PowerShell (Core) 6/7, you have to use the .NET Standard build of the assembly, which is located in the ''netstandard2.0'' subfolder. | + | The version of ''WinSCPnet.dll'' in the root of the package is the .NET Framework build of the assembly. It can be used with Windows PowerShell only. With PowerShell (Core) 6/7, you have to use the .NET Standard build of the assembly, which is located in the ''netstandard2.0'' subfolder. |
For specific cases, read full instructions to [[library_install|installing the WinSCP .NET assembly]]. | For specific cases, read full instructions to [[library_install|installing the WinSCP .NET assembly]]. | ||
Line 103: | Line 101: | ||
==== [[module]] PowerShell Module ==== | ==== [[module]] PowerShell Module ==== | ||
- | There is a third-party PowerShell module, [[https://dotps1.github.io/WinSCP/|WinSCP PowerShell Wrapper]], that provides a cmdlet interface on top of the .NET assembly. | + | There is a third-party PowerShell module, [[https://github.com/tomohulk/WinSCP|WinSCP PowerShell Wrapper]], that provides a cmdlet interface on top of the .NET assembly. |
Example: | Example: | ||
Line 111: | Line 109: | ||
$credential = Get-Credential | $credential = Get-Credential | ||
# Create a WinSCP Session. | # Create a WinSCP Session. | ||
- | $session = New-WinSCPSession -Hostname "example.com" -Credential $credential -SshHostKeyFingerprint "ssh-rsa 2048 xxxxxxxxxxx...=" | + | $session = New-WinSCPSession -Hostname "example.com" -Credential $credential -SshHostKeyFingerprint "ssh-rsa 2048 xxxxxxxxxxx..." |
# Using the WinSCPSession, download the file from the remote host to the local host. | # Using the WinSCPSession, download the file from the remote host to the local host. | ||
Receive-WinSCPItem -WinSCPSession $session -Path "/home/user/file.txt" -Destination "C:\download\" | Receive-WinSCPItem -WinSCPSession $session -Path "/home/user/file.txt" -Destination "C:\download\" | ||
Line 122: | Line 120: | ||
<code powershell> | <code powershell> | ||
# Piping the WinSCPSession into the Receive-WinSCPItem auto disposes the WinSCP.Session object after completion. | # Piping the WinSCPSession into the Receive-WinSCPItem auto disposes the WinSCP.Session object after completion. | ||
- | New-WinSCPSession -Hostname "example.com" -Credential (Get-Credential) -SshHostKeyFingerprint "ssh-rsa 2048 xxxxxxxxxxx...=") | | + | New-WinSCPSession -Hostname "example.com" -Credential (Get-Credential) -SshHostKeyFingerprint "ssh-rsa 2048 xxxxxxxxxxx...") | |
Receive-WinSCPItem -Path "/home/user/file.txt" -Destination "C:\download\" | Receive-WinSCPItem -Path "/home/user/file.txt" -Destination "C:\download\" | ||
</code> | </code> | ||
Line 143: | Line 141: | ||
UserName = "user" | UserName = "user" | ||
Password = "mypassword" | Password = "mypassword" | ||
- | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...=" | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
} | } | ||