让我解释一下我的情况:我有两台运行Windows Server 2019的服务器,并且它们都安装了IIS v10。
两个服务器都是工作组的一部分。
在Server2上,有一些在iis上运行的Web,并且有一些server1需要读取的图片。
Server2上的文件夹图片权限由所有人完全控制。
并且我像这样在Server1上的PowerShell中运行脚本:
$path = "D:\share_local\test.jpg"
$url = "\\192.168.1.55\share_object\test.jpg"
$username = "192.168.1.55\Administrator"
$password = "P@ssw0rdshare"
$cli_web = new-object System.Net.WebClient
$cli_web.UseDefaultCredentials = $False
$cli_web.Credentials = new-object System.Net.NetworkCredential($username, $password)
$cli_web.DownloadFile( $url, $path )
$data = $cli_web.DownloadString($url)
我收到一个错误:
使用“ 2”作为参数调用“ DownloadFile”的异常:“用户名或密码不正确。”在线:9字符:1+ $ cli_web.DownloadFile($ url,$ path)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo:未指定:(:) [],MethodInvocationException+ FullyQualifiedErrorId:WebException
使用“ 1”作为参数调用“ DownloadString”的异常:“用户名或密码不正确。”在第10行:1个字符+ $ data = $ data.DownloadString($ url)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo:未指定:(:) [],MethodInvocationException+ FullyQualifiedErrorId:WebException
我非常确定用户名和密码正确,但是仍然显示不正确。并且我编写了python脚本来获取文件,以证明用户名和密码正确并且可以正常运行的代码如下。
from io import BytesIO
import tempfile
from smb.SMBConnection import SMBConnection
userID = 'Administrator'
password = 'P@ssw0rdshare'
client_machine_name = 'localpcname'
server_name = 'servername'
server_ip = '192.168.1.55'
domain_name = 'domainname'
conn = SMBConnection(userID, password, client_machine_name, server_name, domain=domain_name, use_ntlm_v2=True,
is_direct_tcp=True)
conn.connect(server_ip, 445)
shares = conn.listShares()
attr = conn.getAttributes('share_object', 'test.jpg')
file_obj = BytesIO()
file_attributes, filesize = conn.retrieveFile('share_object', 'test.jpg', file_obj)
with open("test.jpg", "wb") as outfile:
# Copy the BytesIO stream to the output file
outfile.write(file_obj.getbuffer())
print ("download finished")
conn.close()
但是我必须在iis上将.net中的System.Net.WebClient用于我的Web。
对于摘要,我有疑问,为什么
$cli_web.Credentials = new-object System.Net.NetworkCredential($username, $password)
不起作用。请给我建议
谢谢
您的代码段运行正常,PS脚本出了点问题。请参考以下链接。