Powershell copy-item 问题
Powershell copy-item issue
我有一个非常奇怪的问题,一直在拉扯我的头发。
我有两个 PowerShell 脚本。
让我们说 Main 和 child。 Child 脚本位于主文件夹中的一个文件夹中:
Mainfolder\Main_script
MainFolder\ChildFolder\child_script.ps1
主脚本从位置可以更改的地方运行。所以我一开始就得到了脚本的位置
$ScriptDir = (Get-Location).path
我做一些任务,包括复制哪些有效
copy-item -path $src -Destination $dst -force
到目前为止一切都很好。
然后我调用我的 child 脚本
& "$scriptdir\childfolder\Child_script.ps1"
脚本运行,我看到其他任务正在执行。但是我在 child 文件夹中有一些文件需要复制过来。但是副本不适用于 child_Script。
我什至把
"Copying $src to $destination" | Out-file -append $logfile
我看到了 Copying C:\test\copythis.txt to C:\temp
child脚本中的代码是这样的
$Scriptdir = (Get-Location).path
$src = "$Scriptdir\copythis.txt"
$dst = "C:\temp"
copy-item -path $src -Destination $dst -force
如果我尝试在主脚本中复制相同的文件,works.Why 所做的一切 copy-item 在 child 脚本中都不起作用?
我还应该提到,一切都是 运行 系统帐户。所以没有权限问题。
如有任何帮助,我们将不胜感激。
干杯,
正在阅读documentation:
Get-Location
Gets information about the current working location or a location stack.
.....
The Get-Location cmdlet gets an object that represents the current directory, much like the pwd (print working directory) command.
如果您想使用脚本所在的目录,请使用 this Whosebug question 的答案。
我有一个非常奇怪的问题,一直在拉扯我的头发。
我有两个 PowerShell 脚本。 让我们说 Main 和 child。 Child 脚本位于主文件夹中的一个文件夹中:
Mainfolder\Main_script
MainFolder\ChildFolder\child_script.ps1
主脚本从位置可以更改的地方运行。所以我一开始就得到了脚本的位置
$ScriptDir = (Get-Location).path
我做一些任务,包括复制哪些有效
copy-item -path $src -Destination $dst -force
到目前为止一切都很好。 然后我调用我的 child 脚本
& "$scriptdir\childfolder\Child_script.ps1"
脚本运行,我看到其他任务正在执行。但是我在 child 文件夹中有一些文件需要复制过来。但是副本不适用于 child_Script。 我什至把
"Copying $src to $destination" | Out-file -append $logfile
我看到了 Copying C:\test\copythis.txt to C:\temp
child脚本中的代码是这样的
$Scriptdir = (Get-Location).path
$src = "$Scriptdir\copythis.txt"
$dst = "C:\temp"
copy-item -path $src -Destination $dst -force
如果我尝试在主脚本中复制相同的文件,works.Why 所做的一切 copy-item 在 child 脚本中都不起作用? 我还应该提到,一切都是 运行 系统帐户。所以没有权限问题。
如有任何帮助,我们将不胜感激。 干杯,
正在阅读documentation:
Get-Location
Gets information about the current working location or a location stack.
.....
The Get-Location cmdlet gets an object that represents the current directory, much like the pwd (print working directory) command.
如果您想使用脚本所在的目录,请使用 this Whosebug question 的答案。