[英]Powershell issue when executing .ps1 script
So, warning, this is probably a really newbie questions, so apologies in advance. 因此,警告一下,这可能是一个真正的新手问题,因此提前致歉。
I'm starting to learn Powershell and one of the first things I want to do, is just make a directory & copy a file to it. 我开始学习Powershell,而我想做的第一件事就是仅建立目录并将文件复制到其中。
Now, if I use the following commands in a CMD window, they work perfectly. 现在,如果我在CMD窗口中使用以下命令,它们将完美运行。
mkdir %HOMEPATH%\test
cp test.txt %HOMEPATH%\test
However, when I put them into a .ps1 file and execute it, I get an error saying the directory could not be found etc (see below) 但是,当我将它们放入.ps1文件并执行时,出现错误,提示找不到目录等(请参见下文)
Copy-Item : Could not find a part of the path 'C:\\Chef\\windowsdevbox-master\\%HOMEPATH%\\.berkshelf
' Copy-Item : Could not find a part of the path 'C:\\Chef\\windowsdevbox-master\\%HOMEPATH%\\.berkshelf
'的一部分
Now, I got told that this is because I need to put CMD before each command. 现在,我被告知这是因为我需要在每个命令之前放置CMD。 I ran this, with the CMD in front of each one and the error disappeared and instead I was presented with the "Home text" for CMD and the script finished.
我运行了此命令,每个命令都位于CMD前面,错误消失了,取而代之的是,我看到了CMD的“主页文本”,并且脚本完成了。
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
However, the folder was not created and the file was not copied over. 但是,没有创建文件夹,也没有复制文件。
I just wondered what I need to do to get this to work. 我只是想知道要使它正常工作需要做什么。
In PowerShell mkdir
is a built in function designed to emulate the same functionality, but using the built in cmdlet New-Item
to do the underlying work. 在PowerShell中,
mkdir
是一个内置函数,旨在模拟相同的功能,但是使用内置cmdlet New-Item
进行基础工作。
cp
is a straight up alias to PowerShell's Copy-Item
cmdlet. cp
是PowerShell的Copy-Item
cmdlet的直接别名。
You don't need to precede these with cmd
to make them work. 您无需在这些
cmd
之前加上cmd
即可使它们起作用。
PowerShell does not accept the %VAR%
syntax for environment variables though. 但是,PowerShell不接受环境变量的
%VAR%
语法。 Instead you would use the special $env
variable, followed by a colon :
followed by the variable name: $env:HOME
. 取而代之的是,您将使用特殊的
$env
变量,然后使用冒号:
然后使用变量名称: $env:HOME
。
mkdir $env:HOMEPATH\test
cp test.txt $env:HOMEPATH\test
%HOMEPATH%
is not a PowerShell variable. %HOMEPATH%
不是PowerShell变量。
Environment variables are stored in the $env
variable scope. 环境变量存储在
$env
变量作用域中。 You can access it with $env:homepath
. 您可以使用
$env:homepath
访问它。
Here, I would use: 在这里,我将使用:
mkdir "${env:homepath}\test";
cp test.txt "${env:homepath}\test";
I might be inclined to use mkdir "${env:homedrive}${env:homepath}\\test";
我可能倾向于使用
mkdir "${env:homedrive}${env:homepath}\\test";
, but I don't really know what you're trying to accomplish. ,但我真的不知道您要完成什么。
The curly braces here tell PowerShell that the entire contents are the variable name. 这里的花括号告诉PowerShell整个内容都是变量名。 The colon tends to confuse it, IMX, especially when you embed variables in strings.
冒号容易使它混淆,IMX,尤其是在将变量嵌入字符串中时。
Environment variables are special in PowerShell. 环境变量在PowerShell中是特殊的。 They have their own provider.
他们有自己的提供者。 You can list them with
Get-ChildItem env:
, and manipulate them at the env:
PSDrive. 您可以使用
Get-ChildItem env:
列出它们,并在env:
PSDrive上对其进行操作。
Additional Note: Certain configurations might have unpredictable results because the strings might have too many backslashes or have backslashes in the wrong place. 附加说明:某些配置可能会产生不可预测的结果,因为字符串可能包含过多的反斜杠或在错误的位置放置了反斜杠。 In this case, you might need to use
Join-Path
to combine the paths correctly. 在这种情况下,您可能需要使用
Join-Path
正确组合路径。
Say: 说:
$env:homedrive
is 'U:'
$env:homedrive
为'U:'
$env:homepath
is '\\'
$env:homepath
是'\\'
And the subfolder is '\\test'
子文件夹是
'\\test'
Then "${env:homepath}\\test'
is \\\\test
, which looks like a UNC path. Instead you can use Join-Path ${env:homepath} '\\test'
, which will then correctly create '\\test'
. 然后,
"${env:homepath}\\test'
是\\\\test
,看起来像UNC路径。相反,您可以使用Join-Path ${env:homepath} '\\test'
,然后将正确创建'\\test'
。
If you want to join three things, it's a bit complex: 如果您想加入三件事,那就有点复杂了:
Join-Path ${env:homedrive} (Join-Path ${env:homepath} '\test')
But that correctly creates 'U:\\test'
instead of 'U:\\\\test'
. 但这正确地创建了
'U:\\test'
而不是'U:\\\\test'
。
You can also use the -Resolve
option to convert a relative path to the absolute one. 您还可以使用
-Resolve
选项将相对路径转换为绝对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.