You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have used the upload code to transfer file from my local Linux host to a remote Windows using the below code.
def run_to_copy(server_name, local_path, remote_path) :
"""
Usage:
"""
host_name = "http://"+ server_name +":5985/wsman"
win_connect = Protocol(endpoint=host_name, transport='kerberos')
shell_id = win_connect.open_shell()
step = 1024
timeout = 3600
size = os.stat(local_path).st_size
start = time.time()
with open(local_path, 'rb') as f:
for i in range(0, size, step):
script = (
'add-content -value '
'$([System.Convert]::FromBase64String("{}")) '
'-encoding byte -path {}'.format(
base64.b64encode(f.read(step)),
remote_path
)
)
while True:
encoded_ps = base64.b64encode(script.encode('utf_16_le')).decode('ascii')
command_id = win_connect.run_command(shell_id, 'powershell -encodedcommand {0}'.format(encoded_ps))
code, stdout, stderr = win_connect.get_command_output(shell_id, command_id)
if time.time() - start >= timeout:
raise Exception('WinRM upload file transfer timedout '+ str(timeout) +".")
if code == 0:
break
elif code == 1 and 'used by another process' in stderr:
time.sleep(0.1)
else:
print(stderr)
print(stdout.decode('utf-8'))
raise Exception('Failure: in transfer.')
When i execute the same i get the following encoding exception when transferring a simple ASCII file:
< CLIXML Exception calling "FromBase64String" with "1" argument(s): "The input is not a x000D__x000Avalid Base-64 string as it contains a non-base 64 character, more than two x000D__x000Apadding characters, or an illegal character among the padding characters. "x000D__x000AAt line:1 char:22_x000D__x000A_+ add-content -value x000D__x000A$([System.Convert]::FromBase64String("b'SGVsbG8gV29ybGQK'")) ...x000D__x000A+ x000D__x000A~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~x000D__x000A + CategoryInfo : NotSpecified: (:) [], MethodInvocationException_x000D__x000A_ + FullyQualifiedErrorId : FormatException_x000D__x000A_x000D__x000A
Traceback (most recent call last):
The text was updated successfully, but these errors were encountered:
I have used the upload code to transfer file from my local Linux host to a remote Windows using the below code.
def run_to_copy(server_name, local_path, remote_path) :
"""
Usage:
"""
host_name = "http://"+ server_name +":5985/wsman"
win_connect = Protocol(endpoint=host_name, transport='kerberos')
shell_id = win_connect.open_shell()
step = 1024
timeout = 3600
size = os.stat(local_path).st_size
start = time.time()
with open(local_path, 'rb') as f:
for i in range(0, size, step):
script = (
'add-content -value '
'$([System.Convert]::FromBase64String("{}")) '
'-encoding byte -path {}'.format(
base64.b64encode(f.read(step)),
remote_path
)
)
while True:
encoded_ps = base64.b64encode(script.encode('utf_16_le')).decode('ascii')
command_id = win_connect.run_command(shell_id, 'powershell -encodedcommand {0}'.format(encoded_ps))
code, stdout, stderr = win_connect.get_command_output(shell_id, command_id)
if time.time() - start >= timeout:
raise Exception('WinRM upload file transfer timedout '+ str(timeout) +".")
if code == 0:
break
elif code == 1 and 'used by another process' in stderr:
time.sleep(0.1)
else:
print(stderr)
print(stdout.decode('utf-8'))
raise Exception('Failure: in transfer.')
When i execute the same i get the following encoding exception when transferring a simple ASCII file:
< CLIXML
Exception calling "FromBase64String" with "1" argument(s): "The input is not a x000D__x000Avalid Base-64 string as it contains a non-base 64 character, more than two x000D__x000Apadding characters, or an illegal character among the padding characters. "x000D__x000AAt line:1 char:22_x000D__x000A_+ add-content -value x000D__x000A$([System.Convert]::FromBase64String("b'SGVsbG8gV29ybGQK'")) ...x000D__x000A+ x000D__x000A~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~x000D__x000A+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException_x000D__x000A_+ FullyQualifiedErrorId : FormatException_x000D__x000A_x000D__x000ATraceback (most recent call last):
The text was updated successfully, but these errors were encountered: