Joel Sallow replied to the topic How to Enable Admin account by user account with powershell ise in the forum General PowerShell Q&A 4 days, 17 hours ago
Should do the trick, provided the account running it has the necessary access to do so. I think Windows always ensures at least one administrative account is active at any given time, usually.
Joel Sallow replied to the topic Noob Question My apologies in the forum General PowerShell Q&A 5 days, 17 hours ago
I have been putting together some learning resources for PowerShell, written in PowerShell to give you a good level of immersion whilst explaining quite a bit along the way.
For this sort of stuff, specifically, I would highly recommend looking over the AboutDiscovery koans; that covers fairly extensively…[Read more]
Joel Sallow changed their profile picture 6 days, 13 hours ago
Joel Sallow replied to the topic Data Stream Best Practices in the forum General PowerShell Q&A 6 days, 13 hours ago
Verbose should, generally, list what’s being attempted — and perhaps when the action has been successfully completed as well, if listing that is meaningful in context.
Warnings should be emitted whenever a non-critical failure or potentially dangerous action has occurred.
Depending on the situation, you may want to implement ShouldProcess to…[Read more]
Joel Sallow replied to the topic why can't use this variable in my script in the forum General PowerShell Q&A 1 week, 1 day ago
So if I’m reading this correctly… essentially what you’re trying to do is have a function that takes a script path and simply executes it. Or something along those lines.
If you need to read the contents of a script, you want to read it as a text file:
Get-Content -Path $FilePath
If you want to define an input string as an executable script…[Read more]
Joel Sallow replied to the topic How to return an empty array from a function in the forum General PowerShell Q&A 1 week, 3 days ago
Hmm, no, it wouldn’t. Write-Output enumerates collections, sending items one at a time… and there’s nothing in that collection.
In order to do this, you’d need to make use of the unary comma operator:
That said, I’m not sure why you’d do such a thing. An empty array is a useless object — any operations to add another item will destroy the…[Read more]
Joel Sallow I just gained a new achievement!!! 1 week, 6 days ago
I just gained the achievement HELPFUL and got 250 points!
Joel Sallow replied to the topic Pipeline input by property name in the forum General PowerShell Q&A 1 week, 6 days ago
So, interestingly enough, PS Core actually removed the -ComputerName parameter from Get-Service; not sure why. Perhaps related… and there aren’t any other ByPropertyName parameters on that cmdlet. I would generally assume that only -Name will work correctly with pipeline input (it does seem to be what the help documentation states, with the…[Read more]
Get-Service is a bit bugged in that respect. It should work like that (just piping an object into the cmdlet with the right property names) but I believe (not sure whether it’s a bug in the pipeline logic or the cmdlet itself) it also inserts the input value as the -Name parameter of Get-Service despite you specifying it as ComputerName by the p…[Read more]
Joel Sallow replied to the topic How to output multiple regex matches from single string on one line in the forum General PowerShell Q&A 1 week, 6 days ago
you can either add the strings together or, as you mentioned delimiters, you can use -join:
$matchedstrings -join ‘, ‘
Joel Sallow replied to the topic Query regarding limit on maxmimum number of ParameterSets for powershell cmdlet? in the forum General PowerShell Q&A 2 weeks, 3 days ago
You need to explicitly specify the default parameter set in the cmdletbinding statement:
[CmdletBinding(DefaultParameterSetName = ‘All’)]
But in all honesty you probably need to look at breaking that function into several pieces. 32 parameter sets is getting to be far too many; most standard cmdlets have less than 10 sets in total,…[Read more]
Joel Sallow replied to the topic Start-Process passing credentials variable in the forum General PowerShell Q&A 2 weeks, 5 days ago
You need a param() block in the script itself to properly accept the credential variable, otherwise it will be stored in $args in the script. Param() in the invoke-command block is separate and also required if you want to pass the parameter by name. Additionally, I wouldn’t use Start-Process for that; it’s more effective to just invoke the…[Read more]
Joel Sallow replied to the topic In/Notin operator outputs to collection in the forum General PowerShell Q&A 2 weeks, 5 days ago
Where-Object Name -in $list2.Name |
Select-Object -Property Name, ‘Application Name’
Assuming I’m reading your code correctly, this should do much the same thing.
Depends on the collection structure, though.
Joel Sallow replied to the topic Why would a function have empty parentheses? in the forum General PowerShell Q&A 2 weeks, 5 days ago
It’s probably a coding pattern they picked up from elsewhere. For example, in C# a method (aka function) must always have parentheses to be valid syntax.
It’s not very good PowerShell syntax, but it is technically valid.
Joel Sallow replied to the topic Using Class in the forum General PowerShell Q&A 2 weeks, 6 days ago
Put the path in quotes?
using module “$PSScriptRootClassFile.psm1”
It’s not so much that OP wants one class to inherit another. They want one class to return an object type of the other as the returned object from a method… if I’m understanding the intention here.
If you need a relative path, I’d make use of $PSScriptRoot; relative paths are fragile and easily break.
Joel Sallow replied to the topic RegEx Pain in the forum General PowerShell Q&A 2 weeks, 6 days ago
Multiline strings are always a bit tricky, especially because in PS they can come in two flavours — a single string, or many strings, one per line.
I wouldn’t use Select-String here, though, in general. It’s good for finding the line your match exists on, but actually retrieving the match is a little trickier in some cases. I like to work with…[Read more]
Chadnan Kumar and Joel Sallow are now friends 2 weeks, 6 days ago
It’s a bit tricky to do this with the classes in separate files. Typically what I see done is that when someone builds a module, the classes/enums/etc are often all compiled into a single file during the build step of their publishing pipeline.
This helps to circumvent the weird issues with classes like this.
Joel Sallow replied to the topic How to display only filesystemobjects from PSDrive command in the forum General PowerShell Q&A 3 weeks ago
Get-PSDrive -PSProvider FileSystem