2

I have searched here and elsewhere and could not find an answer, so here is my question.

What is the best way to determine PowerShell version on a remote computer using WMI?

Background

My task is to audit and update some 1000 servers to recent version of PowerShell. Some of them have PowerShell v1 and some do not have WinRM configured, so Invoke-command is not an option. PSExec is also not an option in this environment. This is why I need to use WMI for this task.

Any help would be appreciated.

EDIT:

After much research I'm still leaning towards WMI. In particular retrieving the file version of "powershell.exe". This seems to me to be the only way to cover all versions.

Code I have so far is here:

$path = "C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe" 
$query = "SELECT Version FROM CIM_DataFile WHERE Name = '$path'" 
$PSFileVer = Get-WmiObject -Query $query -ComputerName $servername -Credential $creds 
$BuildVer = [version]$PSFileVer.Version.Split()[0]

All I need now is a comprehensive list, mapping file version (build number) to powershell version. Any Ideas?

Jan Chrbolka
  • 4,184
  • 2
  • 29
  • 38
  • How about using a management tool like System Center Configuration Manager? Managing more than a few dozen servers without good tools is waste of time. – vonPryz Apr 16 '15 at 07:37
  • SCCM is an option, but only covers about 80% of the servers. Another reason why I'm looking for a PowerShell/WMI solution. – Jan Chrbolka Apr 16 '15 at 08:12
  • 1
    How about [registry keys](http://stackoverflow.com/a/17142839/503046), then? – vonPryz Apr 16 '15 at 10:07
  • @vonPryz Thanks. *"Microsoft's recommended forward compatible method"* unfortunately breaks down with version 3 of PowerShell. It's still a viable option if you stick with version 1 and 2. – Jan Chrbolka Apr 16 '15 at 22:51

2 Answers2

2

The solution from here is

$command = "ipconfig/all > C:\temp\result.txt"
$cmd = "cmd /c $command"

Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $cmd -ComputerName "remotepc"
sleep 1
Get-Content \\remotepc\C$\temp\result.txt

You can also do similar via this method. If you do so, you can't get the return value directly, u must redirect to a file.

You could setup share for the results instead of putting result on local machine

Community
  • 1
  • 1
majkinetor
  • 8,730
  • 9
  • 54
  • 72
  • Thanks @majkinetor. That is definitely a plausible way to execute code on remote PC via WMI. Although I really don't like using .tmp files to get results, I might end up using this if I don't come up with a better way. – Jan Chrbolka Apr 16 '15 at 22:43
1

If it helps here is the command :

$command = "powershell -command "+'"$PSVersionTable.PsVersion.Major | out-file c:\temp\version.txt"'
paul543
  • 178
  • 4
  • 16
demisco
  • 11
  • 2