Posted On December 4, 2019

PowerShell: Get Executable Version and File Location

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Get Executable Version and File Location
# Quick method to obtain computernames of all nodes in a cluster and adjoin result with standalone machine names
$standaloneFileServers="komputer1","komputer2"
$sourceClusters="cluster1","cluster2"
$nodes=$sourceClusters|%{get-clusternode -cluster $_}
$computerNames=$nodes.Name+$standaloneFileServers

# function to obtain the version of a particular excecutable
function getExecutableVersion{
param(
[string]$computername,
[string]$executablename="robocopy.exe"
)
$localComputerName=$env:computername
$isLocal=if($computername -match "(localhost|127.0.0.1|$localComputerName)"){$true}else{$false}

function getExeInfo{
param($exeName)
$exeFile=$(try{get-command $exeName -ea SilentlyContinue}catch{}).Definition;
$exeInfo=$(try{get-item $exeFile -ea SilentlyContinue}catch{}).versionInfo;
$osName=(Get-WmiObject -class Win32_OperatingSystem).Caption;
$osArchitecture=$ENV:PROCESSOR_ARCHITECTURE
$exeInfo | Add-Member -MemberType NoteProperty -Name "osName" -Value $osName
$exeInfo | Add-Member -MemberType NoteProperty -Name "osArchitecture" -Value $osArchitecture
return $exeInfo;
}

if ($isLocal){
$exeInfo=getExeInfo -exeName $executablename;
}else{
$exeInfo=invoke-command -computername $computername -scriptblock{
param($importedFunc,$executablename)
[ScriptBlock]::Create($importedFunc).Invoke($executablename);
} -args ${function:getExeInfo},$executablename
}
if ($exeInfo){
$exeVersion=$exeInfo.ProductVersion;
$exeLocation=$exeInfo.FileName;
$fileVersion=$exeInfo.FileVersion;
$osName=$exeInfo.osName;
$osArchitecture=$exeInfo.osArchitecture;
return "$computername`: $osName $osArchitecture $executablename $exeVersion$(if($exeVersion -ne $fileVersion){" $fileVersion "})$exeLocation";
}else{
write-host "$executablename is not in the environmental paths of $computername";
}
}


$computerNames|getExecutableVersion -computerName $_ -executablename robocopy.exe

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Add Root Domain to Trusted Sites

Windows Servers often have 'hard admin' modes, where accesses to external websites are painstakingly restricted.…

WinRM Management Consideration

Infrastructure needs to be able to leverage scripting automation to perform Vulnerability Remediation, Resource Provisioning,…

PowerShell: Set ACL

# setAcl-v0.01.ps1# # What this script does:# 1. Set permissions on a set of "destination…