|
adaz posted:
As it turns out we're running Exch2010 here, but our DCs are still running bloody 2003 so I can't run the script.
|
# ? Dec 21, 2011 00:13 |
|
|
# ? May 21, 2024 14:11 |
|
psylent posted:Thanks so much for this, really appreciated. Didn't realize the cmd-lets required 2008 R2 to be running on the servers or that functionality level... but even if they do you can still run the script just need to use the root .NET classes (see this is why I don't trust the dumb modules, they are never in the place you need them) code:
adaz fucked around with this message at 00:28 on Dec 21, 2011 |
# ? Dec 21, 2011 00:26 |
|
Looking for a quick pointer in the right direction. Had some success last night using powershell to do some bulk file renaming at home. Get into work today, now I actually get to try and use some of that! I would like to try and hack through alot of this on my own, but currently I am trying to take a directory with some files in it, then go through and create a folder in that directory with the name of each file name. Example would be I currently have c:\ps which contains a.txt b.txt c.txt I need to then create 3 folders, c:\ps\a, c:\ps\b, c:\ps\d Once that is created, I need to move the respective txt files into the respective folders. Currently hung on creating the folders by the file names. I was trying something along the lines of code:
|
# ? Dec 21, 2011 16:07 |
|
More loving noobie questions: I am rewriting a vbscript script that we currently use to work around the horrible Windows Server 2008/R2 backup feature. Right now part of the vbscript looks as such: code:
code:
|
# ? Dec 21, 2011 16:16 |
|
Moey posted:Looking for a quick pointer in the right direction. Had some success last night using powershell to do some bulk file renaming at home. Get into work today, now I actually get to try and use some of that! Well, when creating a folder manually in explorer with the same name as a file, it asks me if I want to overwrite. You could chop off the extension (or add a .dir extension or something) to differentiate the name.
|
# ? Dec 21, 2011 16:20 |
|
Moey posted:
You were very close -- just need the basename (which omits the file extension) and to put the piepline input in quotes so powershell expands it before attempting to pass it to the parameter - which results in a null being passed and the new-item trying to create the root directory over again code:
adaz fucked around with this message at 16:34 on Dec 21, 2011 |
# ? Dec 21, 2011 16:32 |
|
Wicaeed posted:Always returns "Drive backup does not exists". Is there some secret I'm missing to using variables and boolean operators in functions? This is a problem of scope, your Variable gets set inside the function, but doesn't exist outside it. You could try three things: you could move the Write-Host inside the function, you could not use a function, but have it in the main executing code, or you could have the function return it: code:
Also, since Test-Path already returns a boolean, you could omit the -ne $False, so, just write: if (Test-Path "C:\dell"). But, that is more of a style choice, just keep it readable for you and your colleagues. Jelmylicious fucked around with this message at 16:44 on Dec 21, 2011 |
# ? Dec 21, 2011 16:35 |
|
Okay, thanks for that clarification. The reason the vbscript is run like it is, is because that the results are later send via email, and from what you said regarding functions, you can't actually store the results as a variable that exists outside the function? I was under the impression that Functions functioned () somewhat like subroutes in vbscript. Is that not the case? If I DID want to store the results to call back on, but keep it inside the function, is that even possible?
|
# ? Dec 21, 2011 16:43 |
|
Wicaeed posted:Okay, thanks for that clarification. The reason the vbscript is run like it is, is because that the results are later send via email, and from what you said regarding functions, you can't actually store the results as a variable that exists outside the function? That is a fourth possibility, make it a global variable, by starting your variable with $global: code:
|
# ? Dec 21, 2011 16:48 |
|
adaz posted:You were very close -- just need the basename (which omits the file extension) and to put the piepline input in quotes so powershell expands it before attempting to pass it to the parameter - which results in a null being passed and the new-item trying to create the root directory over again Didn't know that name would grab the extension as well. I am still having a problem as $_ isn't pulling files from that directory. This script will just create a folder called .basement, then error out for the rest of the items.
|
# ? Dec 21, 2011 16:53 |
|
Moey posted:Didn't know that name would grab the extension as well. I am still having a problem as $_ isn't pulling files from that directory. This script will just create a folder called .basement, then error out for the rest of the items. Are you running it from the folder? Otherwise use GCI C:\PS (don't forget to add it to the beginning of your new foldername to).
|
# ? Dec 21, 2011 16:55 |
|
Sweet. HOWEVER, I have to be setting my function incorrectly, or something. When I call my function it returns a blank result, but if I comment out the function part of it so I'm left with:code:
Any ideas?
|
# ? Dec 21, 2011 16:57 |
|
Wicaeed posted:Sweet. HOWEVER, I have to be setting my function incorrectly, or something. Any ideas? You are not calling you function: code:
code:
|
# ? Dec 21, 2011 17:03 |
|
Yeah, now it works. Thanks a bunch
|
# ? Dec 21, 2011 17:05 |
|
Moey posted:Didn't know that name would grab the extension as well. I am still having a problem as $_ isn't pulling files from that directory. This script will just create a folder called .basement, then error out for the rest of the items. Yeah sorry it was early in the morning, going to blame that instead of me just being dumb. Anyway what seems to be happening for some reason is the pipeline object is null (or at least New-Item thinks it is null). I don't know why this is the case and it's kind of pissing me off. Looking into it. $blah is a get-childitem of the current directory. Correct: Being Stupid: adaz fucked around with this message at 18:20 on Dec 21, 2011 |
# ? Dec 21, 2011 18:18 |
|
Looks like you're forgetting your foreach:code:
|
# ? Dec 21, 2011 18:55 |
|
e: Like a million edits but no you shouldn't need the foreach there because new-item should accept pipeline input on its parameters (and does according to the documentation)
adaz fucked around with this message at 19:07 on Dec 21, 2011 |
# ? Dec 21, 2011 18:59 |
|
Trace-Command is our friend. It's because New-Item uses PropertyBinding (http://technet.microsoft.com/en-us/library/dd347566.aspx) on its parameters instead of value binding. Basically with property binding if you're piping an object to that cmd-let it's going to bind its parameters to the same property name on the object that's being passed. So if it takes name as one of its parameters it's going to bind to name on the object no matter what you tell it to do. If the object doesn't have that property it'll actually error out on you (see the full explanation in get-help about_pipeline). So in this case what we're seeing is New-Item is taking the name of the file instead of the basename and erroring out because the file exists, obviously. See here the trace (you can do this yourself by doing: code:
The reason Greth's solution worked is because we're inserting for-each cmd-let into the equation and New-Item no longer thinks it's in the pipeline and doesn't try to bind its parameters to the object being passed automatically. adaz fucked around with this message at 20:36 on Dec 21, 2011 |
# ? Dec 21, 2011 19:18 |
|
Thanks guys, that worked perfect. Now I am stuck on moving the files into the newly created folders. I am trying to do something like this, but it is clearly wrong. code:
Edit If I add in the full path, it will just renames the files without moving them. Both the lines below dont move anything, but rename both the files and the folders (appends a .basename to the end). Don't know why powershell is so difficult for me to grasp. code:
Moey fucked around with this message at 21:29 on Dec 21, 2011 |
# ? Dec 21, 2011 21:24 |
|
This is another odd question. Is it possible to use Powershell to remove al sentences flagged by the Microsoft Word Grammar Checker? Or delete all sentences not flagged by Word's grammar checker? I'm checking the Word Object Model (http://msdn.microsoft.com/en-us/library/bb225019(v=office.12).aspx) but can't really find anything that returns if a sentence is flagged by Word's grammar checker. edit: Found something real interesting! http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.range.gotonext.aspx or more specifically http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdgotoitem.aspx Apparently the Range Interface provides a GoToNext method which contains a way to move the pointer to a grammar area. Time to mess with Powershell! edit edit: Found a way to find a grammatical incorrect sentence and I can delete it. The problem now is looping through the document. I can't seem to find a boolean value in the Office interop that returns true or false if the document still contains grammar errors... Here's what I got so far code:
Rabid Snake fucked around with this message at 23:27 on Dec 21, 2011 |
# ? Dec 21, 2011 22:23 |
|
You are formatting the destination part wrong at first glance. Should be something like thiscode:
|
# ? Dec 21, 2011 23:08 |
|
adaz posted:You are formatting the destination part wrong at first glance. Should be something like this Makes sense and works! It spits out errors but still moves the files. I think it is trying to move the folders as well. If I run it on a directory with 1 file, I get 1 error. 3 files, 3 errors. Don't think it matters, not sure though.
|
# ? Dec 22, 2011 02:30 |
|
Yep it's the directories. If you wanted to filter them out instead of getting a bunch of random errors you can pipe Get-ChildItem to Where-Object {$_.psISContainer -eq $false} before you pass it to your for-each loop. Rabid Snake not sure I have to look at the office interop stuff for a word project i'm working on I'll see if I spot anything there tomorrow. adaz fucked around with this message at 04:30 on Dec 22, 2011 |
# ? Dec 22, 2011 04:22 |
|
So what's the proper way to go about getting folder paths with spaces to parse properly in powershell? Example: code:
code:
code:
|
# ? Dec 22, 2011 18:37 |
|
Single quotes ' are for string-literals which powershell won't try and expand -- see http://technet.microsoft.com/en-us/library/ee692790.aspx
|
# ? Dec 22, 2011 18:50 |
|
code:
|
# ? Dec 22, 2011 19:18 |
|
Woop woop. Ran it today on the production data and everything worked. Could not get the moving to ignore folders, so when it hit that line, I had 1350 errors scrolling across my PS window, but it all worked out.code:
Edit: Just out of curiosity sake, how would I add "where-object {$_.psiscontainer -eq false}" to my last line of code there? Everything I try breaks it. Moey fucked around with this message at 17:26 on Dec 23, 2011 |
# ? Dec 23, 2011 16:52 |
|
adaz posted:Rabid Snake not sure I have to look at the office interop stuff for a word project i'm working on I'll see if I spot anything there tomorrow. Found one! If someone in the near future, for some odd reason, has the same problem I had, I just used the GrammaticalErrors Property. msdn.microsoft.com/en-us/library/aa213190(v=office.11).aspx So my while loop looked like this code:
|
# ? Dec 26, 2011 16:15 |
|
Okay, so I have this stupidly large function I am using to verify my backup results. A previous function moves/renames the Windowsimagebackup folder to a network share, then this function verifies that that path exists, and if so removes the backup folder from the local drive. Heres the code: code:
What's the right way to debug a function, if you have to load the function into memory before you can even run it later with the function name?
|
# ? Dec 27, 2011 15:38 |
|
Trace-Command (http://technet.microsoft.com/en-us/library/dd315284.aspx) is what you'd want to use it is basically the powershell debugger. By "second statement in the codeblock fails" do you mean code:
|
# ? Dec 27, 2011 17:29 |
|
I'm new to Powershell, so please forgive me if this is a stupid question. At my new job our AD forest is 2003 native. However, some of our DCs are running on 2008 R2. Is it at all possible to do AD administrative tasks (e.g. user creation/modification) with Powershell in our forest, or does it need to be 2008 native first?
|
# ? Jan 3, 2012 16:41 |
|
Moey posted:
code:
|
# ? Jan 3, 2012 17:39 |
|
Powdered Toast Man posted:I'm new to Powershell, so please forgive me if this is a stupid question. I believe only 1 DC has to be running 2008 R2 for it to work right. You also can just use the raw .NET classes (system.directoryservices) instead of the Active-Directory Modules. Which isn't that big of a deal, they are pretty easy to use and the classes/examples are well documented. I believe quest software or someone like that makes a AD module that is just a frontend for the .NET system.directory classes for free that works without 2008 R2. I haven't used it myself.
|
# ? Jan 4, 2012 02:40 |
|
adaz posted:I believe only 1 DC has to be running 2008 R2 for it to work right. You also can just use the raw .NET classes (system.directoryservices) instead of the Active-Directory Modules. Which isn't that big of a deal, they are pretty easy to use and the classes/examples are well documented. Ok...well, we're looking at creating some scripts to automate new user creation/old user disabling tasks in our organization because we have a lot of user turnover. I'm assuming that if those scripts were created using the .NET classes we would not need to update them when in the future we transition to a native 2008 domain?
|
# ? Jan 4, 2012 14:32 |
|
Jethro posted:Note that it's $false not false Thanks for that. Outside the $ I was pretty close just by trial and error. I had the same thing, but I had the conditional statement (psiscontainer) after move-item.
|
# ? Jan 4, 2012 15:48 |
|
Powdered Toast Man posted:Ok...well, we're looking at creating some scripts to automate new user creation/old user disabling tasks in our organization because we have a lot of user turnover. I'm assuming that if those scripts were created using the .NET classes we would not need to update them when in the future we transition to a native 2008 domain? Correct they would continue to work just fine. I actually recommend people get used to the .NET classes as you know they will always work and are independent of DC Version/Forest Level/Powershell Version/Powershell installed modules. If you need any help let us know.
|
# ? Jan 4, 2012 16:45 |
|
How would one discover if a file was created on the first weekday in a month? I can find a bunch of examples from years ago that use vbscript, but none do 100% what I need
|
# ? Jan 4, 2012 21:50 |
|
Wicaeed posted:How would one discover if a file was created on the first weekday in a month? code:
|
# ? Jan 5, 2012 02:55 |
|
i barely GNU her! posted:
How about (again, not tested): code:
|
# ? Jan 5, 2012 03:19 |
|
|
# ? May 21, 2024 14:11 |
|
Mario posted:What if the 1st is on a Monday? This would match Tuesday and Wednesday as well.
|
# ? Jan 5, 2012 09:39 |