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

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

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

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

$uname="DBA_User"
$pwd=ConvertTo-SecureString  "My_Pwd" -AsPlainText -Force;
$cred=New-Object System.Management.Automation.PSCredential($uname,$pwd);
Get-WmiObject -Credential $cred -Class Win32_LogicalDisk -ComputerName 192.168.1.111

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

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

参考文档:

posted on 2020-10-22 21:18  vmsky  阅读(241)  评论(0编辑  收藏  举报