H
H
Hecntv2021-12-23 10:13:58
PowerShell
Hecntv, 2021-12-23 10:13:58

Using powershell how to upload file to server?

The problem is that I can’t find how to properly upload a JSON file to a remote server, according to the ConverTo-Json and ConverFrom-Json documentation, as I understood write and read for Json file in comandRow (on the command line). Can someone tell me the correct or simple method how to implement a simple upload of a json file to a remote server, thanks in advance!
Here are attempts, perhaps in the wrong direction, I write the code:

The first option here I save the data to a file in a local directory

# в переменной outFileJson лежат созданные объекты классов в списке
$outFileJson = Get-Content "D:\project\powerShell\json_file.json" | Out-String | ConvertFrom-Json
$outFileJson

Here is an example of how I tried to do it, but it displays an error (I haven’t figured out the error yet, I didn’t climb it because it’s already confused, it will be too necessary to open the file)

$fileJsonInLocal = "D:\project\powerShell\json_file.json"
$ftpUri = "ftp:(Тут два слэша)user:[email protected](Тут один слэш)json_file.json" # слэши текстом что бы не был link
$webClient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftpUri)
$webClient.UploadFile($uri, $fileJsonInLocal)
Ошибка:
Исключение при вызове "UploadFile" с "2" аргументами: "Удаленный сервер возвратил ошибку: (550) Файл недоступен (наприм
ер, не найден или к нему нет доступа)."
D:\project\powerShell\task_one.ps1:136 знак:1
+ $webClient.UploadFile($uri, $fileJsonInLocal)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxKozlov, 2021-12-23
@MaxKozlov

In your first example, you simply create a PSObject object from a JSON file, but do not load it anywhere.
The second one most likely does not work because it does not have write access to the specified path "(There is one slash)json_file.json"

R
Roman Bezrukov, 2021-12-24
@NortheR73

In general, there is such a Posh-SSH module ... Facilitates the implementation of exercises with SSH, SCP, (S)FTP ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question