【问题标题】:How to output from a Powershell hashtable to a output file如何从 Powershell 哈希表输出到输出文件
【发布时间】:2017-03-01 21:49:55
【问题描述】:

我正在尝试从所有 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...}

【问题讨论】:

  • 试试这个:VersionDetails = $res -Join ", "
  • 您指定凭据是否有原因?您没有远程计算机的权限吗?您可以使用这些凭据运行 PowerShell 吗?如果不必使用替代凭据连接到远程系统,我可能可以提供更好的解决方案。
  • 我没有尝试过这个选项。我当然可以访问这些服务器。
  • 感谢您的所有帮助。我仍在尝试将结果输出到文件。我怎样才能从哈希表中做到这一点。
  • 终于可以输出到文件$prop |输出文件 C:\Scripts\frameworkvertion.txt

标签: powershell scripting output hashtable


【解决方案1】:

根据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

如果您想获取不同远程计算机的版本,您可以使用 PowerShell 远程处理。请注意,您必须启用 PS 远程处理。如果您的操作系统版本是 WIN10/WIN2012R2,则默认启用它。如果您使用的是较旧的操作系统,您必须在远程计算机上调用Enable-PSRemoting。有关详细信息,请参阅此link

例子:

$result = Invoke-Command -ComputerName computer1.domain, computer1.domain -Credential (Get-Credential ) -ScriptBlock {
    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
}

$hash = $result | group PSComputerName -AsHashTable # Group the .Net versions by computername

$hash.'computer1.domain'            # Print all  .Net version of computer1
$hash.'computer1.domain'.Version    # Only print the version

希望对您有所帮助。

【讨论】:

  • 我们的大多数服务器是 Win 2008,少数是 2012
  • 然后你必须在你的远程机器上调用Enable-PSRemoting
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2014-01-25
  • 2011-07-26
  • 2022-01-24
相关资源
最近更新 更多