I want to run this command wmic path Win32_Process get ParentProcessId, commandLine, creationdate, executablepath, name, processId with several parameters,but powershell swear at the syntax if I try to write comma-separated. What do I need to fix?
By default, the wmi/cim cmdlets give you all the properties of a class on the object you receive, so you do not need to specify each one:
$wmi = Get-WmiObject -Class Win32_Process
$wmi | Get-Member -MemberType Property
$props = 'ParentProcessId', 'CommandLine', 'CreationDate', 'ExecutablePath', 'Name', 'ProcessId'
$wmi | Select-Object -Property $props
As a best practice: if powershell gives you a native abstraction (in this case, Get-WmiObject or Get-CimInstance), you should use it!
Commas indicate arrays. If you really want to wmic, you can use the magical "stop parsing" operator:
wmic --% path Win32_Process get ParentProcessId, commandLine, creationdate, executablepath, name, processId
Get-wmiobject or get-ciminstance will output objects though, that are easier to manipulate. Get-ciminstance even has tab completion on the classnames, and piping to select-object or where-object, you get tab completion on the properties.
get-ciminstance win32_process | select parentprocessId, commandLine, creationdate, executablepath, name, processId
get-ciminstance win32_process | where commandline -match chrome
Related
I can get list the processes but how would I get them to show by highest usage instead of alphabetically?
Wmic path win32_performatteddata_perfproc_process get Name,PercentProcessorTime
From powershell you don't need to make direct calls to wmic, Get-CimInstance is meant to easily query all instances of WMI and CIM classes and output objects which are easy to manipulate. Sorting objects in PowerShell can be done with Sort-Object.
Get-CimInstance Win32_PerfFormattedData_PerfProc_Process |
Sort-Object PercentPrivilegedTime -Descending |
Select-Object Name, PercentProcessorTime
You could even go one step further and group the objects by their name with the help of Group-Object:
Get-CimInstance Win32_PerfFormattedData_PerfProc_Process |
Group-Object { $_.Name -replace '#\d+$' } | ForEach-Object {
[pscustomobject]#{
Instances = $_.Count
Name = $_.Name
PercentProcessorTime = [Linq.Enumerable]::Sum([int[]] $_.Group.PercentProcessorTime)
}
} | Sort-Object PercentProcessorTime -Descending
I'm trying to put together an expression that returns a bare value, without a table or name of the expression included.
For example, I have this line from another solution:
gwmi win32_logicaldisk -Filter "DeviceID = 'C:'" |
Format-Table #{n="Size";e={[math]::Round($_.Size/1GB,2)}}
This returns:
Size
----
475.33
How can I grab just the 475.33?
Use Select-Object instead of Format-Table:
$DiskC = gwmi win32_logicaldisk -Filter "DeviceID = 'C:'" | Select-Object #{n="Size";e={[math]::Round($_.Size/1GB,2)}}
$DiskC.Size
Format-* cmdlets return objects whose sole purpose is to provide formatting instructions to PowerShell's output-formatting system - see this answer. In short: only ever use Format-* cmdlets to format data for display, never for subsequent programmatic processing.
gwmi is the built-in alias for the Get-WmiObject cmdlet, which is obsolete.
The CIM cmdlets (e.g., Get-CimInstance) superseded the WMI cmdlets in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell [Core] v6+, where all future effort will go, doesn't even have them anymore. For more information, see this answer.
If you simply want to calculate a value derived from (each of) your input object(s), use the ForEach-Object cmdlet.
Therefore:
Get-CimInstance win32_logicaldisk -Filter "DeviceID = 'C:'" | ForEach-Object {
[math]::Round($_.Size / 1gb, 2)
}
Or, more simply, using an expression:
[math]::Round(
(Get-CimInstance win32_logicaldisk -Filter "DeviceID = 'C:'").Size / 1gb, 2
)
If you use your first line:
gwmi win32_logicaldisk -Filter "DeviceID = 'C:'" | Format-Table #{n="Size";e={[math]::Round($_.Size/1GB,2)}}
But Pipe it trough Select-object instead of format table then pipe it again to select object expanding the property (or just .size)
gwmi win32_logicaldisk -Filter "DeviceID = 'C:'" | Select #{n="Size";e={[math]::Round($_.Size/1GB,2)}} | select -ExpandProperty Size
I'm running a powershell script, that when run from the ISE outputs one set of values but when the same task is run through task scheduler it seems to add a second value that doesn't display when run manually. The code that's being executed is as below:
import-module WebAdministration
$app_pool_name = <<app_pool_name_goes_here>>
$memused = ""
$cpuused = ""
$datetime = get-date -format s
$memused = Get-WmiObject Win32_process | where CommandLine -Match "$app_pool_name"
$id = dir IIS:\AppPools\$app_pool_name\WorkerProcesses\ | Select-Object -expand processId
$cpuUsed = Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | where IDProcess -Match $id
Add-Content -path C:\Winsys\PSOutput\$app_pool_name-CPU-RAM_test.txt -value "$datetime,$($memUsed.workingsetsize),$($cpuUsed.PercentProcessorTime)"
When running the script manually the output returned is:
Date,Mem,CPU
2016-08-02T14:09:36,15062687744,0
2016-08-02T14:09:38,15062425600,0
When running the script through task scheduler the output returned is:
Date,Mem,CPU
2016-08-02T13:58:25,15065047040 624189440,0
2016-08-02T14:05:01,15061901312 624713728,0
The difference being the Mem, for some reason it's adding an extra value. Does anyone know why this is?
Turns out this was my own error, there are two app pools with very similar names, the -match was catching both. But it still didn't explain why it was only showing both in task scheduler and not ISE. Ah well, resolved now by adding a -and -notmatch "text" section.
E.g.
Get-WmiObject Win32_process | where {$_.CommandLine -Match "$app_pool_name" -and $_.CommandLine -notmatch "<<text in other command line>>"}
Add Comment
I am trying to let the script below:
Get all the installed software on a number of computers and Sort by computer name, and not list all the software all together.
This is my code -
param(
[string[]]$computername='testpc'
)
Get-WmiObject -computername $computername -Class Win32_Product | Select-Object -Property Name, description, installdate, vendor | Sort-Object Name > C:\desktop-apps.txt
You can add the PSComputerName property to your Select-Object and Sort-Object calls:
Get-WmiObject -computername $computername -Class Win32_Product |
Select-Object -Property PSComputerName, Name, description, installdate, vendor |
Sort-Object PSComputerName, Name
This is the script I have in Powershell;
$wmi = get-wmiobject -Namespace root\ccm -class sms_client -list | gm
The line above works exactly how I want.
The line below gives me completely different results because I removed the -list switch:
$wmi = get-wmiobject -Namespace root\ccm -class sms_client | gm
My question is how can I transpose the first command to VBScript. I want to be able to call the "TriggerSchedule" method.
Something like this should work (schedule ID taken from the documentation):
Set wmi = GetObject("winmgmts://./root/ccm")
scheduleID = "{00000000-0000-0000-0000-000000000001}"
For Each client In wmi.ExecQuery("SELECT * FROM SMS_Client")
client.TriggerSchedule(scheduleID)
Next
Untested, though, because I don't have SCCM at hand here.
Late to the party but try this in PowerShell:
GWMI CCM_Scheduler_ScheduledMessage -namespace root\ccm\policy\machine\actualconfig |
select-object ScheduledMessageID, TargetEndPoint |
where-object {$_.TargetEndPoint -ne "direct:execmgr"}
Returns the following, pay attention to the values which are similar to 00000000-0000-0000-0000-000000000116
ScheduledMessageID TargetEndPoint
------------------ --------------
{F83F662D-3DE6-4696-B064-701B2D86DADA} direct:UpdatesDeploymentAgent
{00000000-0000-0000-0000-000000000116} direct:StateMessageManager