I'm trying to create a commandlet (tried alias, but I understand it can't do what I need), to essentially do the linux equivalent of: history | grep "string".
This is the function I've created after researching on SO:
function hg {h | Select-String -Pattern $pattern}
But when I call it as: hg "some string", I get the error below.
Select-String : Cannot bind argument to parameter 'Pattern' because it is null.
At line:1 char:41
function hg {h | Select-String -Pattern $pattern}
~~~~~~~~
CategoryInfo : InvalidData: (:) [Select-String], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SelectStringCommand
Yes, pattern is null, THAT'S WHY i'm passing a value to it.
I'm about to break a monitor, I've spent an hour trying to find a solution through 100 different combinations, this is utterly infuriating.
Related
I need to write a Powershell one-liner to do a complex task. I need it to be strictly one line because I want to run it in a Go and Python script. The task requires taking the output of the first command and use it as a parameter in the second command.
I thought it was a simple task but I am struggling with it quite a bit. For example, the below command does not work:
$obj = Get-Item . | Get-Item $obj.Root | Format-List *
Get-Item : The input object cannot be bound to any parameters for the command either because the command does not take
pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:21
+ $obj = Get-Item . | Get-Item $obj.Root | Format-List *
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Users\fhcat:PSObject) [Get-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetItemCommand
What am I doing wrong?
Strictly one line sounds like a personal preference rather than a technical requirement.
However, you can end a statement with a semicolon or process the input via ForEach-Object
$obj = Get-Item .; Get-Item $obj.Root | Format-List *
or
Get-Item . | ForEach-Object {Get-Item $_.Root} | Format-List *
I do not have Powershell experience. Looking for some help to convert a huge JSON file to CSV.
I tried the below code which I found online, however it throws below error.
Code Snippet:
$file = "C:\Users\Desktop\Fk9b3ug5u\records.txt"
$pathToOutputFile = "C:\Users\Desktop\Fk9b3ug5u\Fk9b3ug5u.csv"
((Get-Content -LiteralPath "C:\Users\Desktop\Fk9b3ug5u\records.txt") | ConvertFrom-Json).results |
ConvertTo-Csv -NoTypeInformation |
Set-Content $pathToOutputFile
Error :
ConvertTo-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Users\sneshah\convertJsonToXML.ps1:5 char:5
+ ConvertTo-Csv -NoTypeInformation |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertTo-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToCsvCommand
In the code you posted, results is an object inside JSON. If your records.txt doesn't contain that object, you won't have any data when you try to get it with .results. If the entire file is able to be converted to CSV, you will just need to remove .results from your script.
Needed to clean up a winmail.dat issue by adding a contact and setting a couple of parameters. All worked as shown, except the command to test that it worked.
Get-MailContact | Select randomemail#email.com | Select -UseMapiRichTextFormat
What is the reason for this failure?
Select-Object : A parameter cannot be found that matches parameter
name 'UseMapiRichTextFormat'. At line:1 char:62 + ... t 1 Select
support#solidpe.maxdesk.us 1 Select -UseMapiRichTextFormat +
---------------------- + CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException + FullyQualifiedErrorId :
NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
I think what you actually need is this:
Get-MailContact |
Where-Object {$_.ExternalEmailAddress -eq 'randomemail#email.com'} |
Select-Object -Property UseMapiRichTextFormat
Where-Object limits the result set by comparing each contact's ExternalEmailAddress property and only including the objects that match in the output (which should only be one)
Select-Object limits the output object members to a subset of the original members
I'm trying to create a Powershell script that prints out only certain AD groups from the Folder Permission settings. However for some reason Powershell doesn't recognize StartsWith function.
("C:\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).StartsWith("sl_test")) { continue }; $_ }
When I run this I got errors similar to this for every foreach object:
Method invocation failed because [System.Security.Principal.NTAccount] does not contain a method named 'StartsWith'.
At C:\temp\test.ps1:1 char:56
+ ("C:\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).St ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Any suggestions on how to get this to work?
IdentityReference is a [System.Security.Principal.NTAccount]according to your error message.
But .StartWith is a method on the String type. If you call a method, Powershell does no magic for you, AFAIK.
Try ... ($_.IdentityReference) -match "^sl_test" ..., which should do the implicit string conversion.
If you want the string representation of an IdentityReference (regardless of whether it's and NTAccount object or a SID), you can reference the Value property:
$_.IdentityReference.Value.StartsWith('sl_test')
Try:
Get-Acl -Path "C:\folder" | Select-Object -ExpandProperty Access | Where-Object {$_.IdentityReference -like "sl_test*" }
You can customize the output with an additional | Select-Object -Property XY
Given this variable
$foo = help | Select-String powershell
Trying a split will fail
PS > $foo.split()
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo]
does not contain a method named 'split'.
At line:1 char:1
+ $foo.split()
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
How can I split this variable?
The string value of the matched line is in the Line property of the MatchInfo object.
$foo.Line.split()
Split is a String method, so it must be called on a string.
[string] $foo = help | Select-String powershell
Or
$foo = help | Select-String powershell | Out-String