Is there a PowerShell 3 simplified syntax for $_ comparisons? - powershell

PowerShell 3 has a simplified syntax:
$people | ? { $_.Name -eq 'Jane' } can be written as $people | ? Name -eq 'Jane'
However, is there a simplified syntax for $_ itself?
E.g $names | ? { $_ -eq 'Jane' } can't be written as $names | ? -eq 'Jane'.
Is there some other way to write it, or is it not supported?
Not that having {} matters much, but I want to understand the full picture.

The simplified syntax in powershell 3.0 is based on parameters in Where-Object cmdlets: -EQ, -LT, -GT, etc. (named exacly like comparision operators), so it is not a "magic" but wisely chosen parameter names that mimics PowerShell's comparison operators.
Unfortunately it is not possible to reference the object itself, you have to use the old syntax (like you show in your question):
$names | Where { $_ -eq 'Jane' }

Related

Question about pipeline variables and pipeline shortcut(s)?

In the creation of a script I'm writing, I ran across the use of what appears to be a pipeline shortcut that I have never seen before.
For example:
$acl | select -expandproperty Access | ? {$_.IdentityReference -eq "AD\$Group"}
can be shortened to
$acl.Access.Where({$_.IdentityReference -eq "AD\$Group"})
What is this? $acl.Access makes sense to me as it's just a property reference but I do not see any such .Where() method being available on this object. As such it seems .Where({}) is some sort of pipeline shortcut for " | Where-Object {}". Where can I find documentation for such a thing? Do others like this exist?
Second question. I have a need to add a calculated property to my results, and within the property expression I have to perform piping as well, so where I would typically just reference $_ from the parent pipeline, this is lost within the expression statement.
I can get around this by using -PipelineVariable up within the pipeline, but it seems this only works when used with cmdlets and is not available when starting a pipeline with a variable (which I do often). Is there a way around this?
This works:
$acl = Get-Acl -Path "AD:\$dn"
$acl | select -expandproperty access -PipelineVariable AccessRight | ? {$_.IdentityReference -eq "AD\$Group"} | select *, #{N='ObjectTypeName';E={($ADGuidMap.GetEnumerator() | ? {$_.Value -eq $AccessRight.ObjectType}).Name}}
I'd really like to be able to do this with the shortcut as anywhere I can shorten my oneliner I would like to do. Unfortunately the following will not work as I cannot use pipelinevariable at this location.
$acl.Access.Where({$_.IdentityReference -eq "AD\$Group"}) -PipeLineVariable AccessRight | select *, #{N='ObjectTypeName';E={($ADGuidMap.GetEnumerator() | ? {$_.Value -eq $AccessRight.ObjectType}).Name}}
It's always bugged me about not being able to use Pipeline variables outside of cmdlets so I finally figured I'd ask if there was some sort of way around that.
As for the first question:
The .Where() and .ForEach() methods are intrinsic members, i.e. members that PowerShell implicitly makes available on objects of any type.
While they function similarly to their cmdlet counterparts, Where-Object and ForEach-Object, there are important differences.
See this answer for more information.
As an aside: You could have simplified your command even when using the Where-Object cmdlet, namely with simplified syntax:
$acl.Access | Where-Object IdentityReference -eq "AD\$Group"
As for the second question:
Because the .Where() method isn't a cmdlet, you cannot use the common -PipelineVariable parameter with it.
Given that .Access typically returns multiple objects, using the pipeline with -PipelineVariable enables an elegant solution.
If you do want to avoid the pipeline (operator), you can combine the .Where() and .ForEach() methods as follows, with the help of an intermediate (regular) variable:
$acl.Access.
Where({ $_.IdentityReference -eq "AD\$Group" }).
ForEach({
$aclEntry = $_
Select-Object -InputObject $aclEntry -Property *,
#{
N = 'ObjectTypeName'
E = { ($ADGuidMap.GetEnumerator().Where({ $_.Value -eq $aclEntry.ObjectType })).Name }
}
})
Update:
As you've discovered yourself, if you stick with a pipeline, you can combine -PipeLineVariable with Write-Output in order to capture each element of an array / collection in a pipeline variable as it flows through the pipeline, for use a later script block, as the following (contrived) example shows:
$array = 0..3
Write-Output $array -Pipeline arrayElement |
Where-Object { 0 -eq $_ % 2 } | # filter out odd numbers
ForEach-Object { $_ + 1 } | # add 1
ForEach-Object { "pipeline variable / `$_: $arrayElement / $_" }
Output, which shows that each element of input array $array was individually captured in pipeline variable $arrayElement:
pipeline variable / $_: 0 / 1
pipeline variable / $_: 2 / 3

Issue with Powershell custom table

I'm trying to create a custom table based on two other tables (csv-imported) - some kind of a VLOOKUP, but I can't seem to find a solution. I've come up with the following (failing) code:
$DrawPlaces | select Module, Workplace, #{ Name = "IPaddress"; Expression = {$Workstations.workstation.where($_.WorkPlace -eq $Workstations.Workplace)}} -First 15
Both Drawplaces and $Workplaces are PSCustomObject. The result of this would then go to another variable.
I'm not even sure the logic or syntax is correct, but the result table has the IPaddress column empty. I've also tried with -match instead of -eq.
This doesn't make sense: $Workstations.workstation.where($_.WorkPlace -eq $Workstations.Workplace)
.where() requires a scriptblock parameter like .where({}).
Keeping in mind that inside the where-statement $_ is refering to the current object in the $workstations.workstation-loop, your where-statement is testing ex. $workstations.workstation[0].workplace -eq $workstations.workplace. Is that really what you want?
Are you trying to achieve this?
$DrawPlaces |
Select-Object -First 15 -Property #(
"Module",
"Workplace",
#{ Name = "IPaddress"; Expression = {
#Save the Workspace-value for the current object from $DrawPlaces
$wp = $_.WorkPlace;
#Find the workstation with the same workplace as $wp
$Workstations | Where-Object { $_.WorkPlace -eq $wp} | ForEach-Object { $_.Workstation }
}
}
)

Powershell Match a string that may contain asterix

I'm running into issues with a string that gets parsed a certificate friendly name and attemps a lookup to see if it already exists. When the friendly name contains a '*' the script bombs.
I know I could escape the '*', but I don't have control of when it's parsed to the script. What is the best way of either escaping on the fly or matching strings when/if they contain metacharacters?
# this does not work
# Bad argument to operator '-match': parsing "*.test.com" - Quantifier {x,y} following nothing..
if(Get-ChildItem cert:\ -Recurse | Where-Object {$_.FriendlyName -and $_.FriendlyName -match '*.test.com'} | Select-Object -First 1) {
exit 1
} else {
exit 0
}
# this works fine
if(Get-ChildItem cert:\ -Recurse | Where-Object {$_.FriendlyName -and $_.FriendlyName -match 'string.test.com'} | Select-Object -First 1) {
exit 1
} else {
exit 0
}
In your specific case, I think you want to test for an exact match. In that case you should use the -eq operator instead of -match.
mjolinor's answer provides a good general explanation on whild cards.
'*.test.com' is a wildcard pattern. Use that with the -like operator. The -match operator uses a regular expression for matching, which has different rules and metacharacters than the wildcard match.
See:
Get-Help about_Wildcards
Get-Help about_Regular_Expressions

Issues with Powershell Import-CSV

Main Script
$Computers = Get-Content .\computers.txt
If ( test-path .\log.txt ) {
$Log_Successful = Import-CSV .\log.txt | Where-Object {$_.Result -eq "Succesful"}
} ELSE {
Add-Content "Computer Name,Is On,Attempts,Result,Time,Date"
}
$Log_Successful | format-table -autosize
Issues:
Log_Successful."Computer Name" works fine, but if i change 4 to read as the following
$Log_Successful = Import-CSV .\log.txt | Where-Object {$_.Result -eq "Failed"}
Log_Successful."Computer Name" no longer works... Any ideas why?
Dataset
Computer Name,Is On,Attempts,Result,Time,Date
52qkkgw-94210jv,False,1,Failed,9:48 AM,10/28/2012
HELLBOMBS-PC,False,1,Successful,9:48 AM,10/28/2012
52qkkgw-94210dv,False,1,Failed,9:48 AM,10/28/2012
In case of "Successful" a single object is returned. It contains the property "Computer Name". In case of "Failed" an array of two objects is returned. It (the array itself) does not contain the property "Computer Name". In PowerShell v3 in some cases it is possible to use notation $array.SomePropertyOfContainedObject but in PowerShell v2 it is an error always. That is what you probably see.
You should iterate through the array of result objects, e.g. foreach($log in $Log_Successful) {...} and access properties of the $log objects.
And the last tip. In order to ensure that the result of Import-Csv call is always an array (not null or a single object) use the #() operator.
The code after fixes would be:
$logs = #(Import-Csv ... | where ...)
# $logs is an array, e.g. you can use $logs.Count
# process logs
foreach($log in $logs) {
# use $log."Computer Name"
}
I'm not sure if this is the problem but you have a typo, in Where-Object you compare against "Succesful" and the value in the file is "Successful" (missing 's').
Anyway, what's not working?

Check if object is in a list of objects

As PowerShell has many SQL query-like cmdlets, is there a fast way to check if object is in list of other objects with the Where-Object cmdlet?
Something like in SQL:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
Of course I can write a simple subroutine, but I was just wondering if there is such feature.
You can use the -contains operator:
Get-ColumnNames $table | Where-Object { value1,value2,... -contains $_ }
It's backwards, though with the collection of values on the left side.
In PowerShell 3 you can also use the -in operator:
Where-Object { $_ -in value1,value2,... }
or even
Where-Object -In value1,value2,...
Also, a quirk of how PowerShell works with comparison operators, you can apply them directly to a collection on the left side:
Where-Object { value1,value2,... -eq $_ }
The -eq operator here will either yield the respective element if it is in the list, or $null (which coerces to $false).