用于将文件夹和内容复制到远程位置的Powershell脚本

时间:2015-12-23 21:37:48

标签: powershell

我编写了一个power-shell脚本,可以在服务器上将文件从一个位置复制到另一个位置。我试图在一个服务器位置组装文件,同时这样做获取权限错误。下面的脚本: -

$date = (Get-Date).ToString("_MMddyyyy")

#Storing servers

$Servers = Get-Content C:\server.txt

#Scanning Servers and executing 

$Servers | ForEach-Object {

invoke-command -ComputerName $_ -Scriptblock {
$CompName = (Get-WmiObject -Class Win32_ComputerSystem).Name


#Defining Source and Destination path

$DestPath =  "\\$CompName\d$\Temp\IIS_Logs_"+"$CompName"
$SourcePath = "\\$CompName\d$\Logs\W3SVC85\u_ex15122116.log"

#Creating new folder for storing backup

New-Item -Path $DestPath -ItemType directory

#Copying folder

copy -Recurse -Path $SourcePath -destination $DestPath 


}}

4 个答案:

答案 0 :(得分:2)

我删除了invoke命令,并在更改逻辑脚本后按预期工作。

$date = (Get-Date).ToString("_MMddyyyy")

#Storing servers

$Servers = Get-Content C:\server.txt

#Scanning Servers and executing 

$Servers | ForEach-Object {

#Defining Source and Destination path

$DestPath =  "d:\Temp\IIS_Logs_"+"$_"
$SourcePath = "\\$_\d$\u_ex15122717.log"

#Creating new folder for storing backup

New-Item -Path $DestPath -ItemType directory

#Copying folder

Copy-Item -Recurse -Path $SourcePath -destination $DestPath 

}

答案 1 :(得分:1)

您使用的是具有D $管理员共享权限的帐户吗?如果要在远程计算机上调用该命令进行本地复制,则不需要像网络计算机那样引用它,而只需: d:\ TEMP \ IIS_Logs _" +" $ COMPNAME"

答案 2 :(得分:0)

以下代码会将文件夹try: #The code that causes the issue except IndexError: pass 复制到下面<local-folder-path>指定的远程位置。以下函数假设您已设置<remote-folder-path>$session$MySecureCreds个对象。

$session

答案 3 :(得分:0)

如果您不使用共享,这是另一种方法,它可以做到:

  • LocalToRemote
  • RemoteToLocal
  • RemoteToRemote
function MakeRemoteFolder($RemoteServer, $RemotePath)
{
    Invoke-Command -ScriptBlock {
        param (
            [Parameter(Mandatory=$false)][string]$RemotePath
        )
        If(!(test-path $RemotePath))
        {
            New-Item -ItemType Directory -Force -Path $RemotePath
        }
    } -ComputerName $RemoteServer -ArgumentList $RemotePath
}

function LocalToRemote($LocalPath, $RemoteServer, $RemotePath)
{
    Write-Host "Make folder on Remote Server"
    MakeRemoteFolder -RemoteServer $RemoteServer -RemotePath $RemotePath

    Write-Host "Send To Remote Server"
    $TargetSession = New-PSSession -ComputerName $RemoteServer
    Copy-Item -Path $LocalPath -Destination $RemotePath -ToSession $TargetSession -Verbose -Recurse
}

function RemoteToLocal($LocalPath, $RemoteServer, $RemotePath)
{
    Write-Host "Make folder on local system"
    If(!(test-path $LocalPath))
    {
        New-Item -ItemType Directory -Force -Path $LocalPath
    }

    Write-Host "Receive From Remote Server"
    $TargetSession = New-PSSession -ComputerName $RemoteServer
    Copy-Item -Path $RemotePath -Destination $LocalPath -FromSession $TargetSession -Verbose -Recurse
}

function RemoteToRemote($LocalDestinationPath, $LocalSourcePath, $RemoteServer1, $RemotePath1, $RemoteServer2, $RemotePath2)
{
    # Make sure that you have the space needed on the local system, not recomended for big files or folders.

    # Receive From Remote Server 1
    Write-Host "Make folder on local system"
    If(!(test-path $LocalDestinationPath))
    {
        New-Item -ItemType Directory -Force -Path $LocalPath
    }

    Write-Host "Receive From Remote Server 1"
    $TargetSession = New-PSSession -ComputerName $RemoteServer1
    Copy-Item -Path $RemotePath1 -Destination $LocalDestinationPath -FromSession $TargetSession -Verbose -Recurse

    # Send To Remote Server
    Write-Host "Make folder on Remote Server 2"
    MakeRemoteFolder -RemoteServer $RemoteServer2 -RemotePath $RemotePath2

    Write-Host "Send To Remote Server 2"
    $TargetSession = New-PSSession -ComputerName $RemoteServer2
    Write-Host "LocalSourcePath $($LocalSourcePath)"
    Copy-Item -Path $LocalSourcePath -Destination $RemotePath2 -ToSession $TargetSession -Verbose -Recurse
}

# LocalToRemote
Write-Host "LocalToRemote"
$FolderOrFileToSend = "LocalToRemote" # Eg. "Hallo World.txt" for a file

$LocalPath = "C:\Test\$($FolderOrFileToSend)"
$RemotePath = "C:\Test"
$RemoteServer = "RemoteServer"

LocalToRemote `
    -LocalPath $LocalPath `
    -RemoteServer $RemoteServer `
    -RemotePath $RemotePath


# RemoteToLocal
Write-Host "RemoteToLocal"
$FolderOrFileToSend = "RemoteToLocal" # Eg. "Hallo World.txt" for a file

$LocalPath = "C:\Test"
$RemotePath = "C:\Test\$($FolderOrFileToSend)"
$RemoteServer = "RemoteServer"

RemoteToLocal `
    -LocalPath $LocalPath `
    -RemoteServer $RemoteServer `
    -RemotePath $RemotePath


# RemoteToRemote
Write-Host "RemoteToRemote"
$FolderOrFileToSend = "RemoteToRemote" # Eg. "Hallo World.txt" for a file

$LocalDestinationPath = "C:\Test"
$LocalSourcePath = "C:\Test\$($FolderOrFileToSend)"
$RemotePath1 = "C:\Test\$($FolderOrFileToSend)"
$RemoteServer1 = "RemoteServer1"
$RemotePath2 = "C:\Test"
$RemoteServer2 = "RemoteServer2"

RemoteToRemote `
    -LocalDestinationPath $LocalDestinationPath `
    -LocalSourcePath $LocalSourcePath `
    -RemoteServer1 $RemoteServer1 `
    -RemotePath1 $RemotePath1 `
    -RemoteServer2 $RemoteServer2 `
    -RemotePath2 $RemotePath2