变量可以包含下划线。下面的作品和显示字符串“ASDF”
$a_ = "adsf"; $a_
你New-Item
cmdlet的调用会失败,因为$source_
是不是一个变量,会返回null。这是PowerShell的默认行为。当我运行你的代码时,我得到以下内容:
New-Item : Cannot find drive. A drive with the name '02/18/2017 22' does not exist.At line:1 char:1
+ New-Item -ItemType Directory -Force -Path "$source_$(Get-Date)" -what ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (02/18/2017 22:String) [New-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.NewItemCommand
所以我会期望你的文件夹变量为null。 wOxxOm brings this up in comment as well
几种途径来解决什么我肯定是你的问题的部分源。
$BackupFolder = New-Item -ItemType Directory -Force -Path "$source`_$(Get-Date)"
$BackupFolder = New-Item -ItemType Directory -Force -Path "$($source)_$(Get-Date)"
$BackupFolder = New-Item -ItemType Directory -Force -Path ("{0}_{1} -f "$source, Get-Date)
你还是要尽量从副本排除该文件夹,以及像Keith Hill's answer is telling you
Copy-Item $Source\* $BackupFolder -Exclude $BackupFolder
我假设你已经试过管理权 – William
'$ source_'是一个有效的标识符名称。使用'$($ source)_'或''''source'_''' – wOxxOm
William - 我没有得到您的问题,如果您询问有管理员权限的运行Powershell,那么是运行脚本的帐户有admin previliges – ATLANTAFALCONS