【发布时间】: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