|
So I've been using powershell for the last 2 years and I've been getting progressively better at it. Helped that Exchange 2010 had a major bug that prevent you from using the console if you authenticated to the machine with creds in a cross forest (The console is built on top of powershell and up until RU6 for Exchange 2010, the console didn't know to check cross forest DCs for authentication). I've written fairly simple scripts to perform certain functions like MRM policy migrations that perform a pre and post migration report. code:
code:
code:
|
# ? Jan 5, 2012 16:24 |
|
|
# ? May 21, 2024 17:46 |
|
The {} are there, because a function can contain multiple lines, and PS needs a way to delimit this.code:
code:
code:
code:
code:
Jelmylicious fucked around with this message at 17:23 on Jan 5, 2012 |
# ? Jan 5, 2012 17:13 |
|
Mario posted:What if the 1st is on a Monday? This would match Tuesday and Wednesday as well. Awesome, I was able to make that work with 99% accuracy. Actually all of that Saturday/Sunday stuff I don't even need since the folders are only created on Weekdays. If I wanted to return a list of folders, but exclude any created in the previous month, how could I do that? I know I can filter my results with something like code:
Any ideas? Wicaeed fucked around with this message at 18:12 on Jan 5, 2012 |
# ? Jan 5, 2012 17:30 |
|
Jelmylicious posted:Thanks, that's probably the best explanation I've received thus far about this. I kind of knew it was for an organizational purpose, but for some reason it just wasn't clicking properly.
|
# ? Jan 5, 2012 19:13 |
|
Wicaeed posted:Awesome, I was able to make that work with 99% accuracy. Actually all of that Saturday/Sunday stuff I don't even need since the folders are only created on Weekdays. code:
|
# ? Jan 5, 2012 21:19 |
|
i barely GNU her! posted:
When you run this in januari, it will filter out anything made in december of any year. So this doesn't work as asked, when you span multiple years. Also, it doesnt filter out the current month (which I also needs to be filtered out). e: And that is exactly what he asked, but I already made this, so here you go, getting to first day of that month: code:
code:
code:
FirstOfMonth(1) will give you: donderdag 1 december 2011 0:00:00 (Locale is set to Dutch, but you get the gist). If anyone is interested in forcing the locale so you can read the date, no matter what language the server is set to, let me know. (Very important if you work with the currency object, because it would display as 100 dollars, or pounds, or Rupee depending on the locale, without converting the currency) Jelmylicious fucked around with this message at 17:01 on Jan 6, 2012 |
# ? Jan 6, 2012 09:56 |
|
Hmm good point. Is there any way to format the value of the file/folder CreationTime to match a get-date format of Myyyy (January 2012 would be 12012, November 2011 would be 112011, and so on). I honestly don't really need the date, I'm just filtering out files the current and prior month, the date filtering comes later: http://pastebin.com/WqUp6gxF Edit: Actually I came across a realization. I don't need to filter on the first weekday of the month, but the first Friday of the month, and this makes it much easier simple because The year doesn't matter, because at the point where this script has been running that long I will already have the only backup I want for that time period (the first Friday in the month), and the first Friday will always fall within the first 7 days of the month. code:
Tell me I'm not wrong in my assumption Wicaeed fucked around with this message at 18:21 on Jan 6, 2012 |
# ? Jan 6, 2012 17:09 |
|
Rather than a giant megapost of questions for the current script I'm working on, I'll ask bit-by-bit () instead: Need some help with regular expressions. I am trying to match large integers that are formatted with commas every three numbers in a string. So there's anything from 24 all the way up to 34,127 or 156,870,240 and beyond. What would a [regex] matching rule look like to account for potentially "n" instances of \d{1,3}\, ending with \d{1,3} is what I'm thinking the question is.
|
# ? Jan 9, 2012 06:34 |
|
How about \d{1,3}(\,\d{3})* 1 to 3 digits at the start, followed optionally by any number of ,ddd groups.
|
# ? Jan 9, 2012 07:52 |
|
Looks good, but do sub-expressions work inside of other sub-expressions? So I would ultimately have: [regex]"(?<Amount>\d{1,3}(\,\d{3})*)" edit: Removing instances of 134,04 and the like! edit2: Crap, is ?<Amount> a Powershell-only syntax for use with -match, and doesn't work inside of .NET [regex]'s? Siets fucked around with this message at 17:26 on Jan 9, 2012 |
# ? Jan 9, 2012 16:42 |
|
Siets posted:Looks good, but do sub-expressions work inside of other sub-expressions? So I would ultimately have:
|
# ? Jan 9, 2012 17:57 |
|
Jethro posted:That's the standard .NET syntax for a named capture group, not just PowerShell. Is it giving you problems? Yeah, I'm really bad at this. Alright, let me expand a little on what I'm trying to do. I'm pretty good at AD filesystem Powershell stuff, but regular expressions are new ground for me. I essentially have a table that I am trying to parse and keep the relationship between certain dates and the values that I find on those dates. So the table is like this: code:
Would a fancy regex handle all this logic, or do I need to delve into setting up my own array variables and a bunch of rules to keep the Value(X) data related to the Date it was found next to? Thanks for the help guys. I love this thread.
|
# ? Jan 9, 2012 18:08 |
|
It seems to me that it would make a lot more sense to import this into a database of some sort (assuming it's not already in one) or Excel or something, since those tools are more or less designed to do this sort of thing. If you really need to do this all in Powershell, I'd suggest you start by creating objects and adding properties see here.
|
# ? Jan 9, 2012 19:13 |
|
http://gskinner.com/RegExr/ is a great site if you're building regex's, but Jethro is right -- you probably want to build your own custom objects.
|
# ? Jan 9, 2012 19:38 |
|
^^ That is incredibly handy! I'm pulling around ~1,800 queries of HTML page source, each from a different URL (which takes time to load). I timed it out at around 100 seconds each query for me to manually copy it down and format it to be usable, which means... well... a lot of manual labor unless Powershell does all of that for me. I think it is going to come down to creating objects to hold the arrays for later processing. The pseudocode is basically: While (there is another row) { Find and store a line based on a RegEx which finds everything between <tr></tr> Parse data out of each line by splitting the array at each <td></td>'s Store the parsed data in a new object Type the data } ...but I'm not really sure exactly how all this would look in Powershell. For instance, how would I set up a flag condition to end the While after the last <tr> line was found? Siets fucked around with this message at 19:49 on Jan 9, 2012 |
# ? Jan 9, 2012 19:44 |
|
Quick question, made harder because I don't know what the actual name is for it: I'm using the following script to get the Month number (1 = January, etc) so that I can include/exclude files from backup based on if they are the current are prior months, without having to worry about the date. code:
I'm trying to do something similar for whatever a files creation time was for the week , however I can't find a way get the date/time in that format with the pipeline object $_.CreationTime. I've been reading this Powershell Blog on how the author goes about adding a membertype to the get-date method: http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/08/use-powershell-to-get-the-number-of-the-week-of-the-year.aspx I was wondering if you could do something similar code:
Can you manually add a method to do that type of math? I hope I'm making sense, I'm still pretty new to this and a lot of the terminology isn't there
|
# ? Jan 11, 2012 17:57 |
|
The Get-Date cmdlet can take a date, so something like [int](Get-Date -UFormat %V -Date $_.CreationTime) would get you what you want. Add-Member wouldn't really help you here, because it only adds the member to a particular object instance. Jethro fucked around with this message at 18:54 on Jan 11, 2012 |
# ? Jan 11, 2012 18:44 |
|
I had no idea there was a PowerShell thread, heh. I think I might've posted this over in the General Porgramming thread but didn't get too much insight. I'm a lowly intern whose never done any sort of scripting in my life, but I need to learn and quick. I have ~2,200 different files in different folders. Based upon the folder a file is in, I need to attach a certain prefix to the file name. For example, I have a bunch of documents that have a company's name, and I need add EAA or SCA before the company name. I've scoured Google for various renaming scripts, but I can't make heads from tails from what I'm looking at to alter it to fit my needs. I also can't seem to find any remotely decent tutorial sites, either. Any goons have any recommendations?
|
# ? Jan 12, 2012 16:39 |
|
Conceptually Sab you'd want to do something like this. Create a hashtable or something with a key/value of folder + prefix iterate through your folders/files if they match your hash table rename-item with new prefix So as an example let's say I have two folders in my C:\temp - Something and Awful: Inside each folder I have a single text document. Code wise I'm going to say I want to prefix everything in the something folder with xxx and everything in the awful folder with yyy. So here's my hashtable (for more on hashtables check out this hey scripting guy post: http://blogs.technet.com/b/heyscriptingguy/archive/2011/10/15/automatically-create-a-powershell-hash-table-10-15-11.aspx): code:
code:
adaz fucked around with this message at 18:04 on Jan 12, 2012 |
# ? Jan 12, 2012 17:48 |
|
Interesting, thank you for the indepth response.
|
# ? Jan 12, 2012 18:24 |
|
Adaz -- I know this is from like a month ago but thanks for pointing out "trace-command". I'm actually working on writing a Module in C# right now to act as a CLI for some software I work with, and that is going to save me a lot of headache.
|
# ? Jan 15, 2012 06:47 |
|
You can switch the SQL function in the OP to use PowerShell 2.0 parameter syntax: <# .SYNOPSIS Fill a datatable from a SQL DB .DESCRIPTION Connects to a given SQL server and fills a datatable .PARAMETER ServerName The SQL server to connect to .PARAMETER DatabaseName The database to connect to .PARAMETER Query The query to run .EXAMPLE get-datatable -ServerName myserver -DatabaseName TestDB -Query "Select * from TestTable" .NOTES Additional information about the function goes here. .LINK about_functions_advanced #> param( [parameter(Mandatory=$true)] [string] $ServerName, [parameter(Mandatory=$true)] [string] $DatabaseName, [parameter(Mandatory=$true)] [string] $Query ) That way instead of getting an exception when you launch the script you'll get a prompt for the parameter. Makes it easier when the user doesn't know the parameters. If you add the help block in front of it users can run Get-Help for your script as well. Loves me some PowerShell. Thanks for the thread. Good stuff here.
|
# ? Jan 23, 2012 05:42 |
|
Working on teaching myself PowerShell to use at work, still VERY new to it so I'm going to guess this has a simple answer. However my Google skills are failing me on it. What is the PowerShell equivalent of %~dp0? For example if I want to Start-Process something but use the full path of wherever it is located in the file and any apply the same to anything in arguments. An simple example of an old .bat that I'm working with might be - MSIEXEC.EXE /i "%~dpProgram.msi" TRANSFORMS="%~dp0Program.mst" /qb I've learned how to do almost all of that simply in PowerShell, just getting caught up on the path. *edit* Hm, I think I got it right after posting this of course. Seems to work, any issues with this? - code:
Thrawn200 fucked around with this message at 21:56 on Jan 23, 2012 |
# ? Jan 23, 2012 21:29 |
|
Nope that is a fine way to do it, you can also bind the arguments to a variable and pass it that way as well, or quote everything in string literals - a single quote ' ' - and do it that way. ThatSlacker, you're right I should re-write all those in V2 style advanced functions, as I've actually done that for a few of them already. I'll finish that up soon
|
# ? Jan 24, 2012 04:43 |
|
adaz posted:Nope that is a fine way to do it, you can also bind the arguments to a variable and pass it that way as well, or quote everything in string literals - a single quote ' ' - and do it that way. Well, after looking some more, it mostly works. I can't seem to get the transform to apply to the install no matter what way I write it. Doesn't seem to be a lot of documentation for doing silent installs using PowerShell around. *edit* Bah, I need to be able to delete my posts. That's twice I got stuck on something for like an hour plus only to figure it out seconds after I post about it. I just needed to use ` on the quotes around the transform file name. Thrawn200 fucked around with this message at 17:18 on Jan 24, 2012 |
# ? Jan 24, 2012 17:07 |
|
Is there any hope of me understanding this moonspeak if I "think" in Python already? This is mostly just indecipherable garbage, and it didn't help that by default PowerShell won't run PowerShell scripts I'm trying to run a command on a bunch of files in a directory, this is the command, as outputed by the mkvmerge gui: quote:"C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" -o "C:\\Users\\me\\Desktop\\bad folder\\file.mkv" "--default-track" "0:yes" "--forced-track" "0:no" "--display-dimensions" "0:1280x720" "--language" "2:eng" "--default-track" "2:yes" "--forced-track" "2:no" "-a" "2" "-d" "0" "-S" "-T" "--no-global-tags" "--no-chapters" "C:\\Users\\me\\Desktop\\good folder\\file.mkv" "--track-order" "0:0,0:2" It's big, and it's nasty, and for some reason it's got double quotes puked out all over it. What I'm doing is removing a language track from each mkv, then outputing it to a new directory with the same name. I could write a script to generate all 24 mkvmerge commands in Python in the time it's taken me to try and figure this out, but since I guess I'm a Windows admin I should at least try and figure this out the "right" way?
|
# ? Jan 28, 2012 10:08 |
|
String Literals are your friend ' ' they will cause powershell to ignore all that mess and just pass the entire thing where it needs to go. Otherwise you can use another " to escape out the quotations and ` to escape out the -- but I wouldn't advise it
adaz fucked around with this message at 22:19 on Jan 28, 2012 |
# ? Jan 28, 2012 20:55 |
|
I'm looking for reviewers for the book Powershell in Depth. Please see this thread for details: http://forums.somethingawful.com/showthread.php?threadid=3463489
|
# ? Jan 29, 2012 23:04 |
|
FISHMANPET posted:Is there any hope of me understanding this moonspeak if I "think" in Python already? This is mostly just indecipherable garbage, and it didn't help that by default PowerShell won't run PowerShell scripts Surely Powershell is more analogous to bash than to Python.
|
# ? Jan 31, 2012 16:18 |
|
So I know you can use Get-Member to determine what all possible methods there are based on the object you are working with. Often, what is returned though are a bunch of cryptic methods that aren't exactly clear on how you use them. What is a good way to determine how to use a method or how it works when I find out what is available to me? (Other than Googling of course?)
|
# ? Jan 31, 2012 18:28 |
|
Can you give an example siets? The best bet is to find out the type of the object (listed at the top when you do | get-member or calling GetType() on the object) and look it up on MSDN. So for instance code:
However as an example if I look at the CopyTo method on that class (http://msdn.microsoft.com/en-us/library/5axsfwbc.aspx) I can see it takes String for the first parameter as the file location to copy to, and a boolean as the second value indicating if I should over write it or not. Don't worry too much about the syntax presented there as it'll be in VB/C# but look instead into what exactly each parameter each method, etc is taking and what they do. As another example, look at equals on the fileInfo class (http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx). Equals never works the way people think it does, it's usually reference equality not value equality and that page does a fair to middling job of explaining that. Note also that powershell 2.0 is using the 2.0 .NET Framework I believe unless you force it to the 4 framework (see: http://stackoverflow.com/questions/2094694/how-can-i-run-powershell-with-the-net-4-runtime) so make sure you have the correct one selected at the top of the page adaz fucked around with this message at 18:49 on Jan 31, 2012 |
# ? Jan 31, 2012 18:45 |
|
Ok thanks! I just know that in order to get the maximum benefit out of a object-based tool, you have to be able to leverage the methods available to you. Seems pretty obvious, but for as nice as Powershell makes it to learn about your object, it sorta stops there. I'm trying to chop up some HTML to get to a number in a column, then add that to a variable. Here's one row of many rows exactly like it code:
I feel like string manipulation should be easier. Maybe I'm making this too hard in my own head? e: Ack, sorry for table-breaking. I can fix that if need be.
|
# ? Jan 31, 2012 19:14 |
|
There are a lot of different ways of doing this,but I like regular expressions because they are sweet. I'm going to use the sweet RegExr website to generate the following regex http://regexr.com?2vsgn and replace all that junk with * or something. As an example: code:
So I assume you have this entire webpage in some variable, we'll call it $webPage. Now this is again just one way to do it but I would replace all that junk with *, iterate through each line, run a regex match, split the line on ** and grab the value from the new array based on the split. You could make it much fancier by loading each line into its own custom object, and adding to a multidimensional array as a series of properties, and doing the math off that. But if it's a one time thing and the data is consistent I probably wouldn't bother If you expect possible blank values or some rows might not have same number of columns you will need to tweak the logic. code:
adaz fucked around with this message at 19:54 on Jan 31, 2012 |
# ? Jan 31, 2012 19:50 |
|
Wow, thanks so much for this. Is $line a predefined variable, or something you just threw in so the foreach would work? I know Powershell is smart enough to let you both define and initialize the variable in the foreach, but I'm just hazy on how that code snippet is doing what it's doing. There's other HTML on the page that I omitted, and I'm just curious how $line knows its way around all that other junk when doing -match. Also, if I want to make multiple -replace changes to $strTest, how would I go about doing that? Powershell keeps throwing errors when I try to pipe the resultant string after the first replace to another replace. Basically, I tried: code:
Siets fucked around with this message at 20:48 on Jan 31, 2012 |
# ? Jan 31, 2012 20:43 |
|
$line is just the iteration variable I picked. You can call it whatever you want, it's the unique variable for each run through the loop. If you want to see what it contains do a code:
adaz fucked around with this message at 22:25 on Jan 31, 2012 |
# ? Jan 31, 2012 22:22 |
|
What is the most simple way to save the result of each iteration of a Foreach loop? For example I have the following loop: code:
Right now the write-host lines are there simply so I can see what is going on at the command line. I don't quite understand hash tables or arrays yet, which is what I think I need to properly get this working. Any tips?
|
# ? Feb 2, 2012 15:05 |
|
So, two stupid things I'd like to learn how to do. Honestly, at this point, I'd like to know if they're even possible. I can (hopefully) figure out the actual mechanics. I've only ever had experience scripting with batch files, so for now, I'd like to just use those. If not, hey, always good to learn something new, right? 1) Is there a way to specify a file by the Date Modified column? I basically want to have a script that auto-deletes the oldest file in a certain folder when I run it. 2) How do you check to see if a program is already running? I'm trying to write a script that checks to see if a couple different programs are running, and if not, to start them. Like I said, I've only ever used simple batch files. If it can't be done with those, oh well. Guess I'd better learn some languages, hahaha. Thanks in advance, goons.
|
# ? Feb 2, 2012 15:26 |
|
Walter_Sobchak posted:So, two stupid things I'd like to learn how to do. Honestly, at this point, I'd like to know if they're even possible. I can (hopefully) figure out the actual mechanics. I've only ever had experience scripting with batch files, so for now, I'd like to just use those. If not, hey, always good to learn something new, right? Yay I finally get to help someone! I'm doing pretty much this exact thing with a backup script You can sort objects oldest to newest by creationtime with the following: code:
code:
Wicaeed fucked around with this message at 16:05 on Feb 2, 2012 |
# ? Feb 2, 2012 15:52 |
|
Wicaeed posted:
There are quite a few ways of doing this. You can absolutely use an array or hashtable, but since this is just going to be outputted to an email as a string anyway I'm going to use StringBuilder (http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx) to store the results and then another function to send yourself an email. So, first things first we create our StringBuidler code:
code:
code:
adaz fucked around with this message at 16:20 on Feb 2, 2012 |
# ? Feb 2, 2012 16:18 |
|
|
# ? May 21, 2024 17:46 |
|
Since you seem interested in finding stuff out for yourself, let met introduce you to get-command, get-help and get-memberWalter_Sobchak posted:1) Is there a way to specify a file by the Date Modified column? I basically want to have a script that auto-deletes the oldest file in a certain folder when I run it. You are working with files, command for that is get-childitem (or ls for short). Since you want to know what prperties you get with this, you pipe it to get-member (or gm for short): code:
CreationTime Property System.DateTime CreationTime {get;set;} So yes, it is possible. To get the oldest file, try something like this: code:
Walter_Sobchak posted:2) How do you check to see if a program is already running? I'm trying to write a script that checks to see if a couple different programs are running, and if not, to start them. For this, you are looking for a process that is running. Let's see if powershell has any functions with process in the name, using get-command (gcm for short) code:
code:
code:
get-process explorer returns: code:
returns a big red error. So I am not running, it seems. But we don't want that error. So, let's pipe to a filter: code:
code:
E: The sort option two posts before me is more elegant, I was too hung up on explaining how to find info... Jelmylicious fucked around with this message at 08:38 on Feb 3, 2012 |
# ? Feb 2, 2012 16:23 |