【发布时间】:2020-06-26 01:24:12
【问题描述】:
我正在尝试在 LAN(而不是域)上的多台机器上运行以下命令。当我在本地机器上不使用 Invoke-Command 运行时,它运行良好。当我尝试调用时,它无法再在我正在运行命令的机器上找到文件路径,因为它正在查看远程目录。我无法为此目的使用共享驱动器。我有一个类似的问题,建议并成功实施了哈希表。我无法弄清楚我将如何使用以下内容来做到这一点。
Invoke-Command -Computername $computers -Credential {
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName,DisplayVersion,Publisher,InstallDate,InstallLocation | Format-Table -Property DisplayName,DisplayVersion,Publisher,InstallDate,InstallLocation -AutoSize | Out-File -Width 2048 "c:\scripts\ComputerInformation\SoftwareInformation\$env:COMPUTERNAME.software.txt"
$applications = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
foreach ($application in $applications) {
$hostname = $env:COMPUTERNAME
$DisplayName = $application.DisplayName
$displayVersion = $application.DisplayVersion
$HelpLink = $application.HelpLink
$IdentifyingNumber = $application.PSChildName
$InstallDate = $application.InstallDate
$RegOwner = $null
$vendor = $application.Publisher
if ($DisplayName -ne $null -or $DisplayVersion -ne $null -or $HelpLink -ne $null -or $IdentifyingNumber -ne $null -or $InstallDate -ne $null -or $vendor -ne $null ) {
Add-Content -Path 'c:\scripts\Inventories\SoftwareInventory.csv' "$hostname,$DisplayName,$DisplayVersion,$HelpLink,$IdentifyingNumber,$InstallDate,$RegOwner,$Vendor"
}
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName,DisplayVersion,Publisher,InstallDate,InstallLocation | Format-Table -Property DisplayName,DisplayVersion,Publisher,InstallDate,InstallLocation -AutoSize | Out-File -Append -Width 2048 "c:\scripts\ComputerInformation\SoftwareInformation\$env:COMPUTERNAME.software.txt"
$applications = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*
foreach ($application in $applications) {
$hostname = $env:COMPUTERNAME
$DisplayName = $application.DisplayName
$displayVersion = $application.DisplayVersion
$HelpLink = $application.HelpLink
$IdentifyingNumber = $application.PSChildName
$InstallDate = $application.InstallDate
$RegOwner = $null
$vendor = $application.Publisher
if ($DisplayName -ne $null -or $DisplayVersion -ne $null -or $HelpLink -ne $null -or $IdentifyingNumber -ne $null -or $InstallDate -ne $null -or $vendor -ne $null ) {
Add-Content -Path 'c:\scripts\Inventories\SoftwareInventory.csv' "$hostname,$DisplayName,$DisplayVersion,$HelpLink,$IdentifyingNumber,$InstallDate,$RegOwner,$Vendor"
}
}
【问题讨论】:
-
为什么不使用 PowerShell 重定向来处理这个用例? about_Redirection - PowerShell | Microsoft Docs --- 另见Powershell logging from invoke-command
-
关于最佳实践的单独说明。您应该将 $null 放在比较的左侧。文档here.
-
@postanote 我认为my answer 与您引用的一致。让我知道你的想法。谢谢。
-
好交易,@Steven 和 yeppers,相对于我指导 OP 使用的内容,你说得对。