I’m trying to convert some PowerShell code from using WMI to using CIM. I had a function which used Win32ShutdownTracker (https://msdn.microsoft.com/en-us/windows/desktop/aa394057) to reboot a system, with a timeout, comment, and reason for initiating the reboot. Using WMI I had to give the account running the reboot permissions using “psbase.Scope.Options.EnablePrivileges = $true” even though the account was already an administrator on the local system. I have not figured out how to give the same permissions using CIM and it seems without them, I’m unable to reboot the system. Also, the class binding does not seem to work. Any help would be greatly appreciated.
I've tried many combinations of CIM, including getting the object then invoking it and calling the class directly and none have worked.
WMI Code (Works without issue):
$OSObject = Get-WmiObject -Class Win32_OperatingSystem
$OSObject.psbase.Scope.Options.EnablePrivileges = $true
$OSObject.Win32ShutdownTracker(300,"This is a test",2147745794,6)
CIM Code I've tried (Does not work):
$OSObject = Get-CimInstance -Class Win32_OperatingSystem
$OSObject.psbase.Scope.Options.EnablePrivileges = $true
The property 'EnablePrivileges' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:1
+ $OSObject.psbase.Scope.Options.EnablePrivileges = $true
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Invoke-CimMethod -CimClass $OSObject -MethodName Win32ShutdownTracker –Arguments 300,"This is a test",21477
45794,6
Invoke-CimMethod : Cannot bind parameter 'CimClass'. Cannot convert the "Win32_OperatingSystem: Microsoft Windows
10 Enterprise" value of type "Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_OperatingSystem"
to type "Microsoft.Management.Infrastructure.CimClass".
At line:1 char:28
+ Invoke-CimMethod -CimClass $OSObject -MethodName Win32ShutdownTracker ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-CimMethod], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok
eCimMethodCommand
Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName Win32ShutdownTracker –Arguments #{300,"This is a test",2147745794,6}
Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName Win32ShutdownTracker –Arguments #{300,"This
is a test",2147745794,6}
Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName "Win32ShutdownTracker" –Arguments 300,"This
is a test",2147745794,6
Invoke-CimMethod : Cannot bind parameter 'CimClass'. Cannot convert the "Win32_OperatingSystem" value of type
"System.String" to type "Microsoft.Management.Infrastructure.CimClass".
At line:1 char:28
+ Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName "Win32 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-CimMethod], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok
eCimMethodCommand
Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName Win32ShutdownTracker –Arguments 300,"This is
a test",2147745794,6
Invoke-CimMethod : Cannot bind parameter 'CimClass'. Cannot convert the "Win32_OperatingSystem" value of type
"System.String" to type "Microsoft.Management.Infrastructure.CimClass".
At line:1 char:28
+ Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName Win32S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-CimMethod], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok
eCimMethodCommand
(Invoke-CimMethod -CimClass 'Win32_OperatingSystem').Win32ShutdownTracker(300,"This is a test",2147745794,6
)
Invoke-CimMethod : Cannot bind parameter 'CimClass'. Cannot convert the "Win32_OperatingSystem" value of type
"System.String" to type "Microsoft.Management.Infrastructure.CimClass".
At line:1 char:29
+ (Invoke-CimMethod -CimClass 'Win32_OperatingSystem').Win32ShutdownTra ...
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-CimMethod], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok
eCimMethodCommand
(Invoke-CimMethod -CimClass $OSObject).Win32ShutdownTracker(300,"This is a test",2147745794,6)
Invoke-CimMethod : Cannot bind parameter 'CimClass'. Cannot convert the "Win32_OperatingSystem: Microsoft Windows
10 Enterprise" value of type "Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_OperatingSystem"
to type "Microsoft.Management.Infrastructure.CimClass".
At line:1 char:29
+ (Invoke-CimMethod -CimClass $OSObject).Win32ShutdownTracker(300,"This ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-CimMethod], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok
eCimMethodCommand
This works for me:
$arguments = #{
Timeout = [System.UInt32]300
Comment = 'This is a test'
ReasonCode = [System.UInt32]2147745794
Flags = 6
}
Invoke-CimMethod -Query 'SELECT * FROM Win32_OperatingSystem' -MethodName 'Win32ShutdownTracker' –Arguments $arguments
Related
I need to modify the below script to execute the Windows Update so it downloads and updates the remote server:
Invoke-RemoteExecution($computername){
$cred = get-credential
invoke-command -computername $computername -Credential $cred -scriptblock {
Function Search-Updates
{
$Criteria = "IsInstalled=0 and Type='Software'"
#Search for relevant updates.
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$SearchResult = $Searcher.Search($Criteria).Updates
return [System.MarshalByRefObject]$SearchResult
}
Function Download-Updates
{
Param ($SearchResult)
$Session = New-Object -ComObject Microsoft.Update.Session
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $SearchResult
$Downloader.Download()
}
[System.MarshalByRefObject]$SearchResult = Search-Updates
Download-Updates -SearchResult $SearchResult
}
}
Invoke-RemoteExecution -computername yourcomputername
The error I am facing is like the below:
Invoke-RemoteExecution -computername PRD-SVR01-VM
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
+ CategoryInfo : OperationStopped: (:) [], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException
+ PSComputerName : PRD-SVR01-VM
The property 'Updates' cannot be found on this object. Verify that the property exists and can be set.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
+ PSComputerName : PRD-SVR01-VM
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : PRD-SVR01-VM
I have tried to use the remoting but it is still failed as above.
I'm facing an issue of getting all vms configuration ( using Get-ScVirtualMachine command) into an array from an input file.
The code is this one below
$VmsList = Get-Content C:\VmsList.txt
foreach($vm in $VmsList){
$Result += Get-SCVirtualMachine -Name $vm
}
And I have this error
Method invocation failed because [Microsoft.SystemCenter.VirtualMachineManager.VM] does not contain a method named 'op_Addition'.
At line:3 char:1
$Result += Get-SCVirtualMachine -Name $vm
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [Microsoft.SystemCenter.VirtualMachineManager.VM] does not contain a method named 'op_Addition'.
At line:3 char:1
$Result += Get-SCVirtualMachine -Name $vm
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Get-SCVirtualMachine : Cannot validate argument on parameter 'Name'. The character length (0) of the argument is too short. Specify an argument with a length that
is greater than or equal to "1", and then try the command again.
At line:3 char:39
$Result += Get-SCVirtualMachine -Name $vm
~~~
CategoryInfo : InvalidData: (:) [Get-SCVirtualMachine], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.GetVMCmdlet
I forgot to declare the array $result = #()
I resolved the issue.
Object reference not set to an instance of an object.
Error when running command Get-AzureRmRecoveryServicesAsrRecoveryPlan...
PS C:\WINDOWS\system32> $rpname = "myrecoveryplan"
$recoplan = Get-AzureRmRecoveryServicesAsrRecoveryPlan -Name $rpname
Get-AzureRmRecoveryServicesAsrRecoveryPlan : Operation failed.
Object reference not set to an instance of an object.
At line:2 char:13
+ $recoplan = Get-AzureRmRecoveryServicesAsrRecoveryPlan -Name $rpname
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzureRmReco...AsrRecoveryPlan], Exception
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.RecoveryServices.SiteRecovery.GetAzureRmRecoveryS
ervicesAsrRecoveryPlan
I have updated My powershell and it seems moving on. But I got another blocker. $Context= Set-AzureRmRecoveryServicesAsrVaultContext -Vault $Vault
$recoplan = Get-AzureRmRecoveryServicesAsrRecoveryPlan -Name $rpname
Start-AzureRmSiteRecoveryTestFailoverJob -RecoveryPlan $recoplan -Direction "PrimaryToRecovery" -VMNetwork "asrtestfailtarget"
Start-AzureRmSiteRecoveryTestFailoverJob : Cannot bind parameter 'RecoveryPlan'. Cannot convert the
"Microsoft.Azure.Commands.RecoveryServices.SiteRecovery.ASRRecoveryPlan" value of type
"Microsoft.Azure.Commands.RecoveryServices.SiteRecovery.ASRRecoveryPlan" to type
"Microsoft.Azure.Commands.SiteRecovery.ASRRecoveryPlan".
At line:5 char:56
+ ... rt-AzureRmSiteRecoveryTestFailoverJob -RecoveryPlan $recoplan -Direct ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-AzureRmSiteRecoveryTestFailoverJob], Parameter
BindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.SiteRecovery.Start
AzureRmSiteRecoveryTestFailoverJob
Trying PS command:
Set-FsrmFileGroup -Name "Anti-Ransomware File Groups" -IncludePattern #((Invoke-WebRequest -Uri "https://fsrm.experiant.ca/api/v1/combined" -UseBasicParsing).Content | ConvertFrom-Json | % {$_.filters})
Getting error:
New-FsrmFileGroup : Invalid namespace
At line:1 char:1
+ New-FsrmFileGroup -Name "Anti-Ransomware File Groups" -IncludePattern #((Invoke- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (MSFT_FSRMFileGroup:Root/Microsoft/...T_FSRMFileGroup) [New-FsrmFileGroup], CimException
+ FullyQualifiedErrorId : HRESULT 0x8004100e,New-FsrmFileGroup
I also notice some errors in FSRM:
Firewall is off and I am domain admin running this as admin. SFC and a WMI repair came back as good. I am going based off a guide (https://fsrm.experiant.ca/). This has worked across a ton of other servers so I don't believe the commands to be improperly formatted.
Output of Get-WmiObject -Namespace 'Root/Microsoft/Windows/Fsrm' -List:
Get-WmiObject : Could not get objects from namespace Root/Microsoft/Windows/Fsrm.
Invalid namespace
At line:1 char:1
+ Get-WmiObject -Namespace 'Root/Microsoft/Windows/Fsrm' -List
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : INVALID_NAMESPACE_IDENTIFIER,Microsoft.PowerShell.Commands.GetWmiObjectCommand
I have the following script, but am getting an error -
Script -
$CNAMES = Get-Content "C:\Temp\alias.txt"
$Query = "Select * from MicrosoftDNS_CNAMEType"
$Record = Get-WmiObject -Namespace "root\microsoftdns" -Query $Query -ComputerName 10.10.10.1 | Where-Object{$_.Ownername -match $CNAME}
Foreach($CNAME in $CNAMES)
{
$Record.RecordData = "some.server.net"
$Record.put()
}
Error -
Property 'RecordData' cannot be found on this object; make sure it exists and is settable.
At C:\temp\DNSUpdateCNAMETarget_02.ps1:7 char:9
+ $Record. <<<< RecordData = "some.server.net"
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Method invocation failed because [System.Object[]] doesn't contain a method named 'put'.
At C:\temp\DNSUpdateCNAMETarget_02.ps1:8 char:12
+ $Record.put <<<< ()
+ CategoryInfo : InvalidOperation: (put:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
TIA
I didn't try (cause i don't have a MS-DNS at hand right now) but i'd suspect that you need
$Record.PSBase.Put()
to make it work.
EDIT:
As it looks $Record holds an array of System.Object so will have to cast it a suitable type to access .RecordData and .Put()
Your script queries for records of type MicrossoftDNS_CNAMEType which only support CreateInstanceFromPropertyData.
I would try sending you $Record to Get-Member
Get-WmiObject -Namespace "root\microsoftdns" -Query $Query -ComputerName 10.10.10.1 | Get-Member
to find out, what you are dealing with.