简体   繁体   English

执行.ps1脚本时的Powershell问题

[英]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.

相关问题 使用PuTTY Plink执行.ps1文件时出现意外的char - Unexpected char when executing .ps1 file using PuTTY Plink 从.bat文件执行.ps1文件 - Executing a .ps1 file from a .bat file 如何在没有外部 ps1 文件的情况下创建一个执行 powershell 脚本的 bat 文件? - How do I create a bat file that executes powershell script without an external ps1 file? 如何从 CMD 运行 powershell 脚本,以便 *.PS1 将 HEX 值正确转换为十进制 - How to run powershell script from CMD, so that *.PS1 would properly convert HEX values to decimal powershell拒绝加载任何本地ps1脚本的目的是什么,当这是微不足道的规避? - What is the purpose of powershell refusing to load any local ps1 scripts, when this is trivial to circumvent? 使用VBS脚本中的参数调用ps1脚本 - Call a ps1 script with parameters from a VBS script 在Windows操作系统中调用* .ps1的Python脚本 - Python script to Invoke *.ps1 in windows operating system 将带有单引号的参数传递给.PS1脚本 - Passing argument with one single quote in it to a .PS1 script Powershell-无法运行.ps1,但可以通过控制台本身运行 - Powershell - Running .ps1 doesn't work, but running it through the console itself does 通过使用批处理文件调用.ps1文件在Powershell中使用Invoke -WebRequest下载文件 - Download a file using Invoke -WebRequest in Powershell by calling a .ps1 file using a batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM