|
Ali Aces posted:I'm not sure I follow (still kind of a neophyte at this), but if I'm getting you correctly: code:
code:
To address your confusion about the "foreach loop": this is a quirk about using an array parameter that accepts pipeline input: code:
code:
When you call it by Pipeline, the Process{} block gets called once for each item, and the value of $FileName is a single item in each iteration. When you use a foreach loop on an item that is not array, Powershell gracefully handles it as though it were an array that contained just that single item. It's very handy but can be confusing if you don't realize it. It turns out this is perfect usage in a Process{} block though, because it allows you to neatly handle both ways of calling the function without writing any messy conditionals; your code inside the foreach loop will always be handling a single object. And yes, each call to Write-Output will send the object down the pipeline, but I think it should finish the current instance of the process block before proceeding (it just won't run the End{} code yet). Also, while Write-Output is the correct cmdlet to call, you can also just omit it: code:
But in case you wanted to know, you can explicitly test for which parameter set was called: code:
|
# ? Apr 29, 2015 06:47 |
|
|
# ? Apr 25, 2024 23:09 |
|
Briantist posted:When you call it by parameter, the Process{} block only gets called once, and the value of $FileName is an array. Ah I get it, that makes total sense. Thanks for the explanation! Briantist posted:But in case you wanted to know, you can explicitly test for which parameter set was called: Thanks, I was actually thinking about just getting rid of the parameter sets since there's really no good reason why you couldn't throw both in there if you really wanted to. It'd deal with it just fine, you'd just get a big dump of all the processes together. I originally only put them in so it'd show two different syntaxes on the help page.
|
# ? Apr 29, 2015 19:59 |
|
This was a while back, but i finally finished the script, thx to your hints. I wouldn't have thought to just automate the whole process. Once again thank you guys for steering me in the right direction. code:
|
# ? May 1, 2015 12:03 |
|
Ali Aces posted:Hey I know this was from weeks ago now, but I've been meaning to post this and now it's cleaned up, so here: This is so friggin cool. poo poo, I need to get better at Powershell. I've been using it for really basic automation but this is great. Thanks a lot! sorta edit: this thread's been around for a long while so I'm sure the info is in here somewhere but... besides just figuring out scripts as the need arises do you guys have any advice on a good book or some way to train myself as some sort of powershell ninja? snackcakes fucked around with this message at 01:53 on May 2, 2015 |
# ? May 2, 2015 01:50 |
The latest version of the Pwershell in a month of lunches book
|
|
# ? May 2, 2015 02:09 |
|
ConfusedUs posted:The latest version of the Pwershell in a month of lunches book The author of which posted this fantastic 4-hour video, which is how I got started. https://www.youtube.com/watch?v=-Ya1dQ1Igkc
|
# ? May 2, 2015 03:05 |
|
Ali Aces posted:The author of which posted this fantastic 4-hour video, which is how I got started. Neat. I'll check that out! ConfusedUs posted:The latest version of the Pwershell in a month of lunches book Been meaning to get this book. I was hoping to hold out to see if he'd come out with a new edition for 4 but...this version is probably good enough.
|
# ? May 2, 2015 03:26 |
|
That din jones video taught me as well. I recommend him to everyone.
|
# ? May 2, 2015 09:25 |
|
Swink posted:That din jones video taught me as well. I recommend him to everyone. I worked with them on creating a Powershell quiz on Smarterer, though until I just searched for it now, I hadn't realized it went live.
|
# ? May 3, 2015 00:36 |
|
Is there an easy way to print a PDF file to a network printer, using powershell? I couldn't find an easy way to do it, so right now I'm having the script make the network printer be the default printer, then print to default printer.
Hughmoris fucked around with this message at 20:23 on May 5, 2015 |
# ? May 5, 2015 20:20 |
|
Ignore.
|
# ? May 5, 2015 20:21 |
|
Are you using powershell 4? Get-Printer and Out-Printer should probably do what you need. There's also the .Net System.Drawing.Printing namespace which you could use with some effort: https://msdn.microsoft.com/en-us/library/system.drawing.printing%28v=vs.110%29.aspx
|
# ? May 5, 2015 20:42 |
|
Reiz posted:Are you using powershell 4? Get-Printer and Out-Printer should probably do what you need. There's also the .Net System.Drawing.Printing namespace which you could use with some effort: https://msdn.microsoft.com/en-us/library/system.drawing.printing%28v=vs.110%29.aspx I'm using Powershell 2. It's a work computer running Windows 7, and I'm a bit leery about trying to update powershell to a newer version. I'll have to take a look at that Namespace link you provided. The method I'm using now works but its a bit...awkward.
|
# ? May 6, 2015 00:25 |
|
Hughmoris posted:I'm using Powershell 2. It's a work computer running Windows 7, and I'm a bit leery about trying to update powershell to a newer version. I'll have to take a look at that Namespace link you provided. The method I'm using now works but its a bit...awkward.
|
# ? May 6, 2015 21:16 |
|
There are no concerns with upgrading your version of Powershell. Please install 4.0! It's very good and much improved, the ISE is improved too!
|
# ? May 6, 2015 21:30 |
|
Briantist posted:What are your concerns about upgrading powershell? If you already have .Net 4 then you just need to install WMF 4 to get it. I'm not too familiar with PS, and with it being a work computer I'm not sure if an upgrade would have a negative impact on anything else installed.
|
# ? May 7, 2015 01:21 |
|
Hughmoris posted:I'm not too familiar with PS, and with it being a work computer I'm not sure if an upgrade would have a negative impact on anything else installed. If you're not doing administration of those products directly from this machine, then seriously don't worry about it, do the upgrade. If you are using those, just check for when the problems were fixed and see if the updates are installed. To my knowledge, there was no such issue when v4 came out, everything worked.
|
# ? May 7, 2015 16:41 |
|
We're stuck on PS2.0 in production for our SharePoint farms. Validated environments
|
# ? May 7, 2015 16:46 |
|
Is there an easier way to forcibly remove the read only attribute on files recursively than this? ATTRIB won't cut it since some of the files are system/hidden and need to retain those flags (and ATTRIB skips them if they are set). get-childitem . -recurse -force | where-object {$_.GetType().ToString() -eq "System.IO.FileInfo"} | Set-ItemProperty -name IsReadOnly -value $false -force Sheep fucked around with this message at 18:34 on May 8, 2015 |
# ? May 8, 2015 18:11 |
|
Sheep posted:Is there an easier way to forcibly remove the read only attribute on files recursively than this? ATTRIB won't cut it since some of the files are system/hidden and need to retain those flags (and ATTRIB skips them if they are set).
|
# ? May 8, 2015 18:40 |
|
Instead of code:
code:
|
# ? May 8, 2015 18:53 |
|
Yeah, be aware that calling the GetType method means you're using reflection, which is pretty slow. In general, if you find yourself reaching to GetType, chances are very good you're missing a better way of doing what you want to do.
|
# ? May 9, 2015 00:05 |
|
I'm trying to script out getting the total size of a few directories using:code:
Any ideas?
|
# ? May 9, 2015 00:18 |
|
Toshimo posted:I'm trying to script out getting the total size of a few directories using: Unfortunately, for whatever reason, one thing Powershell does not seem to deal with well at all are directory symlinks/junctions, which IMO is a pretty goddamn huge oversight for something running on an operating system that builds a recursive directory link directly into every user's profile. However, you might be able to get it to work by adding -Attributes !ReparsePoint, or maybe -Attributes -notlike *ReparsePoint*, to your Get-ChildItem command. See: http://superuser.com/questions/528487/list-all-files-and-dirs-without-recursion-with-junctions https://technet.microsoft.com/en-us/library/hh847897.aspx
|
# ? May 9, 2015 03:15 |
|
Just starting out, learning the help commands. It seems like help and man are identical, while get-help gives all of the information and then scrolls all of the way to the bottom. Are there any other nuances?
|
# ? May 18, 2015 22:47 |
|
22 Eargesplitten posted:Just starting out, learning the help commands. It seems like help and man are identical, while get-help gives all of the information and then scrolls all of the way to the bottom. Are there any other nuances? You can see why that is by typing: get-alias man "man" is an alias for "help". The get-help cmdlet lets you pull up info on any given cmdlet. For example: get-help select-object -full Give it a try! e: remember to try update-help first assuming you have an internet connection to download updated help files
|
# ? May 18, 2015 22:50 |
|
When I'm doing get-help stuff, I almost always do "help <whatever> -online" and view it in a browserCLAM DOWN posted:e: remember to try update-help first assuming you have an internet connection to download updated help files
|
# ? May 18, 2015 23:04 |
|
myron cope posted:When I'm doing get-help stuff, I almost always do "help <whatever> -online" and view it in a browser Yes you need to be elevated for this since it updates the help globally.
|
# ? May 18, 2015 23:07 |
|
22 Eargesplitten posted:Just starting out, learning the help commands. It seems like help and man are identical, while get-help gives all of the information and then scrolls all of the way to the bottom. Are there any other nuances? If you like the way that help/man don't scroll all the way down, you can make other commands do that by piping them to the command "more" code:
|
# ? May 19, 2015 00:52 |
|
I've done something stupid and run a start-dscconfiguration on my workstation. Now it processes the config every 30 mins which includes renaming my PC and a heap of other poo poo I do not want. How do I tell the LCM to stop processing the DSC config?
|
# ? May 25, 2015 03:58 |
|
Does Stop-DSCConfiguration not work?
|
# ? May 25, 2015 20:08 |
|
That seems to only stop an in-progress configuration. I got around it by generating a new config with no configurations and applied it.
|
# ? May 25, 2015 23:57 |
|
So I have been trying to refactor this python script in to Powershell. Basically it simulates (at a very basic level) people entering a bank, getting in lined, served by a teller and leaving. The problem is that while Powershell offers a Get-Random function, you can specify a min and a max, but it doesn't have anywhere near Python's random.expovariate(lambd) function, which is described as, python documentation posted:"Exponential distribution. lambd is 1.0 divided by the desired mean. It should be nonzero. (The parameter would be called “lambda”, but that is a reserved word in Python.) Returned values range from 0 to positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative.". In another description, "expovariate() produces an exponential distribution useful for simulating arrival or interval time values for in homogeneous Poisson processes such as the rate of radioactive decay or requests coming into a web server. On line 16 they use that function. Here's a youtube video describing it in great detail starting around 4:45 The TL;DR version is that you want random numbers, but 50% of the time you want the number to be between 0 and 1, 25% of the random numbers between 1 and 2, 12.5% of numbers to be between 2 and 3, 6.25% of numbers between 3 and 4, etc etc like this, but in a random order. So I decided to write it. I'm having one of these moments, Because it mostly works, but it's not quite right. Instead of 50/25/12.5/6.25 split I am getting 50/23/11/6. Which statistically is mostly within the margin of error on 1000 runs, but it's always low, never high. More importantly: I'm only getting these values when I set for a Median of 3, not of 1. I've having to multiply the result by three, effectively, to get the valid test data I want. Obviously this won't work very well/reliably if I have to massage the formula every time I modify my curve. If I set the median to 3 instead of 1, I come out with that split. My functions were modeled after this (Pastebin of C# library). You don't need to dig any deeper than GetUint as it's effectively calling "Get-Random -Minimum -0.00 -Maximum 1" Supposedly this is the model I should be following: log(1-$u)/(−λ) code:
Hadlock fucked around with this message at 05:28 on May 26, 2015 |
# ? May 26, 2015 05:15 |
|
I wouldn't even know where to start with the math operations, but the nice thing about PowerShell is that if the cmdlet or assemblies don't exist, you can always write your own in C#. You might have better luck that way then looking for a cmdlet to do it for you.
|
# ? May 26, 2015 13:09 |
|
Spazz posted:I wouldn't even know where to start with the math operations, but the nice thing about PowerShell is that if the cmdlet or assemblies don't exist, you can always write your own in C#. You might have better luck that way then looking for a cmdlet to do it for you.
|
# ? May 26, 2015 16:39 |
|
Swink posted:That seems to only stop an in-progress configuration. http://stackoverflow.com/a/25526945/3905079
|
# ? May 26, 2015 16:43 |
|
Kind of stumped here. I'm working with a CSV and following this technet tutorial. My starting CSV is in this format: code:
and my output is this in this format: code:
code:
Hughmoris fucked around with this message at 03:17 on Jun 2, 2015 |
# ? Jun 2, 2015 03:14 |
|
Hughmoris posted:Kind of stumped here. I'm working with a CSV and following this technet tutorial. The important thing to remember is that you have an object, with properties, and you can view and manipulate it as you see fit. The way it's displaying at the moment is in "List" format, probbaly because PowerShell determined it would fit better that way or be too crammed in a table. But you can easily change that with Format-* cmdlets: code:
code:
|
# ? Jun 2, 2015 03:25 |
|
Briantist posted:The important thing to remember is that you have an object, with properties, and you can view and manipulate it as you see fit. The way it's displaying at the moment is in "List" format, probbaly because PowerShell determined it would fit better that way or be too crammed in a table. You're the man! The solved my problem. Trying to fumble my way through automating some tasks at work and its amazing how easy PowerShell makes it. A quick google search showed me how simple it is to incorporate regex into the Where-Object command: code:
|
# ? Jun 2, 2015 03:39 |
|
|
# ? Apr 25, 2024 23:09 |
|
So I just finished Powershell in a Month of Lunches a few weeks ago and I think I have the basics of PS down pretty well. Where would someone move next book-wise? For background, I am a helpdesk monkey with some occasional admin stuff that I do.
|
# ? Jun 2, 2015 20:04 |