0

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 : WebException

Exception 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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Brice
  • 1
  • Can you try giving the username as `Administrator` or `.\Administrator` ? – Theo May 09 '20 at 13:30
  • 1
    As an aside: Please avoid pseudo method syntax: instead of `New-Object SomeType(arg1, ...)`, use `New-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`). – mklement0 May 09 '20 at 13:45
  • I don't have an explanation, but since you're targeting an SMB share, you can try to (temporarily) create a drive with [`New-PSDrive`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive) and then simply use `Copy-Item` to get the file. – mklement0 May 09 '20 at 13:53

1 Answers1

0

Your code snippet works perfectly, there is something wrong with the PS script. Please refer to the below link.

$path = "test.JPG"
$url = "file:\\10.157.19.131\\share\"
$username = ".\administrator"
$password = "abcde12345!"
$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)
Set-Content mytest.JPG -Value $cli_web.DownloadData($url+$path) -Encoding Byte 

About Powershell Set-Content command.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-content?view=powershell-7
Here are some related discussions.
Using WebClient to access local files
Write bytes to a file natively in PowerShell

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22