如何从Powershell哈希表输出到输出文件

时间:2017-03-01 21:49:55

标签: powershell scripting output hashtable

我正试图从所有Windows服务器获取.NetFramwork版本。我正在使用powershell脚本。我可以显示输出,但无法将哈希表的输出输出到输出文件。我怎么能摆脱" ..."来自VersionDetails:{1.0.3705,1.1.4322,2.0.50727,3.0 ...}并显示完整内容。

非常感谢任何帮助

这是我正在使用的代码:

$username = "username"
$password = "Password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$query = "select name from win32_directory where name like 'c:\\windows\\microsoft.net\\framework\\v%'"

$ComputerNames = Get-Content "d:\Scripts\serverList.txt"

foreach ($ComputerName in $ComputerNames)
   {
write-host "ComputerName = $ComputerName"
$ComputerName | ForEach-Object {
$res = Get-WmiObject -query $query -Credential $cred -ComputerName $ComputerName | ForEach-Object {
Split-Path $_.name -Leaf } | # returns directories
    Where-Object { $_ -like 'v*' } | # only include those that start with v
        ForEach-Object { [system.version]( $_ -replace "^v" ) }     
# remove "v" from the string and convert to version object

# Create hashtable with computername and version details
$prop = @{
    ComputerName = $ComputerName
    #V1_Present = &{ if ( $res | Where-Object { $_.Major -eq 1 -and $_.Minor -eq 0 } ) { $true } }
    #V1_1Present = &{ if ( $res | Where-Object { $_.Major -eq 1 -and $_.Minor -eq 1 } ) { $true } }
    V2_Present = &{ if ( $res | Where-Object { $_.Major -eq 2 -and $_.Minor -eq 0 } ) { $true } }
        V3_Present = &{ if ( $res | Where-Object { $_.Major -eq 3 -and $_.Minor -eq 0 } ) { $true } }
    V3_5Present = &{ if ( $res | Where-Object { $_.Major -eq 3 -and $_.Minor -eq 5 } ) { $true } }
    V4_Present = &{ if ( $res | Where-Object { $_.Major -eq 4 -and $_.Minor -eq 0 } ) { $true } }
    VersionDetails  = $res
}
# Create and output PSobject using hashtable
New-Object PSObject -Property $prop
}

=========================================================
Output dispalys
PS D:\Scripts> .\GetDotNetFrameworkver.ps1
in for loop ComputerName = XXXXXXX
V4_Present     : True
V3_5Present    : True
V2_Present     : True
V3_Present     : True
ComputerName   : XXXXX
VersionDetails : {1.0.3705, 1.1.4322, 2.0.50727, 3.0...}

1 个答案:

答案 0 :(得分:0)

根据link的答案,有一个“更简单”(更快)的解决方案来获取版本。

resize

如果要获取不同远程计算机的版本,可以使用PowerShell远程处理。请注意,您必须启用PS远程处理。如果您的操作系统版本为WIN10 / WIN2012R2,则默认启用它。如果您使用的是较旧的操作系统,则需要在远程计算机上调用[trialNumberPositions, trialNumberEvenOrOdd] = BalanceTrials(ntrials, 1, num_pos, evenodd); 。有关详细信息,请参阅此link

示例:

Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse | Get-ItemProperty -name Version,Release -ErrorAction Ignore | Where { $_.PSChildName -match '^(?!S)\p{L}'} | Select PSChildName, Version, Release

希望有所帮助。