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 |