|
FISHMANPET posted:I'm writing an Azure runbook that's going to validate a spreadsheet of customer data to make sure the data meets various conditions (make sure every email address is actually an email address, make sure the data matches up with our inventory, etc). It's a series of for each loops that loop through testing every row for whatever the condition is. I'd really like to pesterize this but I can't figure out any way to do it. I can break each test into a function, and I can write a pester test against that function, but I have no idea what the "best" way to actually run the tests is. The functions aren't exported, they only exist within the script. I don't want to put them in a module or something like that, because they serve no purpose outside of this runbook. I don't really want to have the functions external to the runbook in anyway because I don't want to have extra runbooks hanging out that only exist to be a source of functions. The corrected answer is "module".
|
# ? Apr 19, 2019 03:43 |
|
|
# ? May 5, 2024 17:19 |
|
New Yorp New Yorp posted:The corrected answer is "module". FISHMANPET posted:I don't want to put them in a module or something like that, because they serve no purpose outside of this runbook.
|
# ? Apr 19, 2019 03:57 |
|
Your desire doesn't change that it's the correct solution.
|
# ? Apr 19, 2019 03:59 |
|
Doesn't making a highly specific module just for a single script go against the principal of writing functions to be as modular as possible?
|
# ? Apr 19, 2019 04:03 |
|
FISHMANPET posted:Doesn't making a highly specific module just for a single script go against the principal of writing functions to be as modular as possible? No, the two things aren't related at all. Modules enable reuse across multiple consuming scripts. In this case, your scenario for reuse is "testing".
|
# ? Apr 19, 2019 04:14 |
|
FISHMANPET posted:Doesn't making a highly specific module just for a single script go against the principal of writing functions to be as modular as possible? Write your functions more broadly then, so they can be used on similar reports in the future if the need arises. Have one highly specific function that ties all your pieces together for this specific report. Then you can pester the underlying worker functions that you might end up sharing later.
|
# ? Apr 19, 2019 04:15 |
|
FISHMANPET posted:Doesn't making a highly specific module just for a single script go against the principal of writing functions to be as modular as possible? I feel personally attacked
|
# ? Apr 19, 2019 04:52 |
|
I got an ask to validate and custom sort input based on an expected list of values, but I'm not sure if there's a simpler way to do what I did. While it certainly works, it seems a little lengthy for what I'm trying to do. Can anyone tell me if this is fine or what improvements could be made?code:
PierreTheMime fucked around with this message at 16:26 on Apr 23, 2019 |
# ? Apr 23, 2019 16:23 |
|
PierreTheMime posted:I got an ask to validate and custom sort input based on an expected list of values, but I'm not sure if there's a simpler way to do what I did. While it certainly works, it seems a little lengthy for what I'm trying to do. Can anyone tell me if this is fine or what improvements could be made? code:
(You could also just use a list rather than a hashtable for the valid locations if they are sorted.) mystes fucked around with this message at 17:26 on Apr 23, 2019 |
# ? Apr 23, 2019 17:23 |
|
mystes posted:If you want something that behaves exactly like what you have except that I changed the format for the validlocations hashtable (it prints the messages about invalid/duplicate input in the order of the input and uses a hashtable to check for duplicate input for efficiency) how about something like this? No this is exactly what I was asking for. I hadn't thought to pipe the values through a chain like that, which helps flatten it out. Just using this as yet another learning exercise. Thanks!
|
# ? Apr 23, 2019 17:27 |
|
I have a tendency to overuse ForEach-Object actually; It's probably better PowerShell style to use a foreach loop when it makes sense.
|
# ? Apr 23, 2019 17:29 |
|
You mentioned just sorting based on a list and not a hashtable; provided its simpler than the current method how would you go about that with a required order like the example? I only use hashtables when necessary, so I'd love to drop that as well.
|
# ? Apr 23, 2019 17:37 |
|
PierreTheMime posted:You mentioned just sorting based on a list and not a hashtable; provided its simpler than the current method how would you go about that with a required order like the example? I only use hashtables when necessary, so I'd love to drop that as well. code:
code:
|
# ? Apr 23, 2019 17:52 |
Instead of $foo | % {$_.Trim()} you can simply write $foo | % Trim. Since Powershell 3 ForEach-Object can take the name of a member property or function and will just extract (or call) that on each object. If you don't need the verbose output you can do just this: code:
|
|
# ? Apr 23, 2019 17:55 |
|
As much as I like the succinct version, from a troubleshooting perspective I'll probably want to kick messages to the log, but it's cool to know that you can sort in order of unique keys that way.
|
# ? Apr 23, 2019 18:39 |
|
Has anyone had a problem where the Powershell ISE just starts skipping through the entire script and not running? One of my first cmdlets is essentially $aadusers = Get-AzureADUser -all $true but it just doesn't run? I've already connected, authenticated, etc. It worked flawlessly last week. EDIT - Well, I guess restarting the ISE along with re-connecting to Azure AD several times fixed it but drat that was one bizarre bug. Gucci Loafers fucked around with this message at 20:10 on May 6, 2019 |
# ? May 6, 2019 20:04 |
|
That's powershell telling you to use VS Code
|
# ? May 6, 2019 23:42 |
|
You should totally be using VS Code. I'm as shocked as anyone that Microsoft is releasing cross-platform (Win, Mac, Linux) development tools that are actually good. I need to argue with the debugger some more to get good at it, but just having multiple editors open at once is a loving godsend (I do a lot with telling machine X to run a script hosted on file share Y). I do Powershell on Windows, and have it on my Mac laptop for my occasional foray into Python (BBEdit is good enough for bash).
|
# ? May 7, 2019 08:06 |
|
mllaneza posted:You should totally be using VS Code. I'm as shocked as anyone that Microsoft is releasing cross-platform (Win, Mac, Linux) development tools that are actually good. I need to argue with the debugger some more to get good at it, but just having multiple editors open at once is a loving godsend (I do a lot with telling machine X to run a script hosted on file share Y). I do Powershell on Windows, and have it on my Mac laptop for my occasional foray into Python (BBEdit is good enough for bash). MS is seriously trying to make macs obsolete for dev work, which I'm 100% for. (My company is 95% windows, but some of our special snowflake devs need their macbooks.) https://code.visualstudio.com/blogs/2019/05/02/remote-development You can now run your stuff in vscode remotely over ssh to a linux box, to a container, to your WSL linux installation, or in a hyper-v VM hosted on your machine. I do powershell, terraform, and bash all day, and vscode is a lifesaver.
|
# ? May 7, 2019 14:30 |
|
mllaneza posted:You should totally be using VS Code. I'm as shocked as anyone that Microsoft is releasing cross-platform (Win, Mac, Linux) development tools that are actually good. I need to argue with the debugger some more to get good at it, but just having multiple editors open at once is a loving godsend (I do a lot with telling machine X to run a script hosted on file share Y). I do Powershell on Windows, and have it on my Mac laptop for my occasional foray into Python (BBEdit is good enough for bash). I love VS code but the powershell extension is loving awful and crashes every 15-20 minutes whenever I'm trying to use it to do any serious work. I have seen this behavior across 4 different computers.
|
# ? May 7, 2019 14:32 |
|
New Yorp New Yorp posted:I love VS code but the powershell extension is loving awful and crashes every 15-20 minutes whenever I'm trying to use it to do any serious work. For me the powershell debugger crashes every 3rd or 4th time I launch it.
|
# ? May 7, 2019 15:30 |
|
VS Code is awesome but my current client refuses to update it enough to be useful. I've used it on a few other projects and it's pretty drat close to replacing the whole ISE. Not quite there yet completely but pretty drat close.
|
# ? May 7, 2019 20:33 |
|
Might as well post this here. I'm hoping to automate grabbing some shopify product information via their API and dump/display it somewhere so it can be compared against a database. This is mostly half-assed together from other people's scripts, and the first part works (connecting/retrieving) and what errors out is actually using/displaying what comes down in any usable fashion:code:
The JSON is 'valid' in the sense that I normally take this output and manually parse it in MSSQL using JSON_VALUE/OPENJSON, but powershell doesn't seem to like it saying: code:
Ideally I'd like the output to look like: SKU : fulfillment_service 1 : yes 2: no etc etc. if anyone wants to make it even fancier with say CSV-output or the like.
|
# ? May 7, 2019 21:51 |
|
I suspect that Invoke-RestMethod is returning a PSObject and ConvertFrom-JSON is expecting a JSON formatted string. e: Back at a computer. Let's try a few things: code:
code:
code:
Let's just do this: code:
But Invoke-RestMethod is already producing your PSObject for you, if you wanted to select specific properties you can do this: code:
code:
The Fool fucked around with this message at 23:19 on May 7, 2019 |
# ? May 7, 2019 22:46 |
|
To put it more briefly, I think the main point is that Invoke-RestMethod is equivalent to Invoke-WebRequest | ConvertFrom-Json so you don't need to pipe Invoke-RestMethod to ConvertFromJson.
|
# ? May 8, 2019 01:32 |
|
mystes posted:To put it more briefly, I think the main point is that Invoke-RestMethod is equivalent to Invoke-WebRequest | ConvertFrom-Json so you don't need to pipe Invoke-RestMethod to ConvertFromJson. In addition, Invoke-RestMethod and ConvertFrom-Json to weird things to the pipeline in order to properly unroll arbitrary length JSON objects.
|
# ? May 8, 2019 01:50 |
|
Scaramouche posted:since I know it's good JSON coming down the pipe from the Invoke-RestMethod? A dangerous assumption to make. It definitely returns a psobject. Powershell json shows up as a type of string, and is generally not operable by other powershell commands. You can operate on the object returned by invoke-restmethod directly.
|
# ? May 8, 2019 04:44 |
|
Hey! Thanks guys; The Fool especially, I quite liked seeing the iteration of outputs and seeing the differences between them. I do believe the prevailing theory here is correct, I was trying to do some things but after the ConvertFrom-JSON when I should have been manipulating it natively as the PSO object from the Rest-Invoke method. I've got "an" output now, just need to tweak things so it is "the" output I want. I'm also doing some array within array stuff, so I foresee some for each coming up in my future.
|
# ? May 8, 2019 18:32 |
|
Work is willing to pay for some training. Before I started this job (and using powershell every day), I tried working my way through the month of lunches book and never finished. I imagine the right move is for me to start it again and finish it this time, and then look at the toolmaking book too. What do I do after that?
|
# ? May 9, 2019 04:54 |
|
It really depends on what you want to accomplish. Off the top of my head, I'd suggest some basic Azure training (when Microsoft gets their certification path figured out), which uses PowerShell for 93.3% of what you want to do.
|
# ? May 9, 2019 06:33 |
|
Happiness Commando posted:Work is willing to pay for some training. Before I started this job (and using powershell every day), I tried working my way through the month of lunches book and never finished. I imagine the right move is for me to start it again and finish it this time, and then look at the toolmaking book too. IMO once you learn the language fundamentals the next step is learning how to use the myriad core and product-specific (e.g. Active Directory, Exchange, SharePoint, PowerCLI, etc.) cmdlets that are available as well as some of the more advanced language features that are described in the about pages. When I was starting out with PowerShell I found that I learnt the fastest when I was doing things related to my work. Whenever you approach an activity take a moment to see whether it can be done with PowerShell and then try that out (Or refuse to deploy anything other than Server Core so that you're forced to use PowerShell ). Even better is when you identify an inefficient process that can be optimised or even fully automated with PowerShell. Specific to training perhaps you could talk to your employer about being granted time to work on optimisations like that to both improve your ability and provide value. Also once you've finished Month of Lunches convince your employer to shell-out for a copy of PowerShell in Depth which is also by Don Jones. It's very thorough and teaches some good design patterns for extending the utility of the code that your write.
|
# ? May 9, 2019 12:45 |
|
Learned about a cool PS script that I can use to figure out Windows 7 usage in my environment. https://gallery.technet.microsoft.com/Get-ADComputerInfops1-b5b3e656 Tried running it on a '08 R2 AD test server (with AD module imported), and all it spit out was a single column named "Length" with a bunch of three digit numbers. Can't find any use examples, does anybody know if there's any switches I need to suffix to ./Get-ADComputerInfo.ps1 ?
|
# ? May 14, 2019 23:51 |
|
Reading through that script, it doesn't look like it accepts any passed information. Is there something specific you need it to do? If you just have one domain, you should be able to use something like the below to get your Win7 count: code:
|
# ? May 15, 2019 04:48 |
|
Wizard of the Deep posted:Reading through that script, it doesn't look like it accepts any passed information. Is there something specific you need it to do? That worked perfectly actually, thanks! Am I using the second thing you provided correctly? code:
|
# ? May 15, 2019 16:27 |
|
You were using it right, I just gave you the wrong switch. Replace -name with -identity, I think. Use "get-help get-adcomputer" to get a better idea of how to use the command. You may have to run update-help from an elevated/administrator PowerShell instance.
|
# ? May 15, 2019 16:31 |
|
Wizard of the Deep posted:You were using it right, I just gave you the wrong switch. Replace -name with -identity, I think. -Identity works, thanks! I'll def take a look at what get-help shows me. The more accurate we can get our active W7 domain computer count for the big W10 upgrade, the happier the people above are going to be.
|
# ? May 15, 2019 16:51 |
|
For identifying active Win7 machines in the domain, I'd filter against PwdLastSet < 90 days. I think PwdLastSet syncs to all DCs, where LastLogonDate might not. I don't have an AD environment available right now to verify, though.
|
# ? May 15, 2019 17:38 |
|
Wizard of the Deep posted:Use "get-help get-adcomputer" to get a better idea of how to use the command.
|
# ? May 16, 2019 03:36 |
|
Wizard of the Deep posted:Use "get-help get-adcomputer" to get a better idea of how to use the command. You may have to run update-help from an elevated/administrator PowerShell instance. Also, if you don't have a test environment, using -WhatIf at the end of a command can tell you what it's going to do without it actually committing any actions.
|
# ? May 16, 2019 04:49 |
|
|
# ? May 5, 2024 17:19 |
|
Dirt Road Junglist posted:Also, if you don't have a test environment, using -WhatIf at the end of a command can tell you what it's going to do without it actually committing any actions. Assuming the command supports shouldProcess and is built with the state-changing part of the function inside a shouldProcess or shouldContinue block. It's a dangerous assumption for functions and scripts built in-house.
|
# ? May 16, 2019 14:18 |