|
Yep, a Hashtable or Dictionary is what you want for taking a department and selecting its file. Depending on how the script is going to be used, consider making it a function. That way, you could include an enum of department names, and the function's -Dept parameter could be limited to accept only values from the enum. As a bonus, you'd get tab-completion on the parameter. I can get into more detail if it's something you think you might want to do.
|
# ? Mar 11, 2022 20:26 |
|
|
# ? May 15, 2024 04:53 |
|
anything to make it "proper" or the right way.
|
# ? Mar 11, 2022 20:42 |
|
The Fool posted:it's called a hashtable I like to pronounce it "hash-tuh-bull" sorta rhyming with mashable.
|
# ? Mar 12, 2022 05:56 |
|
Thanks again everyone. I got a fully functional script to do what I needed to do and sorry for bumbling into, and making GBS threads on the carpet, of this fine thread. Even added some parameters requirements and auto tab complete the parameters that were needed so it is 100% self contained.
|
# ? Mar 13, 2022 03:08 |
|
incoherent posted:Thanks again everyone. I got a fully functional script to do what I needed to do and sorry for bumbling into, and making GBS threads on the carpet, of this fine thread. Even added some parameters requirements and auto tab complete the parameters that were needed so it is 100% self contained. Listen pal, I've shat the carpet in WAY nicer places than here. You're fine, we've all filled that quota.
|
# ? Mar 15, 2022 04:24 |
|
it's kind of cool that people will organically walk into the need for the kinds of structures that people learn in CS classes, where a kid who learns what a hashtable is in CS might never actually use such a structure in their life
|
# ? Mar 16, 2022 15:59 |
|
I have a use case where I need to execute a process, wait for it to either complete successfully or if it is still running terminate it and try again. If it terminated successfully I need to start another process/function/whatever they are called in powershell to send an email with the contents of the first command output. I literally know nothing about Powershell except what I've been able to Google. code:
How can I make the loop restart after it's killed in the if ($timedout) portion of the loop? Basically there's an Access database that has a built in function to generate a couple csv files, it gets hung up sometimes and doesn't complete and has to be manually killed. I'd like to have the ability to have this run it, watch it for 90 seconds, if it's still not done after 90 seconds kill it and start over. I hope that random mishmash of ideas makes sense.
|
# ? Mar 18, 2022 22:07 |
|
continue should do it https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_continue?view=powershell-7.2
|
# ? Mar 18, 2022 23:12 |
return will leave the loop if you have a successful first run
|
|
# ? Mar 19, 2022 17:46 |
|
Powershell implementation horror of the day: What does this snippet of code output, and why? code:
|
# ? Mar 24, 2022 05:32 |
|
|
# ? Mar 24, 2022 07:17 |
|
New Yorp New Yorp posted:Powershell implementation horror of the day: Yikes, that is rough. The good news is, it's fixed as of PowerShell 7.2 for string and DateTime. If you're stuck with PowerShell 5.1, one workaround is PowerShell code:
|
# ? Mar 24, 2022 12:22 |
|
Toast Museum posted:Yikes, that is rough. The good news is, it's fixed as of PowerShell 7.2 for string and DateTime. If you're stuck with PowerShell 5.1, one workaround is Yeah, I realized the problem and the workaround. It's just an annoying footgun that bites me every year or two and leads to confusion until I remember why it's happening and what the fix is.
|
# ? Mar 24, 2022 17:52 |
|
Newbie Powershell question, I'm trying to figure out how to powershell forcing a password reset on next login to specific users in Azure AD, but it looks like the only way to do so it by specifying object ID, and I'd like to do it by selecting userprincipalname. How do I combine Get-AzureADUser -ObjectId "name@domain.com" | select objectid with Set-AzureADUserPassword -ObjectId "ObjectIDGoeshere" -ForceChangePasswordNextLogin 1
|
# ? Apr 5, 2022 19:16 |
|
klosterdev posted:Newbie Powershell question, I'm trying to figure out how to powershell forcing a password reset on next login to specific users in Azure AD, but it looks like the only way to do so it by specifying object ID, and I'd like to do it by selecting userprincipalname. How do I combine PowerShell code:
|
# ? Apr 5, 2022 19:53 |
|
Thanks!
|
# ? Apr 5, 2022 20:42 |
|
klosterdev posted:Newbie Powershell question, I'm trying to figure out how to powershell forcing a password reset on next login to specific users in Azure AD, but it looks like the only way to do so it by specifying object ID, and I'd like to do it by selecting userprincipalname. How do I combine Without knowing the behavior of those particular cmdlets, you may be able to just pipe the one to the other. Powershell is frequently good about inferring the way objects fit together. And sometimes infuriating bad. code:
|
# ? Apr 6, 2022 00:04 |
|
Checking the docs, Get-AzureADUser's return object includes an ObjectID property, and Set-AzureADUserPassword's -ObjectID parameter accepts pipeline input, so piping one to the other should work fine. That said, some of Microsoft's examples indicate that Set-AzureADUserPassword -ObjectID should accept a UPN directly, so there should be no need to pipe from Get-AzureADUser at all.
|
# ? Apr 6, 2022 04:47 |
|
So, I'm trying to introduce the team at work to concepts like "functions" and "not writing things whole cloth every week that are impossible to QC because you aren't reusing code and nobody knows what you are trying to do". And it's an adventure because idk what I'm really doing and so I'm just looking for pointers on how to Best Practice this stuff. The team is fairly receptive to changes I've previously proposed, and they've stated interest in standardizing things going forward, but they've got even less idea what that would look like than I do, and are largely content to just winging it for another decade, unless someone sets the direction for them. So, like, here's an example of one I wrote up this morning. Common piece of code we do is stopping services before an install, but standardizing it into a robust piece with logging so they can just yeet stuff at it and it will Just Work but also make logs, would cut down on time spent and also make it easier to QC. Idk if anyone has any thoughts about whether I'm approaching it the right way, or if there's a better way to handle it. Really open to any suggestions. PowerShell code:
Toshimo fucked around with this message at 00:28 on Jun 9, 2022 |
# ? Jun 8, 2022 19:53 |
First, check what's going on with your copy-paste. All the line breaks are double on the forums, and the indentation looks uneven, especially in the documentation block. (Also, 16 spaces indentation is weird too.) As far as I can tell from reading the code, if you send in services by the pipeline, they're going to be processed sequentially with waiting for the timeout for each individual service. It would run the timeouts in parallel only if you passed the services as an array in a single parameter. If your intention is to process the timeout for all the service stopping in parallel then you'd have to only start those jobs in the Process{} block, and then do all the remaining work in the End{} block, which runs after the pipeline input to the cmdlet has finished. The basics of CmdletBinding and functions with Process{} that take input from the pipeline, is that the Process{} block is an implicit loop over each object on the pipeline, so treat it as that. The exception to that is if the type of the pipeline parameter allows array types and you pass the input object as an array as a parameter instead of through pipeline, then the Process{} block runs once with the array as input, and in that case you'd have to do an internal foreach loop over that. So what I want to say is, if you want to allow passing an array of services as a parameter and have it work, then keep the foreach loop inside the Process{} block too. Your return values are odd. The best way to return objects to the pipeline is actually to not use the return statement but just leave them to output. Additionally, you should normally not return a hashtable, but use the [PSCustomObject] pattern to convert hashtables to objects that behave nicer and work with the standard formatting and output cmdlets. Consider the standard PowerShell function naming scheme, "Verb-Noun". Stuffing extra terms in front of the verb is bad practice. PowerShell code:
It's often a good idea to try to structure data all the way through, including returns, rather than formatting for text display only.
|
|
# ? Jun 8, 2022 21:29 |
|
nielsm posted:First, check what's going on with your copy-paste. All the line breaks are double on the forums, and the indentation looks uneven, especially in the documentation block. (Also, 16 spaces indentation is weird too.) Yeah, something went bad copy/pasting it from my email on mobile (also I need to remind the Awful.apk devs that Powershell is a supported language now). nielsm posted:As far as I can tell from reading the code, if you send in services by the pipeline, they're going to be processed sequentially with waiting for the timeout for each individual service. It would run the timeouts in parallel only if you passed the services as an array in a single parameter. Yeah, I wasn't familiar with any of this, so thanks, I'll definitely invest the time into updating my stuff this way, as it'll be a lot more useful. I'm basically working from scratch and I just don't know the things I don't know. nielsm posted:Your return values are odd. This, too. It's very much a "I got things to work by doing this, so this is the way I know to do it". nielsm posted:Consider the standard PowerShell function naming scheme, "Verb-Noun". Stuffing extra terms in front of the verb is bad practice. Yes, I do know this is non-standard, but because I'm working with a group who doesn't have a deep familiarity, I'm prepending everything that is internal with the name of our team so that they can readily identify the inhouse pieces from the standard stuff. nielsm posted:[code=powershell] Thanks for this, I'll see about getting things update in the morning. nielsm posted:The entire way of returning the status seems a little odd to me, but I'm not sure if you intend to connect it with other things on pipeline that want to consume the output. I don't really have a process in place, so no, I wasn't planning on doing other stuff with this. My primary goal is to just start reducing the overhead of a lot of our common stuff to shave off errors and QC time, and then I can revisit it later for better functionality once we've got some breathing room and some more people who know what's up.
|
# ? Jun 9, 2022 00:40 |
|
I am an extremely casual user of PowerShell, but one thing I find helpful when writing larger stuff with a bunch of different functions is, before I write so much as a single line of code, breaking everything down in writing -- can be on paper, can be on the computer -- into the component functions that I'm going to need to write. Sometimes those functions will imply the need for additional functions, so I write those down too. This way, before I write, I have a list of what functions I'm going to need. The reason I find this helpful, other than the fact that it obviously makes it easier to organize and wrap my head around a larger project, is that it helps me to compartmentalize that functionality. I am going to need to do thing x a lot, I don't have to worry about it when I'm writing this other function because the logic exists in this other function that I already wrote (or am going to write). So I can just reference that function by name and assume it will return what I need, when I need it. When I write that second function, it's easier to think about it in just its own context than in the large scope of the program.
|
# ? Jun 9, 2022 02:19 |
|
Yeah, my shop is weird (or maybe not) because:
It's weird, and not conducive to learning or improvement, but at least it's a bunch of good eggs who are willing to accept new things and improve, so if I can fix me, I can port that stuff over to everyone.
|
# ? Jun 9, 2022 02:30 |
|
Toshimo posted:Yeah, my shop is weird (or maybe not) because: Have you looked at https://psappdeploytoolkit.com/? It has a bunch of pre-canned stuff specifically for app deployments that you might be able to make use of and let somebody else maintain and improve.
|
# ? Jun 9, 2022 04:00 |
|
Zaepho posted:Have you looked at https://psappdeploytoolkit.com/? It has a bunch of pre-canned stuff specifically for app deployments that you might be able to make use of and let somebody else maintain and improve. It is certainly something I've considered, when we get enough breathing room that I can start looking at better solutions, but for the moment, I've got an overwhelming backlog of several thousand applications that I have to review, so I fix what I can, and push the big stuff to next year's Toshimo.
|
# ? Jun 9, 2022 04:46 |
|
Toshimo posted:[*] We don't have any source control. This is insane and needs to be fixed immediately. There is zero excuse to not use source control and any manager who hasn't enforced it years ago is straight up incompetent. Also, start using Pester to write tests for your scripts. Then take your newfound source control and hook it up to a CI system and run the tests on every commit. New Yorp New Yorp fucked around with this message at 06:30 on Jun 9, 2022 |
# ? Jun 9, 2022 06:25 |
|
New Yorp New Yorp posted:This is insane and needs to be fixed immediately. There is zero excuse to not use source control and any manager who hasn't enforced it years ago is straight up incompetent. Yeah, we just have everything tossed up on a SMB share. My boss is all for source control, as is my team, and I've got a BitBucket set aside for it, but I haven't had enough time to figure out how I'm going to use it (again, we've got several thousand scripts that never really get updated, so it's a weird case). I'll look into Pester, thanks.
|
# ? Jun 9, 2022 07:03 |
|
Good luck feeding Stop-Service a service that doesn't exist. Also, good luck checking if a service exists before feeding it to Stop-Service. Toshimo posted:It is certainly something I've considered, when we get enough breathing room that I can start looking at better solutions, but for the moment, I've got an overwhelming backlog of several thousand applications that I have to review, so I fix what I can, and push the big stuff to next year's Toshimo. Master Packager sits on top of psappdeploytoolkit, and wraps a bunch of its options into checkboxes that you can then make templates for. Literally drag and drop msi's into a window. It'll do fancier stuff if you pay. (no affiliation) EoRaptor fucked around with this message at 04:04 on Jun 11, 2022 |
# ? Jun 11, 2022 04:01 |
|
EoRaptor posted:Also, good luck checking if a service exists before feeding it to Stop-Service. Get-service |? { $_.name -eq 'whatever'}
|
# ? Jun 11, 2022 05:27 |
|
New Yorp New Yorp posted:Get-service |? { $_.name -eq 'whatever'} Yeah, now that I reread what I wrote, I realized I’d memory holed the other part of the issue I faced, which was a security product that hid its service and process. I just remembered the frustration. But do check for a service existing before using Stop-Service, its error handling is terrible.
|
# ? Jun 11, 2022 07:43 |
|
I am losing my goddamn mind and this has to be the simplest thing.PowerShell code:
Output: --- File Contents: Alpha,1,2/3/22,22.99,76.70,123-45-6789 Beta,999,4/5/1999,.75,63.69,234-56-789 --- Alpha,1,2/3/22,22.99,76.70,123-45-6789 Beta,999,4/5/1999,.75,63.69,234-56-789 Name Value ---- ----- domain CONTOSO user jsmith 0 was CONTOSO\jsmith Why the heck is $Matches not populating and why am I not getting a boolean output from -match? Toshimo fucked around with this message at 19:10 on Aug 10, 2022 |
# ? Aug 10, 2022 19:08 |
|
Of the top of my head I see two issues: -match works on strings, $payload is an array of strings $Matches isn't getting set anywhere, so I'm not sure what you expect its value to be.
|
# ? Aug 10, 2022 19:17 |
|
To solve the first issue, I would do something like this: code:
|
# ? Aug 10, 2022 19:19 |
|
Thanks, yeah, I forgot that Get-Member doesn't tell you that it's an array unless you use -inputObject because reasons... Which I still don't think -match should just drop on the floor, but here we are.
|
# ? Aug 10, 2022 19:28 |
|
To come at the problem from another direction, if the sample data is representative of how the real data looks, have you considered using Import-Csv instead of messing around with regex?
|
# ? Aug 10, 2022 23:51 |
|
Toast Museum posted:To come at the problem from another direction, if the sample data is representative of how the real data looks, have you considered using Import-Csv instead of messing around with regex? I did, but it's not my data, and the only thing the person wants is to be able to change the money amounts easily, instead of having to do it in excel one-by-one. I had expected it to be a quick thing I knocked out over coffee until I got side-tracked over -match being buggy.
|
# ? Aug 10, 2022 23:54 |
|
Toshimo posted:I did, but it's not my data, and the only thing the person wants is to be able to change the money amounts easily, instead of having to do it in excel one-by-one. I had expected it to be a quick thing I knocked out over coffee until I got side-tracked over -match being buggy. Use import-csv and export-csv. They are designed for reading and writing csv data.
|
# ? Aug 11, 2022 00:25 |
|
New Yorp New Yorp posted:export-csv
|
# ? Aug 11, 2022 00:32 |
|
New Yorp New Yorp posted:Use import-csv and export-csv. They are designed for reading and writing csv data. I understand what they are for, but I have never seen the data, so I don't even know if they are in legit CSV format or anything.
|
# ? Aug 11, 2022 01:22 |
|
|
# ? May 15, 2024 04:53 |
Im trying to use the PowerBI cmdlet to do a survey of the datasets in my tenant. I can get a list of datasets fairly easily with Get-PowerBIDataset, but I also want to get a list of what tables each dataset uses. Supposedly Get-PowerBITable does this, but no matter what parameters I pass, or what login I use, including a service admin account, I cant get anything out of it other than an error message saying not found. Have tried searching the web for this but cant really find an answer from a reliable source, other than one post on the community forums that I hope is wrong.
|
|
# ? Aug 23, 2022 08:21 |