有没有办法保存我的凭据,这样它就不会一直询问每个循环?比如,将它隔离在 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/