Comments (4)
Check the results in your own shell for:
& (Get-Command Get-History)
vs
Get-Command Get-History
The exercise here is intended to illustrate the object types that are returned from the cmdlets as you actually execute them. The syntax here is a bit confusing perhaps because of the nature of the test, though.
Get-History
will not be a great cmdlet for this purpose since PSKoans runs all tests in an isolated session, so Get-History
will have nothing to show and will return no results. If you leave the test as it was and use (for example)Get-ChildItem
in the test, you would get different results.
To illustrate, run these commands one after another:
Get-Command Get-ChildItem
& (Get-Command Get-ChildItem)
In other words, &
is used to call the function that's being named as a string in the test; & (Get-Command Get-History)
does the same thing as just calling Get-History
.
A proper fix here would probably be to collect results first and verify the command produce some results, and give a specific error if it doesn't.
from pskoans.
I see. So, me choosing Get-History
caused this to fail, not the syntax itself. Interesting. Have not considered the consequences of my actions (the output of the function^^). I knew &
does call (or invoke?) what comes next, but I don't fully understand why it is synonymous with the second command (but not the first?) How does it know that? Following your Get-ChildItem example, I see the output is the same as if you were to run the command. Piping this to Get-Member will set it to find the Members of the Object coming through the pipeline. So far so good.
I understand that the Object received without the call operator is a different one:
❯ Get-History | Get-Member
TypeName: Microsoft.PowerShell.Commands.HistoryInfo
❯ Get-Command Get-History | Get-Member
TypeName: System.Management.Automation.CmdletInfo
❯ & (Get-Command Get-History) | Get-Member
TypeName: Microsoft.PowerShell.Commands.HistoryInfo
If I do understand this correctly, i can mock run every single command and get the (default) output type it returns (Parameters changing the output notwithstanding)? - If so, this will be quite an epiphany!
Testing:
$cmdlet1 = 'Get-ChildItem'
$PropertyName = 'Directory'
$Reason = $BecauseString -f $PropertyName, $cmdlet1
$result = & (Get-Command -Name $Cmdlet1) | Get-Member -MemberType Property -Name $PropertyName
$result | Should -Not -BeNullOrEmpty -Because $Reason
and
$cmdlet1 = 'Get-History'
$PropertyName = 'CommandLine'
$Reason = $BecauseString -f $PropertyName, $cmdlet1
$result = & (Get-Command -Name $Cmdlet1) | Get-Member -MemberType Property -Name $PropertyName
$result | Should -Not -BeNullOrEmpty -Because $Reason
both work for me in the context of my shell (with Clear-History
sprinkled in just before running the $result =..
)
Haven't tested it now with the full Koan though... - It might need a bit more testing :)
from pskoans.
If I do understand this correctly, i can mock run every single command and get the (default) output type it returns (Parameters changing the output notwithstanding)? - If so, this will be quite an epiphany!
It's not really a mock run in the sense that if you do it with a destructive command, it can and will still do the things it's designed to do. But yeah, it's an alternative way of calling a command. 🙂
Get-Command returns one or more CommandInfo objects, and &
has the ability to invoke those (among other things as well) which tells PS to call the command.
from pskoans.
ok. I think this was an unfortunate user error - closing
from pskoans.
Related Issues (20)
- Problem in koan AboutModules
- Koan AboutSelectObject has undesired error
- Problem with different PowerShell versions in koan AboutSortObject
- Topics solved before starting in koan AboutComparison
- Show-Karma failed on relatively fresh installation HOT 6
- AboutCmdletVerbs.Koans.ps1 - Forward slash in $Filename results in wrong string comparison at the end HOT 2
- AboutPSProviders.Koans.ps1 - thows errors (may be unavoidable) HOT 8
- Recursion on $home throws UnauthorizedAccessException - Local Issue HOT 4
- Enumeration time is quite long the more you progress HOT 2
- AboutRedirection misses some spaces in output?
- Get-Unique is used on unsorted collections
- AboutAssignmentAndArithmetic: ExpectedMessage and implicit character ranges in Pester 5 HOT 1
- Show-Karma fails after repo-based installation HOT 2
- AboutBitwiseOperations - Error in description? HOT 1
- Show-Karma -Contemplate breaks after Classes HOT 12
- VScode error: Could not find the module 'PSKoans'. commands typed in console window work fine. HOT 2
- About XML addresses $RootElement but that changes with the next test HOT 1
- Regex: $matches[0] is empty within "It does not need to match the entire string" and "It is not just an array of text"
- AboutNumbers wrong types HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pskoans.