|
i'm writing a script to only keep the latest 3 files of each file in 1 directory These files are logs which are generated every week. Rhey all start with the <name>.conf<Date> There are 20 different names I foundthis script on the internet that is similar to what I want. But the trouble i'm having is i'm not sure how to define each unique file in this directory Currently the script looks like this. I've now copied this script and changed the filemask name for each other file. code:
I'm not sure how to do that
|
# ? Mar 4, 2016 12:36 |
|
|
# ? May 10, 2024 04:50 |
|
Sefal posted:i'm writing a script to only keep the latest 3 files of each file in 1 directory Simplify simplify simplify. Break the task down into 2 parts. Work against only one name. Find all files with that name $name.conf* Loop over that and parse out the date part and determine if it should stay or go. Delete what needs deleting. Now that you have it working for one file, loop over all of the unique file names and execute the above for each one.
|
# ? Mar 4, 2016 15:45 |
|
Yeah, you can generate a simple array of names:code:
code:
code:
|
# ? Mar 6, 2016 13:19 |
|
Put everything in different folders to do this in 5 seconds If $_.LastWriteTime lines up with your file creation dates, do PHP code:
|
# ? Mar 7, 2016 22:35 |
|
Sefal posted:i'm writing a script to only keep the latest 3 files of each file in 1 directory What format is your date in and are you preferring to determine age by that datecode as opposed to last file written timestamp?
|
# ? Mar 7, 2016 23:04 |
|
GAZE UPON MY WORKS, YE MIGHTY, AND DESPAIR! Using the assumption that the log files will be in <NAME>.confMMDDYY format, like so: code:
PHP code:
code:
|
# ? Mar 7, 2016 23:44 |
|
Trying to wrap my head around when it's appropriate to use parameters and when it's not. I have a script that onboards a new user and takes input like their name, start date, etc. using Read-Host. For stuff like that, is it better to just wrap it all into a param block? When does it make sense to use Read-Host instead?
|
# ? Mar 8, 2016 00:31 |
|
beepsandboops posted:Trying to wrap my head around when it's appropriate to use parameters and when it's not. I have a script that onboards a new user and takes input like their name, start date, etc. using Read-Host. If you ever anticipate another script calling your new employee script, then definitely paramterize. Generally, given the choice parameters will be a bit more extensible than read-host.
|
# ? Mar 8, 2016 00:35 |
|
Wow, Thank you guys. sry for the late response. I was puzzled on how to do the for each. But the examples you guys have given are excellent. I'm going to finish it. today I hope. I feel I have so much to learn in powershell, so I try to do as much scripting as I can. You're advice helps!
|
# ? Mar 8, 2016 14:34 |
|
beepsandboops posted:Trying to wrap my head around when it's appropriate to use parameters and when it's not. I have a script that onboards a new user and takes input like their name, start date, etc. using Read-Host. Not only is param more extensible, you can also just create a prompt with Read-Host in the parameter definition.
|
# ? Mar 8, 2016 15:32 |
|
Sefal posted:Wow, Thank you guys. sry for the late response. I was puzzled on how to do the for each. But the examples you guys have given are excellent. The big part of foreach (aliased as "%" in the other script) is that you can use whatever you want for the "this specific object" variable, and you can reference that specific object with $_ or $specificObjectVariable to do stuff code:
To see what you can do or read from a variable, pipe it to get-member (e.g. "$textFiles | get-member" would show you that you can read $textFiles.LastWriteTime, convert to string with $textFiles.ToString(), etc)
|
# ? Mar 8, 2016 15:52 |
|
Jethro posted:Not only is param more extensible, you can also just create a prompt with Read-Host in the parameter definition. code:
code:
|
# ? Mar 8, 2016 18:15 |
|
beepsandboops posted:Oh, interesting. I was setting them up like: Correct*. In essence, you are setting the default to be the input from Read-Host. *: the syntax is actually [string]$username= $(Read-Host "Please provide the username") Jethro fucked around with this message at 20:27 on Mar 8, 2016 |
# ? Mar 8, 2016 20:23 |
|
I'm creating a new script with parameters but they are not working. I've saved the file as new-user.ps1 and when I run ./new-user.ps1 from the console I expect to the able to tab through the available parameters. Am I missing something painfully obvious here? code:
Swink fucked around with this message at 08:19 on Mar 10, 2016 |
# ? Mar 10, 2016 07:54 |
|
beepsandboops posted:Oh, interesting. I was setting them up like: I thought Read-Host was a bad way to do it. I was told to use something like code:
|
# ? Mar 10, 2016 16:47 |
|
Correct - Read-Host assumes that there is a host to read from, that someone will always be running your script interactively from the console. If your script is called from another script, but someone hosed up and forgot the parameter, the version with Read-Host will hang indefinitely waiting for input that will never come, while the version with mandatory=$true should error out. Similarly, before PowerShell v5 introduced the Information stream, there was no way to log information displayed to the console with Write-Host, while now you can do that like you can with Verbose and Warning messages (see help about_redirection). EDIT: Actually running a test: code:
code:
Venusy fucked around with this message at 14:32 on Mar 11, 2016 |
# ? Mar 11, 2016 14:05 |
|
Going back to Gothmog1065 and his scripting... When it comes to verifying a computer is there, is on, and other things like that, I really try to avoid Test-Connection. I don't trust a ping attempt to either make it or make it back, even if the other side is definitely there. My reasoning goes like this, at least in my network. Other networks would be different, so you do things that fit your situation: 1. ICMP may be blocked either coming or going for various reasons, so to prove that ICMP is valid from one side to the other will probably involve a long chat with Networking. 2. If that machine is Windows and is in the domain, there's certain info I can rely on being able to find. AD Record, DNS record, DHCP record, RADIUS log record, RADIUS connection policy... That's verification outside the machine. Once I have those, then I can start looking at the machine itself. 3. If I'm connecting to a domain joined Windows machine (in my case, that's pretty much true), I can do one of two things: A TCP port test to 135 or a path test to \\compname\Admin$. If I can get a valid connection to either of those, I'm 99% sure. 4. If those ports or path test don't work, I hit it with a TCP 9100, TCP 80, TCP 443, and UDP 161. If it comes back saying yes to 9100 and 161, then I hit it with a specific SNMP request so I can tell what kind of printer it is. I can also ignore this line if I don't care, or focus on this line if I'm doing printer stuff. If I do things right, I can get those tests done before your ping times out, specially if you're using Test-Connection with more than a single attempt, and a single attempt to ping a system is no guarantee it'll respond. The ICMP process is a low priority process, and the ICMP packet also has no guarantee of being transferred along by a router or even a switch since it's pure layer 3 IP which has no mechanics to verify delivery.
|
# ? Mar 11, 2016 22:39 |
|
I'm writing a script that automates an install. Part of this involves certificates, and it's complicated. I have the exact commands, but they involve a lot of quotation marks and environment variables. I can get it working with: pre:$command = "$env:PROGRAMROOT\sbin\program.exe" $arguments =@("pki","new-cert","--n","$env:FQDN",...) & $command $arguments Any better way?
|
# ? Mar 19, 2016 00:22 |
|
Start-Process $command -ArgumentList $arguments? If the problem is with how you populate $arguments, you can do:code:
|
# ? Mar 19, 2016 13:06 |
|
If you're up for some DSC:code:
|
# ? Mar 19, 2016 18:57 |
|
Is there a way to plug in a variable to Invoke-Webrequest? Basically, what I want to do is code:
|
# ? Mar 21, 2016 20:19 |
Yes you can just do string interpolation:PHP code:
|
|
# ? Mar 21, 2016 20:24 |
|
nielsm posted:Yes you can just do string interpolation: Or if it's a property of an object, $result = invoke-webrequest "http://downforeveryoneorjustme.com/$($someObject.site)"
|
# ? Mar 21, 2016 22:03 |
|
Is it possible to use custom colors, ie custom RGB, as a value for write-host's Fourground parameter? The docs say it's an enumeration, but perhaps there's a clever trick to write with my own color?
|
# ? Apr 10, 2016 01:50 |
|
Whee! That was hard work (mainly due to our own inability to read MSDN or apply the correct Google-fu), but we finally did it. I now have an incredibly short PS script that can take the place of Trace32/CMTrace to connect to, read, and output lines from open, active log files. The problem came from this situation: We use NPS as our RADIUS solution for 802.1x. Multiple NPS servers. We've been wanting a way to follow a request from the switch to the first tier, to second tier, to third tier. We tried our damndest to use Get-Content asjldkf -Tail #, but it just wasn't fast enough. I knew if Trace32 could watch an open file and pop lines up as they're written, it could be done. aaaaand...all the code is at work and I can't get to it. I'll put it up here as a generic bit of PS tomorrow! I found a script that someone wrote to turn a NPS log (xml format) into a custom PSObject, so we modified it for our needs and now we can get a stream of PSObjects from a live log and do all the where | select we want to do! Horay for CommandLine.Net!
|
# ? Apr 12, 2016 00:57 |
|
Alrighty, back to business. This is some weakass code, but it works. There's a ton more work to be done for errors, partial answers, converting between different outputs (did you know that DHCP Audit log lines and NPS log lines are separated by different characters? Well, now you do!) code:
code:
The more you sleep, the bigger the chunk of output you have to deal with, but it seems to affect both machines less to grab every half second or second or two, depending on the speed of the information being written.
|
# ? Apr 12, 2016 19:43 |
|
I signed myself up for some powershell scripting that I have no idea how to do, and my precursory one hour of blind attempts is not going well. I'm tasked with auditing our file server, which is split into 2 tasks. One is to get the file count and overall size, and the other is to report on permissions. I've got the basics on how to do both of these with parent level folders. The trick is that I need to report on folders 1 level deep in each directory. For example, the structure is Departments > Technology > Target Subfolders. I've resigned to manually targeting each department's folder instead of aiming at "Departments" and going 2 levels deep. If "Get-ChildItem" is the right command to use, which I believe best suits the purpose, how do I design "-Recurse" to stop at 1 sublevel? For reference this is what I have so far and obviously it does not work. The recurse inside the "forEach" recurse hangs after the first folder, and $size is resolved to "Microsoft.PowerShell.Commands.GenericMeasureInfo". I'm not worried so much about fixing this exact script because I know it will need a total overhaul to work, but I'm really interested in an intuitive way to do that 1 sublevel recurse. code:
Judge Schnoopy fucked around with this message at 14:43 on Apr 13, 2016 |
# ? Apr 13, 2016 14:41 |
|
Measure-Object doesn't return the sum, it returns an object that has a Count, Sum, Average, etc. properties, and the ones you specify when you call the cmdlet are the ones that are populated (Count is always populated). This means that you can get the file size and file count in one pass.code:
I don't think you can recurse a set number of levels, so if you want to do all the department folders at once, you just have to put another ForEach around the existing one. code:
|
# ? Apr 13, 2016 16:00 |
|
On Windows 10, Get-ChildItem has a Depth parameter, but I don't think that's available on Windows 7 or Windows 8 even with PSv5 installed.
|
# ? Apr 13, 2016 16:42 |
|
Jethro posted:Measure-Object doesn't return the sum, it returns an object that has a Count, Sum, Average, etc. properties, and the ones you specify when you call the cmdlet are the ones that are populated (Count is always populated). This means that you can get the file size and file count in one pass. Thank you for clarifying the Measure-Object cmdlet. The idea of getting this info in one go instead of running get-childitem twice is amazing since I'm looking at about a million files total in $root. I was able to get the rest of the script working besides size, and you helped me clean up the count output as well. However! I'm getting warnings when running this script because empty folders return lengths of "null", not 0. This doesn't stop the script and I added a quick "+0" to help format my output data, so no big deal, but I dislike seeing red blocks of text in my powershell window. Any way around that, maybe an error suppression command, or is it better to just leave it be and ignore? New script by the way which works, with progress bars / information! code:
Judge Schnoopy fucked around with this message at 17:39 on Apr 13, 2016 |
# ? Apr 13, 2016 17:36 |
|
Judge Schnoopy posted:Thank you for clarifying the Measure-Object cmdlet. The idea of getting this info in one go instead of running get-childitem twice is amazing since I'm looking at about a million files total in $root. This script is going to not look for any files in level 0 or 1, as it just looks for files in the $root/$folder/$sub level. To do that you'll need to add the stuff under sub to each level above. As for the errors, you can add -erroraction SilentlyContinue to the offending line, but it'd be better to add Try/Catch with error logging as you'll want to see why it doesn't work like you think it should.
|
# ? Apr 13, 2016 19:32 |
|
Venusy posted:On Windows 10, Get-ChildItem has a Depth parameter, but I don't think that's available on Windows 7 or Windows 8 even with PSv5 installed. Get-ChildItem -Depth is available on Windows 7, used it the other day to figure out which folder someone had moved their folder into.
|
# ? Apr 14, 2016 02:38 |
|
So it is, for some reason it wasn't showing up in tab completion for me.
|
# ? Apr 14, 2016 10:50 |
|
sloshmonger posted:This script is going to not look for any files in level 0 or 1, as it just looks for files in the $root/$folder/$sub level. To do that you'll need to add the stuff under sub to each level above. This worked fine, level 0 is the root target and level 1 is just a folder directory with $n folders in it, no files. I originally had the same meat of the script copied in at the level 1 get-childitem and was going to run the script on $n folders individually. This way I was able to point at the root one time and have everything complete for me.
|
# ? Apr 14, 2016 20:05 |
|
Some sweet Done Jones action from the Powershell Summit thing https://www.youtube.com/watch?v=playlist There's like 30 videos on all kinds of topics. The forums arent letting me post the link to the playlist so here's a single video. You can find your way from there. https://www.youtube.com/watch?v=KprrLkjPq_c Swink fucked around with this message at 01:58 on Apr 16, 2016 |
# ? Apr 16, 2016 01:56 |
|
I want to rename a boatload of files with powershell because I'm anal about my music/movie/tvshow collection. These files have periods instead of spaces between stuff, like The.Two.Towers etc. If I wildcard the names by doing something like *.*.*.* and tell it to replace period with a space, will it replace the period before the filename as well? I haven't written anything out yet or I'd paste some super butchered code.
|
# ? Apr 18, 2016 02:02 |
|
I would do something likephp:<? $safety = 0 $JamesRegex = $regex $files = $filepath $filecount = 0 function renamefile{ # code goes here return $newfilename } foreach($file in $files){ if($file -match $JamesRegex){ Write-Host $file Write-Host "New file name is" $(renamefile) if($safety -ge 1){ rename-file $file $(renamefile) } $filecount++ } Write-Host "$filecount Files were renamed." ?>
|
# ? Apr 18, 2016 04:23 |
|
Using -whatif is a great idea as well. You can avoid making a bad mistake that way.
|
# ? Apr 18, 2016 06:16 |
|
When you use Get-Item on a file (or Get-ChildItem, the PS equivalent of dir), the resulting object(s) will have a BaseName property, which is the filename without the extension. You can use that to make sure that you just grab the right part of the name:code:
code:
|
# ? Apr 18, 2016 16:17 |
|
|
# ? May 10, 2024 04:50 |
|
Thanks guys! Yeah I was going to -whatif whatever I came up with, but sometimes -whatif is a bit... unhelpful and/or misleading depending on the command you're using.
|
# ? Apr 18, 2016 18:06 |