windows - 我将如何在 Powershell 中的每个循环中保存我的凭据

标签 windows powershell server credentials

有没有办法保存我的凭据,这样它就不会一直询问每个循环?比如,将它隔离在 foreach 循环之外。我尝试删除 -credentials 并将其添加到外部,但它给我一个未经授权访问的错误。我似乎无法弄清楚。抱歉,如果这个问题看起来很愚蠢,我是 Powershell 的新手。

Import-Module ActiveDirectory

$ServerList = Get-Content "C:\servers.txt"

foreach($ServerName in $ServerList){

"$ServerName"
"=========="
Get-WmiObject -Class Win32_UserAccount -Computer $ServerName -Filter "LocalAccount='True'" -credential CORPORATE\usmenm03adm| Select Name

" "

}

最佳答案

您可以在循环外定义凭据。 我像下面这样编辑了你的脚本

Import-Module ActiveDirectory

$ServerList = Get-Content "C:\servers.txt"

$cred=Get-Credential

foreach($ServerName in $ServerList){

"$ServerName"
"=========="
Get-WmiObject -Class Win32_UserAccount -Computer $ServerName -Filter "LocalAccount='True'" -credential $cred | Select Name

" "
}

关于windows - 我将如何在 Powershell 中的每个循环中保存我的凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40299181/

相关文章:

c# - 何时/如何注销 RegisteredWaitHandle

powershell - 防止绕过PowerShell执行策略

powershell - 我不能在 ForEach-Object 中使用开始/处理/结束 block ?

c - C Socket编程中的多个客户端

Python:将文件发送到没有第三方库的服务器?

windows - Vagrant:如何禁用 Windows 主机的 NFS 同步文件夹?

c# - 从证书存储中获取 X509Certificate 列表 C# MVC

c - Visual-C Win32 API 分层窗口闪烁

powershell - VSTS : Pass build/release variables into Powershell script task

windows - Dockerfile vs从容器创建镜像