1

我正在使用一个 PowerShell 脚本从网站下载内容(一个 zip 文件),该网站需要凭据才能获得该网站的身份验证。此外,已使用代理和代理凭据,因为它是经过身份验证的代理。我收到一个错误,我在下面粘贴了该错误。

我曾尝试提供代理和代理凭据,但没有运气。我已经绕过了使用Powershell -ExecutionPolicy Bypass. 当我尝试从浏览器访问该网站时,我可以访问该 zip 文件。

请在下面找到代码片段:

#Path to Save data
$tipath = "C:\Somepath"
$localpath = "\somefile.zip"
$timepath = "\Zip_file.txt"
$unzippath = "\Someotherpath"
$storepath = $tipath + $localpath
$remotefilepath = $tipath + $timepath
$unzipfolder = $tipath + $unzippath

#   Create Folder If Not Exists #
$FolderToCreate = $tipath
if (!(Test-Path $FolderToCreate -PathType Container)) {
    New-Item -ItemType Directory -Force -Path $FolderToCreate
}

#Downloading File #
$username = "username"
$password = "Password" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)

$secPasswd = "password123" | ConvertTo-SecureString -AsPlainText -Force
$wc = New-Object System.Net.WebClient
$wc.Credentials = New-Object System.Net.NetworkCredential("domain\user", $secPasswd)

# get remote file info
Invoke-WebRequest -Proxy "http://x.x.x.x:8080" -ProxyCredential $myCreds -Uri "https://somewebsite/checksum.txt" -OutFile $remotefilepath -Credential $cred
$remotefile = Get-Content -Path $remotefilepath
Write-Host Remote File Hash : $remotefile

# if the file exists already
if (Test-Path $storepath) {
    # get local file info
    $localfile = (Get-FileHash $storepath -Algorithm SHA256).Hash
    Write-Host Local File Hash : $localfile

    # if the remote file is newer than the local file
    if ($remotefile -ne $localfile) {
        Invoke-WebRequest -Proxy "http://x.x.x.x:8080" -ProxyCredential $myCreds -Uri "https://somewebsite/checksum.txt" -OutFile $remotefilepath -Credential $cred
        Invoke-WebRequest -Proxy "http://x.x.x.x:8080" -ProxyCredential $myCreds -Uri "https://somewebsite/somezipfile.zip" -OutFile $storepath -Credential $cred

此代码应检查哈希并直接下载 从网站下载 zip 文件并解压缩。如果它可以在 zip 文件中下载,那么我可以在后面的部分进行故障排除。

PS C:\Users\Administrator\Desktop> powershell -ExecutionPolicy ByPass -File C:\Users\Administrator\Desktop\mypowershell.ps1
powershell : 调用-WebRequest : .?? ?????????????????????
在行:1 字符:1
+ powershell -ExecutionPolicy ByPass -文件 C:\Users\Administrator\Desktop\mypowershell.p ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (Invoke-WebReque...??? ????? ?????:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

您尝试访问的网站包含被禁止的内容。
如果您认为您尝试访问的网站不包含任何此类
内容,请点击这里。

以上是我得到的错误。

4

0 回答 0