如何将此简单命令转换为从远程系统列表循环的内容?

时间:2016-05-11 12:57:53

标签: powershell-v2.0

我收到了在我的每个系统上运行此请求的请求,该系统提取已安装应用程序的列表并将其输出到文本文件中。然后我必须将所有这些东西组合成更具可读性的东西,这需要一段时间。我正在学习Powershell,并希望从一个系统执行此操作,从文本文件中的服务器列表中提取并从一个位置对所有系统运行此查询:

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize > "$Env:userprofile\desktop\Installed Programs for $env:computername.txt"

我已经开始研究它,但我想我错过了让它工作的东西。我目前正在将其换成一个字符串然后输出到csv(我愿意接受建议)。这是我到目前为止所做的。

# Computer running this script
$WhoAmI = $env:ComputerName

$ServerList = get-content -path "C:\scripts\ServerList.txt"
$Path = "C:\scripts\results"

foreach ($server in $ServerList) 
    {   

        $InstalledApps = Invoke-Command -ComputerName $server {Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* }

            $Results += $InstalledApps |
                Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
                    Out-String  


    }
Write-Host $InstallApps

# $InstallApps | export-csv "$Path\InstalledFiles.csv"  

我目前正在尝试让它写入屏幕来测试循环的功能。我只得到一个空白的回复。

2 个答案:

答案 0 :(得分:0)

我有点想通了。在新的一天休息的眼睛。我写的内容有些错误等等。如果有人有任何贡献,我愿意接受改进。

编辑:我主要使用以下内容工作,但输出很乱。接受建议。

# Computer running this script

$ServerList = get-content -path "C:\scripts\ServerList.txt"
$Path = "C:\scripts\results"

foreach ($server in $ServerList) 
    {   

    $Results += "Results for $server"

        $InstalledApps = Invoke-Command -ComputerName $server -ScriptBlock {Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* }

            $Results += $InstalledApps |
                Select DisplayName, DisplayVersion, Publisher, InstallDate |
                    Out-String  


    }

# Write-Host $Results

$Results | out-file -filepath "$Path\InstalledPrograms.txt" -width 200

答案 1 :(得分:0)

您没有获得输出,因为您使用了(未定义的)变量$InstallApps而不是变量$results

话虽如此,我不建议在循环中进行字符串连接。这样的事情会更优雅:

Get-Content -Path 'C:\scripts\ServerList.txt' | ForEach-Object {
    $server = $_
    Invoke-Command -ComputerName $server -ScriptBlock {
        Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
    } | Select-Object @{n='Server';e={$server}}, DisplayName, DisplayVersion,
                      Publisher, InstallDate
} | Export-Csv 'C:\scripts\results\InstalledPrograms.csv' -NoType