【发布时间】:2020-12-23 21:36:20
【问题描述】:
我正在尝试使用 Powershell 从我们的 HTTP 服务器复制一个目录文件夹,我想将它的全部内容(包括子文件夹)复制到我当前服务器的本地驱动器中。这样做的目的是为了服务器部署自动化,这样我的老板就可以运行我的 powershell 脚本并进行整个服务器设置,并将我们所有的文件夹复制到它的 C: 驱动器。这是我的代码
$source = "http://servername/serverupdates/deploy/Program%20Files/"
$destination = "C:\Program Files"
$client = new-object System.Net.WebClient
$client.DownloadFile($source, $destination)
当我以管理员身份在 Powershell ISE 中运行脚本时,我收到错误消息
“使用“2”个参数调用“DownloadFile”的异常:“WebClient 请求期间发生异常。”
对可能发生的事情有什么建议吗?
我也试过这个代码块,但是当我运行它时没有任何反应,没有错误或任何东西。
$source = "http://serverName/serverupdates/deploy/Program%20Files/"
$webclient = New-Object system.net.webclient
$destination = "c:/users/administrator/desktop/test/"
Function Copy-Folder([string]$source, [string]$destination, [bool]$recursive) {
if (!$(Test-Path($destination))) {
New-Item $destination -type directory -Force
}
# Get the file list from the web page
$webString = $webClient.DownloadString($source)
$lines = [Regex]::Split($webString, "<br>")
# Parse each line, looking for files and folders
foreach ($line in $lines) {
if ($line.ToUpper().Contains("HREF")) {
# File or Folder
if (!$line.ToUpper().Contains("[TO PARENT DIRECTORY]")) {
# Not Parent Folder entry
$items =[Regex]::Split($line, """")
$items = [Regex]::Split($items[2], "(>|<)")
$item = $items[2]
if ($line.ToLower().Contains("<dir>")) {
# Folder
if ($recursive) {
# Subfolder copy required
Copy-Folder "$source$item/" "$destination$item/" $recursive
} else {
# Subfolder copy not required
}
} else {
# File
$webClient.DownloadFile("$source$item", "$destination$item")
}
}
}
}
}
【问题讨论】:
标签: powershell