Let me explain my situation: I have 2 server running Windows Server 2019, and both of them have IIS v10 installed.
- server1 is IP 192.168.1.54
- server2 is IP 192.168.1.55
both servers are part of a workgroup.
On Server2 have some web that run on iis and have some picture that server1 need to read.
Folder picture on Server2 permission is everyone full control.
And I run script in PowerShell on Server1 like this:
$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)
I get an error:
Exception calling "DownloadFile" with "2" argument(s): "The user name or password is incorrect. "
At line:9 char:1
+ $cli_web.DownloadFile( $url, $path )
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebExceptionException calling "DownloadString" with "1" argument(s): "The user name or password is incorrect.
"
At line:10 char:1
+ $data = $data.DownloadString($url)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
I'm very sure that User and password is correct but it still show incorrect. and I write python script to get file for prove that user and password is correct and it work fine code is below.
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()
however I must used System.Net.WebClient in .net for my web on iis.
For summary I have question why
$cli_web.Credentials = new-object System.Net.NetworkCredential($username, $password)
is not working. Please give me advice
Thank you
Administrator
or.\Administrator
? – TheoNew-Object SomeType(arg1, ...)
, useNew-Object SomeType [-ArgumentList] arg1, ...
- PowerShell cmdlets, scripts and functions are invoked like shell commands, not like methods. That is, no parentheses around the argument list, and whitespace-separated arguments (,
constructs an array as a single argument, as needed for-ArgumentList
). – mklement0New-PSDrive
and then simply useCopy-Item
to get the file. – mklement0