PowerShell脚本单独运行但不能一起运行,无法找出原因

时间:2014-11-11 11:20:17

标签: powershell scripting acl

我有几个PS脚本,我试图将它们组合成一个,但它不起作用。我有一个脚本使用robocopy复制文件夹结构,然后将一些其他文件复制到新创建的文件夹结构中。第二个脚本将acls从源复制到目标。

如果我运行robocopy脚本然后单独复制acl脚本它可以正常工作,但是当我将它们组合时,acls不会被复制...

Robocopy脚本:

## Asks User which Purpose they would like to clone
$ReadHostSourceFolder = "c:\sp6" #Read-Host "Enter the full path of the Source Folder without the final \"

## Asks User where they would like the clone to be copied to 
$ReadHostTargetFolder = "c:\SP7" #Read-Host "Enter the full path of the desired Destination without the final \"

## Sets arrays for additional files to be copied
$RPackSFolder = $ReadHostSourceFolder + "\Aggregation Kernel\End User Apps\Run Packagers"
$RPackTFolder = $ReadHostTargetFolder + "\Aggregation Kernel\End User Apps\Run Packagers"
$RViewerSFolder = $ReadHostSourceFolder + "\Aggregation Kernel\End User Apps\Results Viewers\"
$RViewerTFolder = $ReadHostTargetFolder + "\Aggregation Kernel\End User Apps\Results Viewers\"
$DPackSFolder = $ReadHostSourceFolder + "\Aggregation Kernel\End User Apps\Data Packagers\"
$DPackTFolder = $ReadHostTargetFolder + "\Aggregation Kernel\End User Apps\Data Packagers\"
$LHSSFolder = $ReadHostSourceFolder + "\Aggregation Kernel\End User Apps\LHS ETL Tool\"
$LHSTFolder = $ReadHostTargetFolder + "\Aggregation Kernel\End User Apps\LHS ETL Tool\"
$ModelsSFolder = $ReadHostSourceFolder + "\Aggregation Kernel\Aggregation Kernel Build\Models"
$ModelsTFolder = $ReadHostTargetFolder + "\Aggregation Kernel\Aggregation Kernel Build\Models"

## Clones just the folder structure from the Source to the Target destination
robocopy $ReadHostSourceFolder $ReadHostTargetFolder /e /xf *.*

## Copies additional files to the new purpose
Copy-Item -Path $RPackSFolder\*.* -Destination $RPackTFolder  
Copy-Item -Path $RViewerSFolder\*.* -Destination $RViewerTFolder  
Copy-Item -Path $DPackSFolder\*.* -Destination $DPackTFolder  
Copy-Item -Path $LHSSFolder\*.* -Destination $LHSTFolder   

这是复制acl脚本:

$ReadHostSourceFolder = "C:\SP6"  
$ReadHostTargetFolder = "C:\SP7"  

$TFolders = GCI $ReadHostTargetFolder -Recurse | Select -ExpandProperty Fullname  

GCI -LiteralPath $ReadHostSourceFolder -Recurse | % {  
    $SPath = $_.FullName  
    $TPath = $_.FullName -replace     "$($ReadHostSourceFolder.replace("\","\\"))","$ReadHostTargetFolder"  
    $NewACL = Get-Acl -LiteralPath $SPath  
    $NewACL.SetAccessRuleProtection($True, $True)  
    If ($TFolders -ccontains $TPath){  
        Try{          
        Set-Acl -LiteralPath $TPath -AclObject $NewACL -EA Stop  
        Echo "Copied permission from $SPath to $TPath"  
        }Catch{  
        Echo "Error on copying permission from $SPath to $TPath : $($_.Exception.Message)"  
        }  
    } Else{  
    Echo "Unable to find destination for $SPath"  
    }  
}  

显然,我将从复制acl脚本中删除重复的$ReadHostSourceFolder$ReadHostTargetFolder

我很难过,所以非常感谢任何帮助。

编辑:

找到一些额外的信息,如果我将$ReadHostTargetFolder设置为c:\ SP7它不起作用,但是如果我将它设置为C:\ SP7(使用大写C :)它确实有效...不我设置$ReadHostSourceFolder的情况似乎很重要,甚至在$ReadHostTargetFolder中C:\之后我使用的情况如何C:\ sp7和C:\ SP7一样好,而c:\ SP7或c:\ sp7则不行。

我怀疑它是由于这一行"$($ReadHostSourceFolder.replace("\","\\"))","$ReadHostTargetFolder"使它全部区分大小写,不确定为什么呢?

0 个答案:

没有答案