Answers: <p>Your python problem is the <code>\n</code> after the commands, <code>write</code> already append a newline, so your script does the following:</p>
  
  <pre><code>$cred = Get-Credential # Start the command
  # This line is ignored as extraneous after Get-Credential, but you can see it in your output.
  Administrator # Send proprely to the User: prompt
  # Sent to the Password: prompt
  Password # Send to the shell input, causing the error at end of your log.
  </code></pre>
  
  <p>Now that's a quite hard way to automate the credential object in powershell, usually the object is build by other way like described in <a href="https://blogs.technet.microsoft.com/gary/2009/07/23/creating-a-ps-credential-from-a-clear-text-password-in-powershell/" rel="noreferrer">this blog post</a> (quoting here for completeness):</p>
  
  <pre><code>$password = "mypassword" | ConvertTo-SecureString -asPlainText -Force
  $username = "nwtraders\administrator" 
  $credential = New-ObjectSystem.Management.Automation.PSCredential($username,$password)
  </code></pre>
  
  <p>And used as such:</p>
  
  <pre><code>Get-WMIObject win32_logicaldisk -ComputerName Server1 -Credential $credential
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2017-07-21T07:49:10.183Z
Answers: 2017-07-21T07:49:10.183Z
Answers: Tensibai
Body: <p>I am trying to write a python script which automates the powershell's Get-Credential method. Here is something i wrote:</p>
  
  <pre><code>import subprocess
  COMMAND_LINE = 'powershell'
  powershell = subprocess.Popen(COMMAND_LINE, shell=True,
                                     stdin=subprocess.PIPE,
                                     stderr=subprocess.STDOUT,
                                     stdout=subprocess.PIPE)
  output = powershell.stdout.readline()
  print output.rstrip()
  powershell.stdin.write('$cred = Get-Credential\n')
  powershell.stdin.write('Administrator\n')
  powershell.stdin.write('Password')
  output = powershell.stdout.readline()
  print output.rstrip()
  out = powershell.communicate()[0]
  print(out)
  </code></pre>
  
  <p>But i am unable to pass the password as above, i am getting following exception:</p>
  
  <pre><code>&gt; PS /root&gt; $cred = Get-Credential
  &gt; 
  &gt; Windows PowerShell credential request Enter your credentials. User:
  &gt; Administrator Password for user Administrator: PS /root&gt;
  &gt; PasswordPassword : The term 'Password' is not recognized as the name
  &gt; of a cmdlet, function, script file, or operable program. Check the
  &gt; spelling of the name, or if a path was included, verify that the path
  &gt; is correct and try again. At line:1 char:1
  &gt; + Password
  &gt; + ~~~~~~~~
  &gt;     + CategoryInfo          : ObjectNotFound: (Password:String) [], CommandNot    FoundException
  &gt;     + FullyQualifiedErrorId : CommandNotFoundException
  </code></pre>
  
  <p>As i am new to python, Can anyone help me here?</p>
  
Tags: <linux><python><powershell>
Title: How to automate Powershell's interactive Get-Credential method?
UserDisplayName: Vivek Lohiya
ThreadText: # How to automate Powershell's interactive Get-Credential method?
  
  (Asked by: username_0 on 2017-07-21)
  
  I am trying to write a python script which automates the powershell's Get-Credential method. Here is something i wrote:
  
  ```
  import subprocess
  COMMAND_LINE = 'powershell'
  powershell = subprocess.Popen(COMMAND_LINE, shell=True,
                                     stdin=subprocess.PIPE,
                                     stderr=subprocess.STDOUT,
                                     stdout=subprocess.PIPE)
  output = powershell.stdout.readline()
  print output.rstrip()
  powershell.stdin.write('$cred = Get-Credential\n')
  powershell.stdin.write('Administrator\n')
  powershell.stdin.write('Password')
  output = powershell.stdout.readline()
  print output.rstrip()
  out = powershell.communicate()[0]
  print(out)
  
  ```
  
  But i am unable to pass the password as above, i am getting following exception:
  
  ```
  > PS /root> $cred = Get-Credential
  > 
  > Windows PowerShell credential request Enter your credentials. User:
  > Administrator Password for user Administrator: PS /root>
  > PasswordPassword : The term 'Password' is not recognized as the name
  > of a cmdlet, function, script file, or operable program. Check the
  > spelling of the name, or if a path was included, verify that the path
  > is correct and try again. At line:1 char:1
  > + Password
  > + ~~~~~~~~
  >     + CategoryInfo          : ObjectNotFound: (Password:String) [], CommandNot    FoundException
  >     + FullyQualifiedErrorId : CommandNotFoundException
  
  ```
  
  As i am new to python, Can anyone help me here?
  
  **Tags:** linux, python, powershell
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-07-21. Score: 5
  
  Your python problem is the `\n` after the commands, `write` already append a newline, so your script does the following:
  
  ```
  $cred = Get-Credential # Start the command
  # This line is ignored as extraneous after Get-Credential, but you can see it in your output.
  Administrator # Send proprely to the User: prompt
  # Sent to the Password: prompt
  Password # Send to the shell input, causing the error at end of your log.
  
  ```
  
  Now that's a quite hard way to automate the credential object in powershell, usually the object is build by other way like described in [this blog post](https://blogs.technet.microsoft.com/gary/2009/07/23/creating-a-ps-credential-from-a-clear-text-password-in-powershell/) (quoting here for completeness):
  
  ```
  $password = "mypassword" | ConvertTo-SecureString -asPlainText -Force
  $username = "nwtraders\administrator" 
  $credential = New-ObjectSystem.Management.Automation.PSCredential($username,$password)
  
  ```
  
  And used as such:
  
  ```
  Get-WMIObject win32_logicaldisk -ComputerName Server1 -Credential $credential
  
  ```
  
  
  ---
  
  