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
Related
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.
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.
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
I wrote a small script to grab event log entries from a remote machine and write it to a .csv file. The script works when targeting a single machine, but when I try to implement a for loop and loop it over all machines in Active Directory, I get this error:
Method invocation failed because [Microsoft.ActiveDirectory.Management.ADComputer]
does not contain a method named 'op_Addition'.
At Y:\srp.ps1:7 char:143
+ ... | Export-Csv $($computer + ".csv")
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Export-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty.
Provide an argument that is not null or empty, and then try the command again.
At Y:\srp.ps1:7 char:141
+ ... 0 | Export-Csv $($computer + ".csv")
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv],
ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,
Microsoft.PowerShell.Commands.ExportCsvCommand
The error indicates there's a problem with the Export-Csv command, but running the command by itself creates the log files needed. Here is the full script, for reference:
# Gets SRP event log entries from remote machine and writes them to a .csv file
# of the same name.
Write-Output "Running..."
$computers = Get-ADComputer -filter {(Name -like "PC*") -or (Name -like "LT*")}
foreach ($computer in $computers) {
Get-EventLog -LogName Application -Source Microsoft-Windows-SoftwareRestrictionPolicies
-ComputerName $computer -Newest 10 | Export-Csv $($computer + ".csv")
} #end foreach
Write-Host "Done."
Any ideas as to why this error appears when I try to loop over computers in AD?
It looks like Get-ADComputer returns ADComputer objects, but you're passing it to Get-EventLog's ComputerName parameter, which takes a string, as-is. I'm assuming you'll need to grab the name property from the Microsoft.ActiveDirectory.Management.ADComputer object.
Take this as an example:
$var = "one","two","three"
0..2 | select-object $var[$_]
Index operation failed; the array index evaluated to null.
At line:1 char:27
+ 0..2 | select-object $var[ <<<< $_]
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
Why is $_ a null?
I can't figure why you are using select-object
try this:
$var = "one","two","three"
0..2 | % { $var[$_]}
You can also use select-object in a different way:
$var | select -Index (0..2)
And btw, if all you want to do is extract element in the array, you can do array slicing:
$var[0..2]
If you want to use select-object you need to create a hashtable, aka calculated property, and give it a name and expression keys. The result would be objects with one property, var, in the following example
0..2 | select #{name='var';expression={$var[$_]}}
var
---
one
two
three