• powershell: 使用指定账户查询AD


    因为我很多时候在工作组模式下执行操作,所以远程管理其它主机时,必须要输入凭证信息。一般都会用到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才可以。

    参考文档:

  • 相关阅读:
    Gym
    [APIO2014] 回文串
    python选课系统
    python面向对象之类成员修饰符
    python面向对象之类成员
    python的shelve模块
    python的re模块
    python的configparser模块
    python的sys和os模块
    python的hashlib模块
  • 原文地址:https://www.cnblogs.com/vmsky/p/13861066.html
Copyright © 2020-2023  润新知