简体   繁体   English

PowerShell:目录检索和语法错误

[英]PowerShell: Directory Retrieval and syntax error

A little background... 一点背景...
I use Windows XP, Vista, and 7 quite frequently. 我经常使用Windows XP,Vista和7。 As such, I constantly have to move my program settings from the %appdata% folder on each PC to the next. 因此,我必须不断将程序设置从每台PC上的%appdata%文件夹移至下一台。 I figured that making a PowerShell script to do this for me and remove the folders after I finish would be something to ease my troubles. 我认为制作PowerShell脚本来为我执行此操作并在完成后删除文件夹将减轻我的麻烦。 As I generally have my work on a flash drive, I was hoping to use relative paths, but it seems to be causing me a bit of trouble, but the biggest problem is that I don't seem to understand Powershell enough to know what mistake I'm making and how to fix it... So I came here. 因为我通常在闪存驱动器上工作,所以我希望使用相对路径,但这似乎给我带来了麻烦,但是最大的问题是我似乎对Powershell的了解不足,无法知道什么错误我正在制作以及如何修复它...所以我来了。

I figured that I could separate the task into two scripts; 我认为我可以将任务分成两个脚本。 one for placing the directories and the second for copying them back to the original folder and removing any trace of them behind. 一个用于放置目录,第二个用于将目录复制回原始文件夹并删除后面的任何痕迹。 I'll show you want I have so far. 我会告诉你你想要我到目前为止。 I figured retrieving them might be more difficult so I started there. 我认为检索它们可能会比较困难,所以我从那里开始。 Here's what I have so far. 到目前为止,这就是我所拥有的。 I'm using a txt file to make it easy to update the list of folders I want or need transferred so it's also being targeted by a variable. 我使用的是txt文件,可以轻松地更新我想要或需要转移的文件夹列表,因此它也被变量作为目标。

$fldrtxt = Get-Content .\FolderList.txt
$dirget = -LiteralPath ="'%appdata%'\$_fldertxt"
$dirpost = "./Current"
# get-command | Add-Content .\"$today"_CommandList.txt
Set-Location c: {get-content $_dirget} | %{ copy-item $_dirpost}

I can't get PowerShell to recognize the same command that I use when I use the run utility. 我无法让PowerShell识别使用run实用程序时使用的同一命令。 Since I'm sure I can use %appdata% to reference where I want the folders taken from and to, how can't I write this script to do what I want? 既然可以确定我可以使用%appdata%来引用我要从中取出文件夹的位置,那么如何编写此脚本才能完成我想要的工作? I can use an absolute path, because I'd have to use a separate script for all three computers. 我可以使用绝对路径,因为我必须为所有三台计算机使用单独的脚本。 And that I don't want. 而且我不想。

How can I use PowerShell to do what I want and target the folders I need to use? 如何使用PowerShell执行我想要的操作并定位需要使用的文件夹?

First: Accerss the Environment 第一:破坏环境

Since I'm sure I can use %appdata% to reference where I want the folders take from and too 因为我确定我可以使用%appdata%来引用我希望文件夹从何处获取

Wrong syntax for PowerShell, the %var% syntax for environment variables is specific to cmd scripts (and carried forward from MS-DOS batch files). PowerShell的语法错误,环境变量的%var%语法特定于cmd脚本(并从MS-DOS批处理文件继承而来)。

In PowerShell to access environment variables prefix their name with env: , so $env:AppData . 在PowerShell中,访问环境变量的名称以env:前缀,因此$env:AppData

$_dirget = "$env:AppData\$_fldertxt"

Second: Passing parameters 第二:传递参数

Don't include the parameter name in the variable, a variable passed to a cmdlet will be passed as an argument not a parameter name. 不要在变量中包含参数名称,传递给cmdlet的变量将作为参数而不是参数名称传递。 You need: 你需要:

get-content -LiteralPath $_dirget

(There is something call "splat" that allows you to use a hash tables of parameter name-argument pairs as a hashtable, but that's unnecessary here.) (有一个叫“ splat”的东西,它允许您将参数名称-参数对的哈希表用作哈希表,但这在这里是不必要的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM