Below is powershell script.
$Requests = Get-WmiObject -Class SMS_UserApplicationRequest -Namespace root/SMS/site_$($SiteCode) -ComputerName $SiteServer | Select-Object User,Application,CurrentState,Comments | Sort-Object User
$Count = @($Requests).Count
for ($i=0; $i -lt $count; $i++) {
if ($Requests[$i].CurrentState -eq '1') {
$Requests[$i].CurrentState = "Pending"
$checkbox1.Enabled = $true
}
when I execute the script I am getting following error.
Unable to index into an object of type System.Management.Automation.PSObject.
if ($Requests[ <<<< $i].CurrentState -eq '1') {
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex
What I want to do is I am replacing value (1) to Pending.
If Get-WmiObject
returns just a single instance, $Requests
will be a single object, not a collection. Enclose the Get-WmiObject
call in the array subexpression operator (@()
) to have it return a single-item array instead:
$Requests = @(Get-WmiObject -Class SMS_UserApplicationRequest -Namespace root/SMS/site_$($SiteCode) -ComputerName $SiteServer | Select-Object User,Application,CurrentState,Comments | Sort-Object User)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With