I am working with SCOM (System Center Operations Manager).
There is the posibility to query SCOM with powershell.
I tried it and its working good, but I need your help.
I use the following command:
Get-SCOMClass -name Microsoft.Windows.Client.Win10.LogicalDisk |
Get-SCOMClassInstance |
Select-Object *
Here is the output of this:
If I want only the object HealthState thats no problem. Just use this command:
Get-SCOMClass -name Microsoft.Windows.Client.Win10.LogicalDisk |
Get-SCOMClassInstance |
Select-Object HealthState
But how can I get the output of the first entry (FreeSpace)?
[Microsoft.Windows.Client.Win10.Aggregate.LogicalDisk].FreeSpace
I tried several things as using the SCOMClass "Microsoft.Windows.Client.Win10.Aggregate.LogicalDisk" directly, but its the same output as for "Microsoft.Windows.Client.Win10.LogicalDisk".
This should work:
Get-SCOMClass -name Microsoft.Windows.Client.Win10.LogicalDisk | Get-SCOMClassInstance | Select-Object #{Name="Computer";Expression={$_.'[Microsoft.Windows.Computer].PrincipalName'.value}}, #{Name="Logical Disk";Expression={$_.DisplayName}}, #{Name="Free Size in GB";Expression={[math]::round($_.'[Microsoft.Windows.Client.Win10.Aggregate.LogicalDisk].FreeSpace'.value/1GB, 2)}}
Try this:
Get-SCOMClass -name Microsoft.Windows.Client.Win10.LogicalDisk | Get-SCOMClassInstance | Where-Object {$_.HealthState -like "*FreeSpace"}
Related
When I execute this powershell command to get a list of running COM objects that match the prefix "Python", I get the following output:
PS C:\Users\{path-to-arbitrary-directory}> Get-ChildItem HKLM:\Software\Classes | Where-Object {
$_.PSChildName -match '^Python[\.a-zA-Z]*$' } | Select-Object
Hive: HKEY_LOCAL_MACHINE\Software\Classes
Name Property
---- --------
Python (default) : Python ActiveX Scripting Engine
Python.Dictionary (default) : Python Dictionary
Python.Interpreter (default) : Python Interpreter
Python.TestServer (default) : Python Test COM Server
What I would like to do is just get a list of the Name and Description.
Currently I am able to get the names with this command:
PS C:\Users\{path-to-arbitrary-directory}> Get-ChildItem HKLM:\Software\Classes | Where-Object {
$_.PSChildName -match '^Python[\.a-zA-Z]*$' } | Select-Object PSChildName,Property
PSChildName Property
----------- --------
Python {(default)}
Python.Dictionary {(default)}
Python.Interpreter {(default)}
Python.TestServer {(default)}
But I can't for the life of me figure out how to show the Descriptions that I see when I execute the 1st command?
This is the output I would want:
Name Description
---- --------
Python Python ActiveX Scripting Engine
Python.Dictionary Python Dictionary
Python.Interpreter Python Interpreter
Python.TestServer Python Test COM Server
(if it helps anyone, I am also able to view the description with this command)
PS C:\Users\{path-to-arbitrary-directory}> Get-ChildItem HKLM:\Software\Classes | Where-Object {
$_.PSChildName -match '^Python[\.a-zA-Z]*$' } | Get-ItemProperty
(default) : Python ActiveX Scripting Engine
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Classes\Python
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Classes
PSChildName : Python
PSProvider : Microsoft.PowerShell.Core\Registry
...
One way to do this is the use of calculated properties. You can use Get-ItemProperty to get the value of the (default) registry key property. Then you can show this value as a calculated property.
Get-ChildItem HKLM:\software\Classes\ |
Where-Object {$_.PSChildName -match 'document'} |
Select-Object PSChildName, #{Name = "Default"; Expression = {($_ | Get-ItemProperty)."(default)"}}
Try this out. Things like this are better in functions.
function Get-COMDescription {
Param(
[parameter(Mandatory=$true)][string]$Search
)
Get-ChildItem HKLM:\Software\Classes | Where-Object {
# Match naming convention for COM Object ensure they key has a CLSID folder.
$_.PSChildName -match "^$Search\.\w+$" -and (Test-Path -Path "$($_.PSPath)\CLSID") } |
Select-Object PSChildName,#{l="Description";e={$_ | Get-ItemProperty | select -ExpandProperty "(default)" }}
}
Usage example:
PS C:\> Get-COMDescription -Search GoogleUpdate
PSChildName Description
----------- -----------
GoogleUpdate.CoCreateAsync CoCreateAsync
GoogleUpdate.CoreClass Google Update Core Class
GoogleUpdate.CoreMachineClass Google Update Core Class
GoogleUpdate.CredentialDialogMachine GoogleUpdate CredentialDialog
GoogleUpdate.OnDemandCOMClassMachine Google Update Broker Class Factory
GoogleUpdate.OnDemandCOMClassMachineFallback Google Update Legacy On Demand
GoogleUpdate.OnDemandCOMClassSvc Google Update Legacy On Demand
GoogleUpdate.PolicyStatus Google Update Policy Status Class
GoogleUpdate.ProcessLauncher Google Update Process Launcher Class
GoogleUpdate.Update3COMClassService Update3COMClass
GoogleUpdate.Update3WebMachine Google Update Broker Class Factory
GoogleUpdate.Update3WebMachineFallback GoogleUpdate Update3Web
GoogleUpdate.Update3WebSvc GoogleUpdate Update3Web
The existing answers are helpful, but let me add some background information:
The reason that the default output shows the target keys' values is that the default output formatting enumerates them, as this command reveals:
(Get-FormatData Microsoft.Win32.RegistryKey -PowerShellVersion $PSVersionTable.PSVersion).FormatViewDefinition.Control.Rows.Columns.DisplayEntry.Value
This shows:
PSChildName # column 1 - below is the script block that defines column 2
$result = (Get-ItemProperty -LiteralPath $_.PSPath |
Select * -Exclude PSPath,PSParentPath,PSChildName,PSDrive,PsProvider |
Format-List | Out-String | Sort).Trim()
$result = $result.Substring(0, [Math]::Min($result.Length, 5000) )
if($result.Length -eq 5000) { $result += "..." }
$result
As you can see, Get-ItemProperty is called behind the scenes to enumerate a key's values.
As an aside: This method of enumerating values as part of the formatting leads to incorrect output when retrieving values from a remote registry - see this answer.
While calling Get-ItemProperty in the script block of a calculated property, as shown in the other answers, definitely works, there is a more efficient alternative: You can call the .GetValue() method of the Microsoft.Win32.RegistryKey instances that Get-Item outputs:
Get-ChildItem HKLM:\Software\Classes |
Where-Object PSChildName -match '^Python[\.a-z]*$' |
Select-Object #{ n='Name'; e='PSChildName' },
#{ n='(default)'; e={ $_.GetValue('') } }
Code:
$adgroups = Get-ADPrincipalGroupMembership $tag$ | select -ExpandProperty Name | Sort | Select-String "iSite"
Output:
DFSR Managed iSite Enterprise 4.4.542.2 WSA_Rad_A
DFSR Managed iSite Radiology 4.4.516.27 WSA_Rad_A
Basically one command generates two items (output using $variable | out-file C:\file.txt -Append) and when I go to open these in excel they format as one line like this:
DFSR Managed iSite Enterprise 4.4.542.2 WSA_Rad_A DFSR Managed iSite Radiology 4.4.516.27 WSA_Rad_A
Is there a way to break it up // add a new line after each item but still keep them both inside one variable?
Get-ADPrincipalGroupMembership $tag$ | select -ExpandProperty Name | Sort | Select-String "iSite" | ConvertTo-Csv -NoTypeInformation | Out-File C~\Desktop\Sites.csv
I would break up your request a bit.
First, you're using Select -Expand, which is going to discard all of the properties and only return the values for each object's Name. This is a problem because when you export it as a CSV, you're not going to have a heading. I think the lack of header is ultimately leading to the issue you're facing here.
Try this instead:
$adgroups = Get-ADPrincipalGroupMembership $tag$ | Where Name -like "*iSite*" |
select Name | Export-Csv c:\pathto\YourCsv.Csv
Finally, I don't think Select-String is doing you any favors. You could instead use the -like operator.
I am trying to use PowerShell to query a group calendar and return only a subset of the events based on a specific string in the Subject field.
Currently, I can use the following and get a listing of all of the events:
$events = Invoke-RestMethod -Uri "https://outlook.office365.com/api/v1.0/users/$calendar/calendarview?startDateTime=$(Get-Date)&endDateTime=$((Get-Date).AddDays(1))" -Credential $cred | foreach-object{$_.Value}
$events | Select-Object -Property Subject,Start,End | fl
This is where I get stuck. I am trying to filter these results to where I only return results where Subject -like '*string*'
However, I just cannot seem to get that to work on the Invoke-RestMethod line...
Any help would be GREATLY appreaciated.
Bonus appreciation to anyone who can take the results of the Start and Stop times from this:
2016-04-25T13:00:00Z
to this:
4/25/2016
For reference, I have already tried this:
Get-Date $events.Start -Format 'MM/dd/yyyy'
Which gives this error:
Get-Date : Cannot convert 'System.Object[]' to the type
'System.DateTime' required by parameter 'Date'. Specified method is
not supported.
Because your using a CalendarView (which is a filter of sorts) you can't apply another filter at the REST level so just filter the results eg
$events | Where-Object {$_.Subject -match 'string'} | Select-Object -Property Subject,Start,End | fl
or if you want to use wildcards
$events | Where-Object {$_.Subject -Like '*string*'} | Select-Object -Property Subject,Start,End | fl
With the Start Stop time just CAST them eg
$events | % {([DateTime]$_.Start).ToString("MM/dd/yyyy")}
Cheers
Glen
How can I extract one value from the first part of a command chain in my output? For example:
Get-ActiveSyncDevice | Get-ActiveSyncDeviceStatistics | ft UserDeviceAgent,Identity -a
As a fictional example, assume the column UserDeviceAgent is visible in Get-ActiveSyncDevice, however I can't get it to appear in the final output
Please note:
My question is specifically: How do I "back reference" an output in the command chain?
You need to pass it down the pipe, so in your script you would have to convert the second portion of the pipe to a ForEach, and then output both the desired property, and the results of your second command.
So to take your hypothetical let's say this:
Get-ActiveSyncDevice | %{
$CurDev = Get-ActiveSyncDeviceStatistics
add-member -InputObject $CurDev -MemberType NoteProperty -Name UserDeviceAgent -Value $_.UserDeviceAgent
$CurDev
}| ft UserDeviceAgent, Identity -a
You could try -OutVariable on the first CmdLet and then run an expression in Format-Table at the end:
Get-ActiveSyncDevice -OutVariable ASD | `
Get-ActiveSyncDeviceStatistics | `
Format-Table Identity, #{Name="UserDeviceAgent"; Expression ={$ASD.Item($ASD.Count-1).UserDeviceAgent}}
Why does the following command return more attributes than in the Select?
$obj = Get-VM | Select VMName,State; $obj
Output:
VMName : blah-blah
State : Off
PSComputerName : host
RunspaceId : URI
And how do I just get the value for State?
$obj = Get-VM | Select VMName,State; $obj.State
The above should do the trick but doesn't :(
That should work, but it sounds like for some reason the assignment of the results of Get-VM to $obj is happening before anything gets piped to select. Try Get-VM | select VMName, State by itself at the prompt, and if you get the expected results, try this:
$obj = (Get-VM | select VMName, State); $obj
To get just the State property, you can do this:
(Get-VM).State
Problem found :)
The above command is just fine as it is, however when it get's wrapped in an Invoke-Command such that it's run against another machine then the results are modified to include run specific information and a 'follow-on Select' is required like this:
Invoke-Command -Comuptername XXXX -ScriptBlock {$obj = Get-VM | Select VMName,State; $obj} | Select State