|
GPF posted:And, Phone, Import-CSV and Get-Content were brought to us by $deity itself. Yeah, the ease of reading from and writing to files is really nice. The more I use it, the more I like it to be honest. A project I'm working on is using a shitton of legacy VBScript, and I wish they would rework this poo poo to use PS instead for a variety of reasons. The biggest one being that the cscript/wscript engine has a totally sexy bug where it may not always return the exit code you specify in your script to the calling process.
|
# ? Mar 7, 2012 16:56 |
|
|
# ? May 21, 2024 18:44 |
|
Hi! I'm completely new to Powershell, but I'm interesting in learning and I have a task that I'd like to automate. Basically, I need to run a series of SQL queries and then export all columns except the leftmost of each result to it's own Excel file with a slightly different set of headings. I'd then need it to save all of the files in a new folder in a directory, with the files titled like '[Date]_[a]_Import.xlsx'. The structure would look like: run SQL a, export to a.xlsx in xxx\[Date]_Imports run SQL b, export to b.xlsx in xxx\[Date]_Imports run SQL c, export to c.xlsx in xxx\[Date]_Imports I can change the queries if removing the left column or reformatting the headings is too much, but I'm curious if this is doable with Powershell. Anyone feel like lending a hand?
|
# ? Mar 7, 2012 23:07 |
|
Import-CSV is the poo poo, but Export-CSV is horrible piece of poo poo which I hate... until I had to write my OWN CSV import/export functions in VB.NET and goddamn did I miss powershell. It's really hard to write a correct parser for CSV files, they are the devils own work. Viking_Helmet! Do you need excel or can they be CSVs? Because what you describe is pretty darn easy if it can be CSVs! Also whoever was talking about not having enough Powershell projects at work my philosophy was always make myself projects if I had time. If there was a task I had to do at work that could be automated with powershell, automate it. My general experience has been that employers will see that, recognize it as a skill, and start to give you more projects of that nature. adaz fucked around with this message at 01:26 on Mar 8, 2012 |
# ? Mar 8, 2012 01:09 |
|
Sure, just checked and CSVs import fine. Thanks!
|
# ? Mar 8, 2012 01:32 |
|
Each SQL query is unique? How many are there? Is there any reason why you need to exclude the leftmost column as opposed to just not querying for that data? To get started what I'd use is a function I wrote long ago and my normal method of avoiding export-CSV's... eccentricities. Yes it would be a glorious world where we could just pipe the DataSet to export-csv and it'd handle it gracefully... unfortunately that world doesn't exist. code:
code:
adaz fucked around with this message at 01:44 on Mar 8, 2012 |
# ? Mar 8, 2012 01:41 |
|
I'm trying to get a list of servers (~400), their operating system version and all the groups/users in the local Administrators group and export it to a csv. I've modified a script to give me the list of servers and the operating system and export it, but having trouble getting a Get-WMIObject query on each server name for the local administrators group. Any ideas where to go from here?code:
|
# ? Mar 8, 2012 22:44 |
|
adaz posted:Each SQL query is unique? How many are there? Is there any reason why you need to exclude the leftmost column as opposed to just not querying for that data? Well, the main reason I was hoping to exclude the leftmost column is because the (lengthy) query was written by someone else who didn't comment anything. However, after actually looking at it I found out that it's fairly simple and removing the column wasn't a problem. The only difference between the queries is a "where" clause that's in a few different locations (select a where name='x', select a where name='y', select a where name='z') Since the actual difference between queries is small, how hard would it be to make powershell loop through and replace each instance of x then y then z while still exporting each set of results to a csv?
|
# ? Mar 9, 2012 01:45 |
|
Viking_Helmet posted:Hi! If you're using MS SQL Server, you can put FOR XML in your query. Probably not with other implementations.
|
# ? Mar 10, 2012 03:34 |
|
Hey guys, I'm back to leeching off your expertise without ever contributing anything once again. I've got a bunch of product images are 500 pixels tall by (x) pixels wide. What I want to do is identify files where (x)<500 and then I want to 'pad out' the image file with white pixels (equally on either side) so it ends up being 500 x 500. I'm know I can do this in Image Majick but would rather not install it on a production server (hundreds of thousands of images are involved), so I figured I'd ask you guys if it's possible in PowerShell. I know from reading around it's possible to resize things using WIA (http://blogs.msdn.com/b/powershell/archive/2009/03/31/image-manipulation-in-powershell.aspx), but it looks a bit daunting and was wondering if anyone had any experience doing it. I also can't find reference to padding the image instead of resizing it.
|
# ? Mar 12, 2012 02:49 |
|
Hmmm, that's an interesting one. The padding is harder than I thought, or at least, I couldn't find an option for it. (I tried cropping a negative amount of pixels, but that seems to error out.) Filtering is easy:code:
Only thing I could think of, is to have the script create an empty image, of $image.height * 500, and use the "stamp" filter to add the image over the blank one. See this MSDN page for an example in .NET.
|
# ? Mar 12, 2012 11:52 |
|
Thanks for that. I actually have a .vbs script that can do it so I'm aware of the .NET solution, which goes exactly as you surmise. Create white 500x500 canvas, paste centered, and your uncle is bob. Was hoping to centralize around PowerShell and drop some of this array .vbs, .bat, .cmd files that have been following me around since the old days. I'm still pretty sure it's possible, just have to figure out how to access external libraries properly in PowerShell; I know the .NET syntax but am not sure how to reference it. Basically I want to be able to do:code:
code:
EDIT-ugh stupid question didn't notice your WIA call in the example code until I tried to run it just now. Embarrassing. Scaramouche fucked around with this message at 01:40 on Mar 13, 2012 |
# ? Mar 12, 2012 21:33 |
|
I've got a bunch of computer objects in AD that are containers containing sub-objects, and I'm having a problem trying to pull the sub-objects out. I've tried get-adcomputer and get-adobject in conjunction with get-childitem, but I apparently don't know the right magic. Can anyone suggest another way to attack this?
|
# ? Mar 13, 2012 16:28 |
|
On the subject of really using .NET, I am only a noob at that, I think Adaz or someone else will be better suited to answer that. And Angrytech: what properties are you trying to get from the computer object? Have youtried simply calling $_.propertyname or used get-adcomputer with the -property flag? Have you checked the Technet page on get-adcomputer
|
# ? Mar 13, 2012 16:48 |
|
Jelmylicious posted:On the subject of really using .NET, I am only a noob at that, I think Adaz or someone else will be better suited to answer that. And Angrytech: what properties are you trying to get from the computer object? Have youtried simply calling $_.propertyname or used get-adcomputer with the -property flag? Have you checked the Technet page on get-adcomputer From what I can tell, the problem is that the sub-object isn't actually a property, so the property flag won't get me what I want.
|
# ? Mar 13, 2012 17:30 |
|
I actually just recently was working with a bunch of image conversion stuff in powershell. Maybe this will help? It's a resize-JPEG function that returns a byte array (it's designed for loading thumbnail photos into Active Directory) but it might give you a head start on your project. Gives you an example of how to use the system.drawing stuff.code:
As an example to access children on an AD object using the base .NET classes, here is the basic code I used. We're looking at the children of a DC and the screenshot is showing an example of such. Works the same for an OU, etc. If you wanted to iterate through them use a foreach loop. code:
adaz fucked around with this message at 19:14 on Mar 13, 2012 |
# ? Mar 13, 2012 19:04 |
|
Thanks Adaz, think I got it mostly figured out. I just needed to get onto the right new-Object x.y.z syntax before I could start hacking around. After that it's trivial to re-word the .vbs script. If I can get it cleaned up I'll try to post it here.
|
# ? Mar 13, 2012 21:47 |
|
I'm now trying to get a list of all server's current time zone settings as we may have had a few that had the wrong ASI installed. Testing this line by line works, but when it gets down to the $timezone part, it's giving me the following:Powershell posted:Name Changing $Server to SERVERNAME01 in the script it works fine--any idea what I'm doing wrong? code:
|
# ? Mar 14, 2012 16:10 |
|
Stupid question We have a server that I have to backup using powershell and robocopy because it is still stuck on Server 2003 SP1. I have the following script and it runs just fine but I'd like to add some error detection. Would it be possible to have the script return an email of robocopy returned error level 1 or something similar? code:
|
# ? Mar 14, 2012 16:30 |
|
Recluse $Server is just the ADobject you're retrieving, you need to access the property (name). Take a looksee at screenshot but you need to replace $server with $server.name Nebulis01 Short answer: yes Long answer: maybe You can encapsulate that entire thing in a try/catch block and then call a send-email function (I posted one a page or so back) when powershell encounters a terminating error. The problem i've had before with using third party utilities - such as robocopy - is they might error out and powershell probably will have no idea unless they are throwing a terminating error which is rare. It will, however, catch when a powershell function or cmdlet encounters an error. What i've done in the past when using robocopy inside powershell is to log it and have powershell email me the logs, then check those. Here are some examples Try/Catch block code:
code:
adaz fucked around with this message at 19:48 on Mar 14, 2012 |
# ? Mar 14, 2012 19:39 |
|
adaz, thanks. I'll give that some testing
|
# ? Mar 14, 2012 20:45 |
|
adaz posted:I actually just recently was working with a bunch of image conversion stuff in powershell. Maybe this will help? It's a resize-JPEG function that returns a byte array (it's designed for loading thumbnail photos into Active Directory) but it might give you a head start on your project. Gives you an example of how to use the system.drawing stuff. Thanks dude, this did exactly what I need.
|
# ? Mar 15, 2012 17:54 |
|
Getting an unexpected error with a script I've used successfully on other servers without any problems:code:
Anyway, I get the following error: ConvertFrom-SecureString: The system cannot find the file specified Some googling implies that this might be a UAC thing, can anyone confirm? I don't think it's related to c:\passwordfile.txt, since I would expect an error to be thrown on Set-Content in that case.
|
# ? Mar 15, 2012 18:09 |
|
Not sure if it's UAC or not. With UAC turned off everything works fine. UAC turned on I get some errors about writing to the root of C:\, if I change it to C:\temp\blah (or whatever your environment has set as the temp dir) it works fine. Try writing to temp or like ENV:\Temp and see if it'll work for you but I was unable to replicate the exact error you're getting. We don't have UAC turned on on our servers though, so I was just testing with Windows 7 instead of server 2k8 which might explain some of the discrepancies. The other thing I guess it could be is if C:\ isn't on one of your servers for some reason. Are you powershell remoting to the servers? Do any of your servers not have a C:\ drive for some reason (for instance, some of ours don't if they are citrix boxes)? adaz fucked around with this message at 18:25 on Mar 15, 2012 |
# ? Mar 15, 2012 18:20 |
|
stubblyhead posted:Getting an unexpected error with a script I've used successfully on other servers without any problems: Unless you're running as admin in 7/2008R2 with UAC enabled. You won't be able to write to the root of a drive, it's a security thing. Try ENV:\temp or something else? edit: damnit adaz, you're quick
|
# ? Mar 15, 2012 18:27 |
|
Could very well be a UAC error, since server 2008 doesn't like you putting things in the C:\ root. Either put it in a folder or elevate your powershell.
|
# ? Mar 15, 2012 18:30 |
|
Nebulis01 posted:edit: damnit adaz, you're quick Heh it's the lunch hour was just checking threads!
|
# ? Mar 15, 2012 18:32 |
|
I'm sorry, I was a little extreme in my script sanitizing. I'm writing my file to a directory, not the the root. The path definitely exists, but regardless if it were an issue creating that file wouldn't the error get thrown on Set-Content? I have some additional error text too that will probably be helpful, but I had to jump back on a vpn which blocks sa. I'll try to remember to post later. e: Just realized I can ssh to home and get to the forums that way. Here's the full output: code:
stubblyhead fucked around with this message at 18:39 on Mar 15, 2012 |
# ? Mar 15, 2012 18:34 |
|
SecureString and convert-SecureString requires you to use the same user account to decrypt the password as you used to encrypt it. Are you encrypting the save file with another user account then running it on the server under a different account?
|
# ? Mar 15, 2012 18:46 |
|
adaz posted:SecureString and convert-SecureString requires you to use the same user account to decrypt the password as you used to encrypt it. Are you encrypting the save file with another user account then running it on the server under a different account? This is the script which is going to create that encrypted file to be used later; there's no decryption happening at all in this script. The very same script has worked without error on other servers, but I have limited visibility to see what kind of settings might be different on this one compared to those.
|
# ? Mar 15, 2012 19:01 |
|
adaz posted:Recluse Excellent, worked great, thank you!
|
# ? Mar 15, 2012 19:29 |
|
stubblyhead posted:This is the script which is going to create that encrypted file to be used later; there's no decryption happening at all in this script. The very same script has worked without error on other servers, but I have limited visibility to see what kind of settings might be different on this one compared to those. If that's the case I'm guessing it is going to be something more esoteric, perhaps a different version of .NET? Are the servers you're having the issue on older/newer/have anything at all in common? Does your account not have admin permissions on those servers?
|
# ? Mar 15, 2012 21:20 |
|
adaz posted:If that's the case I'm guessing it is going to be something more esoteric, perhaps a different version of .NET? Are the servers you're having the issue on older/newer/have anything at all in common? Does your account not have admin permissions on those servers? I'm going to go with 'esoteric' as well. The account I'm using is an enterprise admin, so it should be able to do anything it wants on that server. That's one of the reasons I can't do this interactively actually--I'm not permitted to log in to the remote server with my personal account, and I can only use that admin account in automation scripts and crap like that. So basically I have the whole thing wrapped up into a process that runs powershell on the remote server (authenticating as the admin user) and executes the script. It's a moot point anyway though. I finally just asked one of my colleagues with greater access to do it for me. Same script worked perfectly logged into the server as that user, so I don't know what the gently caress. Hopefully I'll be able to actually read it as a secure string later on.
|
# ? Mar 15, 2012 23:40 |
|
stubblyhead posted:I'm going to go with 'esoteric' as well. The account I'm using is an enterprise admin, so it should be able to do anything it wants on that server. That's one of the reasons I can't do this interactively actually--I'm not permitted to log in to the remote server with my personal account, and I can only use that admin account in automation scripts and crap like that. So basically I have the whole thing wrapped up into a process that runs powershell on the remote server (authenticating as the admin user) and executes the script. Is your account actually blocked from logging into the server via GPO or something not just like a IT policy? If that's the case that would explain it. The secure cmdlets actually require a profile to be created and for the given user to "log onto" the machine before they will work. I ran into it before when doing scheduled tasks with cmdlets and secure strings, since the profile wasn't loaded I got a generic cryptographic error. If this is the case it won't be able to decrypt it when the script runs as the account trying to do the decryption still wouldn't be able to log on to use the cryptographic provider on the machine. adaz fucked around with this message at 00:33 on Mar 16, 2012 |
# ? Mar 16, 2012 00:28 |
|
adaz posted:Is your account actually blocked from logging into the server via GPO or something not just like a IT policy? If that's the case that would explain it. The secure cmdlets actually require a profile to be created and for the given user to "log onto" the machine before they will work. I ran into it before when doing scheduled tasks with cmdlets and secure strings, since the profile wasn't loaded I got a generic cryptographic error. Hmm, interesting... my personal account is not in the remote users for that machine, but the service account is--I'm just barred from using it that way by IT policy. I'm spawning that powershell process in such a way that the target server doesn't even know my personal account exists, but it's entirely possible that the service account didn't have a profile on this server. I was able to decrypt that file and use it in a credentials object though. My friend remoted in using the service account, so if it was a matter of the profile not existing that would have fixed it.
|
# ? Mar 16, 2012 00:42 |
|
Cross posting this from the Windows software megathread. I have a quick question about making a batch file to rename some files. I have to scan a bunch of files for my job. They need to be renamed to 'Scanned As At [DATE][Number]'. I played around with the ren function to get this to work but ultimately could not. The files come from the scanner as seemingly random numbers E.g. "3526346.pdf" & "3526347.pdf" should be renamed to "Scanned As At 16-03-12.pdf" & "Scanned As At 16-03-12 (1).pdf". An awesome bonus would be if they could then be put in to a folder with the same name, but multiples just in the same folder. Is this possible?
|
# ? Mar 16, 2012 05:15 |
|
It's certainly possible, Powershell is a full programming language. Something on the basis ofcode:
|
# ? Mar 16, 2012 09:19 |
|
adaz posted:Whoops, missed this -- did you ever figure it out? At a casual glance it looks like everything is right but what type of right are you setting? deny? modify? allow? or something more esoteric? Late response- but I did figure this out. There are some underlying permissions changes that needed to be added- namely inheritance and propagation flags that without, explorer only identifies the permission set as "special permissions". I suppose there may be underlying octal permissions or something that didn't properly line up with the values explorer was expecting to see in order to assign a normal read, write, or execute permission. (This is just how I rationalized it in my brain) The code that I ended up using looked like this: code:
code:
|
# ? Mar 21, 2012 21:17 |
|
Glad to hear that Dreadite. As a note, and I missed this last week, but the beta of powershell 3.0 is out: http://www.microsoft.com/download/en/details.aspx?id=28998 Changes are fairly minor as far as breaking backwards compatibility, I haven't had a chance to test out the new features. quote:
|
# ? Mar 21, 2012 22:23 |
|
Ooh, I really should try out that Beta! Thanks for the link.
|
# ? Mar 22, 2012 11:35 |
|
|
# ? May 21, 2024 18:44 |
|
Sorry to double post, but this is just too awesome. The new ISE is way more usable! No more seperated input and output pane is good. But the best thing is the syntax checking. Forgot to close some parenthesis? It will highlight your dangling one: Woohoo! It does the same thing in the powershell pane in ISE. Try putting in the following (which just feels wrong to leave like this) code:
Jelmylicious fucked around with this message at 13:35 on Mar 22, 2012 |
# ? Mar 22, 2012 13:23 |