因为我很多时候在工作组模式下执行操作,所以远程管理其它主机时,必须要输入凭证信息。一般都会用到Get-Credential来弹出提示框而输入密码。

比如:

=Get-Credential -Credential DBA_User
Get-WmiObject -Credential  -Class Win32_LogicalDisk -ComputerName 10.0.0.10

只查询一台远程主机还好,查询很多台主机,就要输入好多次密码。有点麻烦!

于是我就想能不能把用户名和密码保存起来,直接调用。还是有方法的:

=
=ConvertTo-SecureString   -AsPlainText -Force;
=New-Object System.Management.Automation.PSCredential(,);
Get-WmiObject -Credential  -Class Win32_LogicalDisk -ComputerName 192.168.1.111

PowerShell中所有东西都是对象。当然Get-Credential也不例外。先实例化一个它的对象,在后面调用就可以了。

只是要这里说明的是,密码必须转成SecureString才可以。

-------------------------------------