I am trying to filter all methods named 'Create' in all workspaces as part of a learning Powershell exercise.
I have this command which seems to list all the methods but I am unable to filter on 'Create' when I pipe the output of teh below to either -Filter or where.
What am I doing wrong here?
Get-WmiObject * -List | Where-Object {$_.methods} | select -ExpandProperty Methods
With -Filter
PS C:\Windows\system32> Get-WmiObject * -List | Where-Object {$_.methods} | select -ExpandProperty Methods | -Filter
-Filter : The term '-Filter' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:86
+ ... Where-Object {$_.methods} | select -ExpandProperty Methods | -Filter
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (-Filter:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Or select where
PS C:\Windows\system32> Get-WmiObject * -List | Where-Object {$_.methods} | select -ExpandProperty Methods | select name
-eq "Create"
Select-Object : A parameter cannot be found that matches parameter name 'eq'.
At line:1 char:98
+ ... _.methods} | select -ExpandProperty Methods | select name -eq "Create ...
+ ~~~
+ CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
If you want to list the actual Create methods themselves:
Get-WmiObject -List |
Select-Object -ExpandProperty Methods |
Where-Object Name -eq Create
If, by contrast, you want to list the classes that have a Create method:
Get-WmiObject -List | Where-Object { $_.Methods.Name -contains 'Create' }
As for what you tried:
-Filter is a parameter, yet you tried to use it as a command. It's also unclear what you're trying to filter, given that there's no argument.
select name -eq "Create" mistakenly applies the Where-Object syntax to the Select-Object (select) cmdlet.
In both attempts:
the use of Where-Object {$_.methods} is unnecessary, because the
Select-Object -ExpandProperty Methods call will automatically ignore method-less classes.
-List by itself is enough to list all classes; no need for wildcard *.
Related
I have a powershell script below which should logically work but throws below error saying "A drive with the name 'E" does not exist." But in fact it exists. This error comes when I input the drive using a variable, but if I input the drive that is path manually like "E:" it will work ok. Do no know what I am doing wrong.
Get-ChildItem : Cannot find drive. A drive with the name 'E' does not exist.
At line:24 char:10
+ $list = Get-ChildItem -path $CDDriveLetterToText
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (E:String) [Get-ChildItem], DriveNotFoundExcepti
on
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
My code is as below.
Function Image-Windows10 () {
$CDDrives = Get-WmiObject win32_volume | where {$_.DriveType -eq '5'} | Select-Object -Property name
$FlashDrives = get-wmiobject win32_diskdrive | where {$_.InterfaceType -eq 'SCSI'} | select-object -property index, size
[int]$NumberOfFlashDrives=$FlashDrives.Count
[int]$NumberOfCDDrives=$CDDrives.Count
$CDDriveLetterToText = Out-String -inputObject $CDDrives.Get($NumberOfCDDrives-1)
$CDDriveLetterToText = $CDDriveLetterToText.Replace("name","").Replace("----","").Replace("`n","").Replace(" ","")
$list = Get-ChildItem -Path $CDDriveLetterToText
}
Image-Windows10
instead of selecting the name you might want to go for the property DriveLetter. the parameter -ExpandProperty will return an array of the specified value - thus no need to manipulate the string'
# get all drive letters from devices of type 'CDRom'
$driveArray = Get-WmiObject win32_volume | where {$_.DriveType -eq '5'} | Select-Object -ExpandProperty DriveLetter
# this array can then be iterated like
foreach($drive in $driveArray) {
$list = Get-ChildItem -Path $drive
}
I am currently trying to run a script that is as follows (to find and uninstall CCleaner):
Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -eq "CCleaner"} -OutVariable Results **{
& "$($Results.InstallLocation)\uninst.exe"
}
The error is:
Where-Object : A positional parameter cannot be found that accepts argument
& "$($Results.InstallLocation)\uninst.exe" /S
.At line:1 char:98
+ Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object <<<< {$_.DisplayName -eq "CCleaner"} -OutVariable Results {
+ CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand
The last part there seems to be a problem. I am sure this is because I am writing this in PS v3 but I'm running this as a PSSession on PC's running PS v2 or v1.
I think this is what you're after:
Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -eq "CCleaner"} | ForEach-Object { & "$($_.InstallLocation)\uninst.exe" }
Otherwise, you need to use a named parameter after "Results"
The first sorry for my english, but I need your help with powershell scripting.
I must config IIS server with powershell script and I have problem with "add application". I need to check whether webapplication exist in site. I want
choose all webapplication in site "testsite2"
$webapplication=Get-WebApplication | Where-Object {$_. applicationpool -eq "testsite2"} | select -expand Name;
But I have problem that command is ending with error
select : Property "Name" cannot be found.At line:1 char:75 Get-WebApplication | Where-Object {$_. applicationpool -eq "testsite2"} | select ... CategoryInfo : InvalidArgument: (Microsoft.IIs.P...gurationElement:PSObject) [Select-Object], PSArgumentException FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
I don´t understand. What have I do when I want choose value from column "Name"?
I get same result
Get-ChildItem –Path IIS:\AppPools\ | Where-Object {$_. Name -eq "testsite2"} | select -ExpandProperty applications
result:
select : Property "applications" cannot be found.
At line:1 char:80
Get-ChildItem –Path IIS:\AppPools\ | Where-Object {$_. Name -eq "testsite2"} | s ...
CategoryInfo : InvalidArgument: (Microsoft.IIs.P...gurationElement:PSObject) [Select-Object], PSArgumentException
FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
The WebAdministration module can be a bit deceiving in that it uses custom formatting data to show stuff like Name with Format-Table (even though the underlying objects have no Name property).
To get the equivalent Name value, take the Path property and trim /:
$WebAppNames = Get-WebApplication |Select #{Name='Name';Expression={$_.Path.Trim('/')}} |Select -Expand Name
The above is exactly what the formatting subsystem in PowerShell does to calculate the Name value.
You can explore the actual properties an object has with the Get-Member cmdlet:
Get-WebApplication |Get-Member
This will also show you the type name, which you can use to explore any additional Type- or Format-Data that might apply:
Get-TypeData -TypeName 'Microsoft.IIS.PowerShell.Framework.ConfigurationElement#site#application'
Get-FormatData -TypeName 'Microsoft.IIS.PowerShell.Framework.ConfigurationElement#site#application'
Can explain why philosophically this doesn't work?
Just as a learning example, I wanted to see the properties of the get-service cmdlet, without the events or methods.
PS C:\Users\Neal> get-service | get-member | {$_.name -eq "Property"}
Result:
At line:1 char:29
+ get-service | get-member | {$_.name -eq "Property"}
+ ~~~~~~~~~~~~~~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
{$_.name -eq "Property"} is just a scriptblock. If you want to use Where-Object to filter the results of get-member, you need to type Where-Object:
PS C:\Users\Neal> get-service | get-member | Where-Object {$_.name -eq "Property"}
or you can use where, which is an alias for Where-Object:
PS C:\Users\Neal> get-service | get-member | where {$_.name -eq "Property"}
There is even a special character ? which refers to Where-Object:
PS C:\Users\Neal> get-service | get-member | ? {$_.name -eq "Property"}
All three examples given above do the same thing. Choosing between them is simply a matter of style.
I am trying to get the OWNER of a process, code :
(Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | Foreach-Object user | out-string**
This works great under win8 but in win7 I get this msg :
ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the "user" val
ue of type "System.String" to type "System.Management.Automation.ScriptBlock".
At C:\Program Files (x86)\Advanced Monitoring Agent GP\scripts\9660.ps1:1 char:
108
+ (Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'
}).getowner() | Foreach-Object <<<< user | out-string
+ CategoryInfo : InvalidArgument: (:) [ForEach-Object], Parameter
BindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh
ell.Commands.ForEachObjectCommand
Help please! Thank you for the time.
Instead of foreach-object user, use select -expand user. This is equivalent to doing foreach-object { $_.user } which is probably what you meant to do. Improvements to flexibility in the grammar allow your first attempt in later versions of powershell.
The older version of Powershell won't work with the simplified syntax. This should work on either one:
(Get-WmiObject -class win32_process |
where{$_.ProcessName -eq 'explorer.exe'}).getowner() |
Foreach-Object { $_.user | out-string }
(Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | select user
I had a similar problem but in my case, there was a non-printable character in my script that appeared after one of the }'s. ASCII code 03. I found that by opening the script in a binary editor (Textpad8). I deleted this character and it fixed the problem for me.