powershell - ForEach-Object 脚本块中的命令意外提示输入参数
问题描述
我有以下脚本
$sourceRoot = "C:\Users\skywalker\Desktop\deathStar\server"
$destinationRoot = "C:\Users\skywalker\Desktop\deathStar/server-sandbox"
$dir = get-childitem $sourceRoot -Exclude .env, web.config
Write-Output "Copying Folders"
$i=1
$dir| %{
[int]$percent = $i / $dir.count * 100
Write-Progress -Activity "Copying ... ($percent %)" -status $_ -PercentComplete $percent -verbose
copy -Destination $destinationRoot -Recurse -Force
$i++
我试图参考这篇文章,但最终在 powershell 控制台中得到了以下提示。
为以下参数提供值:
路径[0]:
解决方案
您正在使用%
( ForEach-Object
)逐个对象处理来自管道 ( $dir
) 对象的输入。
因此,您的copy
( Copy-Item
) 命令:
copy -Destination $destinationRoot -Recurse -Force
缺少源参数,必须更改为:
$_ | copy -Destination $destinationRoot -Recurse -Force
如果没有源参数(传递给-Path
or -LiteralPath
) - 这是强制性的 -Copy-Item
会提示输入它,这是您所经历的(默认参数是-Path
)。
在上面的固定命令中,$_
通过管道传递隐式绑定到Copy-Item
'-LiteralPath
参数。
推荐阅读
- arrays - 在 google 表格中创建表格以用于表格公式
- javascript - 如何在 Electron 中将文件名转换为 URL?
- python - 乘以熊猫数据框
- c++ - 无法打开源文件#include
在发布模式下 - scala.js - 如何访问 JQuery originalEvent 属性
- c - 浮点数及其对 8 位微控制器内存的影响
- api - Symfony api平台安装问题
- xml - 如何加密 XML 中的元素并使用 XSLT1 解密它们?
- javascript - 如何将表单上传的文件保存在javascript中特定文件夹的表单中?
- android - Gradle 自定义 JavaCompile 任务进行注解处理