0

I am trying to get the .NetFramwork version from all the windows servers. I am using powershell script. I can get the output displayed but unable to get the output from the hashtable to a output file. Also how would I get rid of the "..." from VersionDetails : {1.0.3705, 1.1.4322, 2.0.50727, 3.0...} and show the full content.

Any help will be greatly appreciated

here is the code I am using:

$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...}
IceMan
  • 1
  • 3
  • Try this: `VersionDetails = $res -Join ", "` – BenH Mar 01 '17 at 22:21
  • Is there a reason that you are specifying credentials? Do you not have rights to the remote computers? Can you just run PowerShell with those credentials? I can probably provide a better solution if don't have to connect to the remote system with alternative credentials. – TheMadTechnician Mar 01 '17 at 23:44
  • i have not tried that option. I certainly do have access to those servers. – IceMan Mar 02 '17 at 14:34
  • Thanks for all your help. i am still trying to output the results to file. how can i do that from the hashtable. – IceMan Mar 02 '17 at 14:36
  • 1
    Finally able to output to the file $prop | Out-File C:\Scripts\frameworkvertion.txt – IceMan Mar 02 '17 at 15:04

1 Answers1

0

Based on the answer of link there is a "simpler" (and faster) solution to fetch the versions.

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

If you want to get the versions of different remote machines you can use PowerShell remoting. Be aware that you've to enable PS remoting .If your OS version is WIN10/WIN2012R2 it is enabled per default. If you're using an older OS you've to call Enable-PSRemoting on the remote machine. See this link for details.

Example:

$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

Hope that helps.

Community
  • 1
  • 1
Moerwald
  • 10,448
  • 9
  • 43
  • 83