【发布时间】:2019-12-30 07:01:06
【问题描述】:
我必须从一个远程桌面获取特定文件到本地计算机或另一台服务器。如何在 get-content 中传递一个变量以从远程桌面连接获取文件?
我将文件路径存储为变量并尝试在 get-content 中传递它。
Invoke-Command -Computername $Server -ScriptBlock{get-content -path $file }
Invoke-Command -Computername $Server -ScriptBlock{get-content -path ${file} }
$file="C:\Users\Documents\new DB_connection\\log2.txt"
$Server="servername"
$answer= Invoke-Command -Computername $Server -ScriptBlock{get-content -path $file }
write-output $answer
无法将参数绑定到参数“路径”,因为它为空。 + CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
【问题讨论】:
-
我试过这段代码 "Invoke-Command -Computername $Server -ScriptBlock{get-content -path $file } 但它再次显示同样的错误
-
一旦你给$file赋值,然后在脚本块内简单地使用$using:file。在定义之前,您不能将其传递到脚本块中
标签: powershell