将Copy-Item Network Drive用于本地驱动器时清空文件夹

时间:2018-01-04 19:04:41

标签: powershell

我目前正在使用此Powershell脚本

Function Copy-ItemUNC { 
    New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"  

    Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files'; 
} 

当我运行脚本时,它会创建文件夹,但其中没有子文件夹。

我遇到的第二个问题是我必须手动打开Windows资源管理器并输入首先连接到它的路径才能使该脚本运行。有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是因为你没有要求使用子目录,因为这需要你这样做。

Copy-Item -Recurse

-Recurse <SwitchParameter>
    Indicates that this cmdlet performs a recursive copy.

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       False
    Accept wildcard characters?  false

(Get-Command -Name Copy-Item).Parameters.Keys
Get-Help -Name Copy-Item -Full
Get-Help -Name Copy-Item -Examples


This command copies the mar1604.log.txt file to the C:\Presentation directory. The command does not delete the original file.
Example 2: Copy the contents of a directory to another directory
PS C:\>Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse

所以,这一行应该是这样的..

Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files' -Recurse

你也不需要这个......

New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"  

...基于你的追求。特别是因为您没有在代码中的任何位置使用它。也不需要那个分号。

相关问题