Georges Orwell
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 ?
the script:
Thanks for your help
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()
}
Thanks for your help
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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).
Open in new window