xml - 如何在本地机器上使用在远程机器上设置的变量
问题描述
我正在编写一个脚本,该脚本从存储在远程机器上的 XML 文档中获取远程机器上文件夹的位置。然后我想将文件夹复制到本地 PC。这是我目前拥有的代码:
Invoke-Command -Session $TargetSession -ScriptBlock {
if (Test-Path "$env:USERPROFILE\pathto\XML") {
[xml]$xml = Get-Content $env:USERPROFILE\pathto\XML
$XMLNode = $xml.node.containing.file.path.src
foreach ($Log in $XMLNode) {
$LogsP = Split-Path -Path $Log -Parent
$LogsL = Split-Path -Path $Log -Leaf
}
} else {
Write-Host "There is no XML file!"
}
Copy-Item -Path "$LogsP" -FromSession $TargetSession -Destination "$env:TEMP" -Force -Recurse -Container
$logsP
永远不会在脚本块之外填充Invoke-Command
。我尝试过使用return
,我尝试将其设置为全局变量,我尝试使用Copy-Item
脚本块中的命令(无论我使用 Winrm/PSRemoting 进行什么更改,它都会一直给我一个拒绝访问错误)。有谁知道我如何$logsP
在脚本块之外填充?
解决方案
不要在脚本块内的变量中收集父路径。只需让它回显到 Success 输出流,并Invoke-Command
在本地计算机上的变量中收集输出。
$LogsP = Invoke-Command -Session $TargetSession -ScriptBlock {
if (Test-Path "$env:USERPROFILE\pathto\XML") {
[xml]$xml = Get-Content $env:USERPROFILE\pathto\XML
foreach ($Log in $xml.node.containing.file.path.src) {
Split-Path -Path $Log -Parent
}
}
}
$LogsP | Copy-Item -FromSession $TargetSession -Destination "$env:TEMP" -Force -Recurse -Container
推荐阅读
- pycharm - 使用“查找用法”命令查找外部用法?
- laravel - laravel 通过查询订购模型
- python - 使用 Python 在 Amazon Lamda 上调用 Google Sheets API 时出错
- conventions - 静态类的首字母应该大写吗?
- javascript - node.js 中的 Ajax 调用
- database - 基于新表创建创建和命名 sql 表的 Sqlalchemy 最佳实践
- git - 推送到我的分支时收到致命错误
- c++ - 不理解 C++ 中存在流错误的程序流程
- php - 如何在 Laravel 上解决数据 Excel 到 MySQL 的更改值
- javascript - 从表或列表中抓取数据?