如何在使用不同凭证时列出所有计算机操作系统



我尝试检索计算机名称及其当前操作系统的列表。一切都工作得很好,直到我在这个特定的服务器上没有访问我的凭据。解释:为了登录服务器,我必须使用另一个帐户。对于每个用户列表CurrentUserName+"ad"(用于管理)servers.txt类似于

Server1

Server2

Server3

让我给你看两个。

1。

$Login = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name +"ad"
$cred = Get-Credential $Login
$servers = get-Content .server.txt
$OS = Get-WmiObject -class Win32_OperatingSystem -computername $servers -Credential $cred

$OS | Select-Object CSName, Caption, OSArchitecture | Export-CSV -path .$(get-date -f yyyyMMd)_CurrentOS.csv
$OS | select CSName, Caption, OSArchitecture | Out-GridView -Title 'Current OS'

一切都很好,直到我添加了一个服务器,我没有登录权限。之后,我得到以下错误,没有服务器是成功的。

Get-WmiObject: Zugriff verweigert (Ausnahme von HRESULT: 0x80070005 (E_ACCESSDENIED))

2。

$Login = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name +"ad"
$cred = Get-Credential $Login
$servers = get-Content .server.txt
foreach ($server in $servers)
{
    Try {
     $OS = Get-WmiObject -class Win32_OperatingSystem -computername $server -Credential $cred -ea SilentlyContinue
    } Catch {
    Write-Host 'Logon Credentials not accepted. No access to'$Server'.' -fore white -back red
    } Finally {
     Write-Host 'Logon Credentials accepted. Grant access to'$Server'.'
    }
}
$OS | Select-Object CSName, Caption, OSArchitecture | Export-CSV -path .$(get-date -f yyyyMMd)_CurrentOS.csv
$OS | select CSName, Caption, OSArchitecture | Out-GridView -Title 'Current OS'

屏幕上的输出很好,除了"Out-GridView",这里只是GridView中显示的最后一个服务器。

已接受登录凭据。授予访问DEBZIAPP104的权限。

当添加这个"坏服务器"时,我将得到

已接受登录凭据。授权访问DEBZIAPP104.

不接受登录凭据。无法访问DEBZIAPP106.

已接受登录凭据。授权访问DEBZIAPP106.

我想列出所有正常的服务器。如果出现一个服务器,我没有登录权限,应该会显示一条消息,脚本需要继续。

如果有不清楚的地方请询问。希望有人能帮忙。:)

问候,帕特里克。

Try/Catch只捕获终止错误。

这应该让你明白:

$OS = Get-WmiObject -class Win32_OperatingSystem -computername $server -Credential $cred -ea Stop

相关内容

最新更新