复制时拒绝访问

时间:2017-02-07 08:53:39

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v4.0 powershell-remoting

我已经实现了一个脚本,我试图将服务器A上的一些文件复制到服务器B.让我向您解释一下这个过程。

我启动了一个循环,它将运行两次,并且在第一次执行中我停止应用程序池并启动应用程序池而不是创建备份而不是尝试复制文件但是它给了我一个错误的访问权限被拒绝,但我编辑了该文件夹的所有权限,但通过脚本,它不允许我从服务器A复制和替换服务器2上的文件。

# Embedding the password in the script.
"Setting  Variables"
$MyDomain = "ranbi" ;
$MyClearTextUsername = "shian" ;
$MyClearTextPassword = "sham@01" ;
$MyUsernameDomain = $MyDomain + '\' + $MyClearTextUsername;
$SecurePassword = ConvertTo-SecureString -String $MyClearTextPassword -AsPlainText -Force ;
$MyCreds = New-Object System.Management.Automation.PSCredential $MyUsernameDomain,$SecurePassword ;

#System Variable for backup Procedure
$date = Get-Date -Format d-MMMM-yyyy-h-m-s                                                             #Variable is used to get the date and time

for ($i=1; $i -le 2; $i++) {
    $servername = "server" + $i
    $backupsrc = "\\$servername\C$\Program Files (x86)\service\healthService\v1_0"                     #backup directory for source Code
    $backupdes = "\\$servername\C$\Temp\ICS-$date"                                                     #destination for backup appending with date time
    $sourcesrc = "\\server1\C$\Deployment\Health\bin"                                                  #source directory for source Code
    $destinationsrc = "\\server1\C$\Program Files (x86)\service\healthService\v1_0\bin"                #destination directory for source Code on Server1
    $forcetwo ="\\server2\C$\bin"                                                                      #destination directory for source Code on Server2
    $sourceweb = "\\Server1\C$\Deployment\Health\web.config"                                           #source directory for webconfig
    $destinationweb = "\\server1\C$\Program Files (x86)\service\healthservice\v1_0\web.config"         #destination directory for webconfig on Ser1
    $destweb = "\\Server2\C$\Program Files (x86)\service\web.config"                                   #destination directory for webconfig on Ser2
    $pathback = Test-Path $backupdes                                                                   #verifying the backup destination path
    $appPoolName = "HealthService"                                                                     #Initialized App Pool Name
    $forcethree = "\\Server2\C$\bin"                                                                   #webconfigfile source for Server2
    $forcefour = "\\Server2\C$\Program Files (x86)\service\healthService\v1_0\bin"                     #webconfigfile destination for Server2

    # Placing the script under a ScriptBlock
    #####
    $MyScriptblock = {
        Param($appPoolName,$pathback,$date,$backupsrc,$backupdes,$sourcesrc,$destinationsrc,$sourceweb,$destinationweb,$servername,$forcetwo,$destweb,$forcethree,$forcefour)

        function fnStartApplicationPool([string]$appPoolName) {
            Import-Module WebAdministration
            if ((Get-WebAppPoolState $appPoolName).Value -ne 'Started') {
                Start-WebAppPool -Name $appPoolName
            }
        }

        function fnStopApplicationPool([string]$appPoolName) {
            Import-Module WebAdministration
            if ((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped') {
                Stop-WebAppPool -Name $appPoolName
            }
        }
        if ($pathback -eq $false) {
            #Copying Data from Source to Destination
            Copy-Item  -Recurse $backupsrc -Destination $backupdes
            Write-Host "Backup Successful on server $servername"

            #Validating the apppool value
            Import-Module WebAdministration
            if ((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped') {
                #Stop apppool
                Stop-WebAppPool -Name $appPoolName
                Write-Host "AppPool Stopped Successfully for $servername"
            }
            if ($servername -eq 'server1') {
                #Copying Data from Source to Destination
                Copy-Item $sourcesrc -Destination $destinationsrc -Recurse -Force               
                Copy-Item $sourcesrc -Destination $forcetwo -Recurse -Force
                Copy-Item $sourceweb -Destination $destinationweb -Recurse -Force
                Copy-Item $sourceweb -Destination $destweb -Recurse -Force
            }
            if ($servername -eq 'server2') { 
                #Copying Data from Source to Destination
                Copy-Item $forcethree -Destination $forcefour -Recurse -Force
            }
            #Start apppool
            Start-WebAppPool -Name $appPoolName
            Write-Host "AppPool Started Successfully on $servername"
            cd c:\
        }
    }

    $result = Invoke-Command -ComputerName $servername -Credential $MyCreds -ScriptBlock $MyScriptblock -ArgumentList $appPoolName,$pathback,$date,$backupsrc,$backupdes,$sourcesrc,$destinationsrc,$sourceweb,$destinationweb,$servername,$forcetwo,$destweb,$forcethree,$forcefour ;
    $result ;
}

cd c:\

我收到以下错误:

Access to the path 'bin' is denied.
    + CategoryInfo          : PermissionDenied: (\\Server2\C$\bin:String) [Copy-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

2 个答案:

答案 0 :(得分:1)

如果您尝试同步IIS场中的某些站点,请使用WebDeploy cmdlet。

Power Shell IIS cmdlets

Sync-WDServer,Sync-WDSite等

答案 1 :(得分:-2)

您是否查看过Delegated Remote Sessions或JEA(Just Enough Admin)?两者都可以解决Kerberos双跳问题。

查看this series of articles以开始委派会话。