Link to home
Start Free TrialLog in
Avatar of Georges Orwell
Georges OrwellFlag for France

asked on

Debug script powershell wmi

Hello all,

I  have the script below that dont work on all of my domainmembers servers (2008 => 2012R2) with this error:
Can you help me ?


Method invocation failed because [System.Object[] doesn't contain a method named 'Install'.
At C:\scripts\datadogSetup.ps1:40 char:116
+       (Get-WmiObject -ComputerName $srv.host -Credential $Creds -Class Win32_Product -EnableAllPrivileges ).Install <
<<< ("C:\TEMP\ddagent-cli.msi")
    + CategoryInfo          : InvalidOperation: (Install:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

You cannot call a method on a null-valued expression.
At C:\scripts\datadogSetup.ps1:43 char:69
+         $_.Name -match "New Relic Infrastructure Agent" }).Uninstall <<<< ()
    + CategoryInfo          : InvalidOperation: (Uninstall:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


the script:

Import-Module ActiveDirectory

$srcbin = "c:\temp\ddagent-cli.msi"


$Creds = Get-Credential MyDomain\otherusername
# csv format host;ddhost;ddtags
$servers = Import-Csv .\servers.csv -Delimiter ";" 

ForEach ($srv in $servers) {

write-host $srv.host ": datadog"

         
       $remoteDir= "\\"+$srv.host+"\c$\TEMP\"
      
           
       Copy-Item -Path $srcbin -Destination $remoteDir -Force

      (Get-WmiObject -ComputerName $srv.host -Credential $Creds -Class Win32_Product -EnableAllPrivileges ).Install("C:\TEMP\ddagent-cli.msi")

      (Get-WmiObject -ComputerName $srv.host -Credential $Creds -Class Win32_Product -EnableAllPrivileges | Where-Object { 
        $_.Name -match "New Relic Infrastructure Agent" }).Uninstall()


}

Open in new window



Thanks for your help
Avatar of oBdA
oBdA

The first issue here is that there is more than one product installed, so the query returns an array, and this array of WMI objects really doesn't have a method "Install".
The second issue happens if "New Relic Infrastructure Agent" isn't installed.
The following is utterly untested, so don't try it on a production server, and see below as well.
It assumes that the "New Relic Infrastructure Agent" isn't installed as part of ddagent-cli.msi, otherwise you'll have to run the full query again before filtering for this one.
	$Products = @(Get-WmiObject -ComputerName $srv.host -Credential $Creds -Class Win32_Product -EnableAllPrivileges)
	$Products[0].Install("C:\TEMP\ddagent-cli.msi")
	If ($nria = $Products | Where-Object {$_.Name -eq "New Relic Infrastructure Agent"}) {
		$nria.Uninstall()
	}

Open in new window


That said, before you go further down the road with Win32_Product, you should read these:

Win32_Product Is Evil.
https://gregramsey.net/2012/02/20/win32_product-is-evil/

Why Win32_Product is Bad News!
https://sdmsoftware.com/group-policy-blog/wmi/why-win32_product-is-bad-news/

Event log message indicates that the Windows Installer reconfigured all installed applications
https://support.microsoft.com/en-us/help/974524/event-log-message-indicates-that-the-windows-installer-reconfigured-all-installed-applications

Remote Powershell (http://www.google.com/search?q=remote+powershell) would be the better option (or psexec, https://technet.microsoft.com/en-us/sysinternals/bb897553.aspx, though this doesn't always work for msi installations).
	Invoke-Command –ComputerName $srv.host -Credential $Creds –ScriptBlock {& msiexec.exe /i "C:\TEMP\ddagent-cli.msi" /qn REBOOT=ReallySuppress /liewa "C:\Temp\ddagent-msi.log"}

Open in new window

Avatar of Georges Orwell

ASKER

Ok thanks oBdA.

Did you know how can  I pass variable from csv file into {} scriptblock ?
When  I try to pass variable as arguments  in scriptblock  all of these are blank.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial