PowerShell Tip : Error while setting up a password for Windows Services
This helps to fix issues while using Invoke-WMIMethod in your scripts
Scenario:
- Used WMI object to clear the security log - Worked as expected
- Used WMI object to set a password for a service - Failed
Failed Command
Get-WmiObject -Class Win32_Service -Filter "name='BITS'" | Invoke-WmiMethod -Name Change -ArgumentList $null,$null,$null,$null,$null,$null,$null,"Password123" |
Error Message:
Invoke-WmiMethod : Input string was not in a correct format.
At line:1 char:60
- Get-WmiObject -Class Win32_Service -Filter "name='BITS'" | Invoke-WmiMethod -Nam ...
-
+ CategoryInfo : NotSpecified: (:) [Invoke-WmiMethod], FormatException
+ FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.InvokeWmiMethod
Root Cause: Invoke-WMIMethod will not understand using bunch of $null
Alternate Method
Get-WmiObject -Class Win32_Service -Filter "name='BITS'" | ForEach-Object -Process {$_.Change($null,$null,$null,$null,$null,$null,$null,"Password123")} |
Output:
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
PSComputerName :