Powershell script Save Password

Source: Internet
Author: User

When using a batch or vbs script to access a remote computer, we can directly write the user name and password to the script. However, in powershell, the password cannot be written directly, although this greatly improves the security of the script, it sometimes brings us some trouble.

If we want to obtain the system information of the Computer (192.168.12.6), we need to use the get-credential command to provide creden (username + password). We can first save the creden to a variable, as shown in

$ A = Get-credential

Enter the user name and password in the pop-up window. After the input is complete, it will be saved in the variable $ A and then called using the following command

Get-wmiobject win32_operatingsystem-credential$-Computer 192.168.12.6

 

If only one computer is okay, if there are multiple computers, and their usernames and passwords are different, you need to enter them n times, causing a lot of trouble. To solve this problem, we can save the user name and password by creating an object and write it into the script.

Next, let's take a look at the $ A object type and its corresponding attributes. We can know that the object type name is: system. management. automation. pscredential, where the password and username are both attributes, so you can create a new object of this type. Note that the password type is securestring and the username type is string. Therefore, you need to convert the plaintext password to a secure string. You can use the convertor-securestring command.

The Code is as follows:

$ Username = "Administrator"

# Convert the plain text string "boc.123" to a secure string and store the result in the $ password variable.

$ Password = convertid-securestring "boc.123"-asplaintext-Force

# Create an object

$ Cred = new-Object System. Management. Automation. pscredential ($ username, $ password)

Get-wmiobject win32_operatingsystem-credential $ cred-computer 192.168.12.6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.