使用 Invoke-Command 获取 Get-ADComputer 的结果
Getting Results of Get-ADComputer with Invoke-Command
我正在编写一个脚本,该脚本应 运行 在未安装任何 RSAT 工具的客户端(桌面)Windows 计算机上。该脚本将轮询有关机器的各种信息,我想包括的其中一项是 Active Directory 中的机器描述。最终目标是将这些保存到 SQL 中以获得漂亮的 table。
除了广告描述,我能得到所有东西。这就是我现在正在尝试的:
$Computer = (Get-WmiObject Win32_ComputerSystem | Select -Expand Name) | Out-String
$session = New-PSSession -ComputerName domainController
Invoke-Command -Session $session -ScriptBlock {
param(
[Parameter(Position=0)]
$computer
)
Import-Module ActiveDirectory
write-host "the value of the passed parameter is $computer"
Get-ADComputer -SearchBase 'DC=CONTOSO,DC=LOCAL' -Filter {Name -Like $computer} -Properties Description | Select -Expand Description
write-host "the name of this computer is $(Get-WmiObject Win32_ComputerSystem | Select -Expand Name)"
write-host $description
} -ArgumentList $Computer
我知道正在传递计算机名,因为
的结果
write-host "the value of the passed parameter is $computer"
确实显示了我经过的计算机的名称。我知道该命令确实在服务器上 运行ning 因为
的结果
write-host "the name of this computer is $(Get-WmiObject Win32_ComputerSystem | Select -Expand Name)"
确实显示了安装了 ADUC 的服务器的名称。我知道拉取描述的命令在该服务器上有效,因为如果我使用 RDP 连接到所述服务器,我可以 运行 该命令没有问题。
我没有收到任何错误(我的 $ErrorActionPreference 是 "Stop"),它只是像注释一样跳过代码行。
有没有我遗漏的东西,或者更好的方法让我提取所述计算机的描述?
您不需要从 Active Directory 远程请求信息。这是一个甚至不使用 AD cmdlet 的简短示例:
$computerName = [Net.Dns]::GetHostName()
$searcher = [ADSISearcher] "(sAMAccountName=$computerName$)"
$searcher.PropertiesToLoad.AddRange(@("description"))
$searchResult = $searcher.FindOne()
"Computer description: {0}" -f $searchResult.Properties["description"][0]
我正在编写一个脚本,该脚本应 运行 在未安装任何 RSAT 工具的客户端(桌面)Windows 计算机上。该脚本将轮询有关机器的各种信息,我想包括的其中一项是 Active Directory 中的机器描述。最终目标是将这些保存到 SQL 中以获得漂亮的 table。
除了广告描述,我能得到所有东西。这就是我现在正在尝试的:
$Computer = (Get-WmiObject Win32_ComputerSystem | Select -Expand Name) | Out-String
$session = New-PSSession -ComputerName domainController
Invoke-Command -Session $session -ScriptBlock {
param(
[Parameter(Position=0)]
$computer
)
Import-Module ActiveDirectory
write-host "the value of the passed parameter is $computer"
Get-ADComputer -SearchBase 'DC=CONTOSO,DC=LOCAL' -Filter {Name -Like $computer} -Properties Description | Select -Expand Description
write-host "the name of this computer is $(Get-WmiObject Win32_ComputerSystem | Select -Expand Name)"
write-host $description
} -ArgumentList $Computer
我知道正在传递计算机名,因为
的结果write-host "the value of the passed parameter is $computer"
确实显示了我经过的计算机的名称。我知道该命令确实在服务器上 运行ning 因为
的结果write-host "the name of this computer is $(Get-WmiObject Win32_ComputerSystem | Select -Expand Name)"
确实显示了安装了 ADUC 的服务器的名称。我知道拉取描述的命令在该服务器上有效,因为如果我使用 RDP 连接到所述服务器,我可以 运行 该命令没有问题。
我没有收到任何错误(我的 $ErrorActionPreference 是 "Stop"),它只是像注释一样跳过代码行。
有没有我遗漏的东西,或者更好的方法让我提取所述计算机的描述?
您不需要从 Active Directory 远程请求信息。这是一个甚至不使用 AD cmdlet 的简短示例:
$computerName = [Net.Dns]::GetHostName()
$searcher = [ADSISearcher] "(sAMAccountName=$computerName$)"
$searcher.PropertiesToLoad.AddRange(@("description"))
$searchResult = $searcher.FindOne()
"Computer description: {0}" -f $searchResult.Properties["description"][0]