|
Serfer posted:Ok, I've got a problem. I have a datatable (returned data from an SQL query), and I need to count up the items that have the same of a single element. Hrrm. I don't know if this is the right way to do it, but something like this would work, or more precisely it is the first thing that came to mind ($dt is your dataTable) code:
If you want to create a new dynamic variable you can use the New-Variable cmdlet inside a for loop, but I guess not sure exactly what you were asking. Did you want to find out how many times each unique instance of displayName was duplicated? Like in this theoretical table: pre:HostName DisplayName EntryDate Usages compy1 blahblah 1/1/1000 10000 compy2 blahblah 1/1/1000 10000 compy3 foobar 1/1/1000 10000 compy4 snafu 1/1/1000 10000 compy5 foobar 1/1/1000 10000 compy6 foobar 1/1/1000 10000 adaz fucked around with this message at 00:15 on May 3, 2011 |
# ? May 3, 2011 00:11 |
|
|
# ? May 9, 2024 23:32 |
|
adaz posted:Would you want to know blahblah was used twice, foobar three times, and snafu once?
|
# ? May 4, 2011 05:37 |
|
Serfer posted:Pretty much, yes That makes more sense. Again, assuming $dt is your data table... there might be a simpler way of doing this but since you probably want a nicely formatted file or table to look at after it's finished I threw in the export-Csv code:
code:
adaz fucked around with this message at 00:07 on May 5, 2011 |
# ? May 4, 2011 18:33 |
|
I want to rewrite my powershell handbrake script in C# so I can slap a big dumb gui on it but look at that code up above. Just look at it. Oh powershell, you're so pretty.
|
# ? May 5, 2011 04:47 |
|
I went to a Powershell event today and the speaker explained how you could mount Active Directory and then rm -f the entire thing like a real *nix admin. That is the moment I decided to learn Powershell. Anyone have book recommendations? Need a newbie book and then something that has some meat on it. I already ordered http://www.manning.com/jones/
|
# ? May 5, 2011 12:06 |
|
No real expert, but the other manning powershell books are first-rate too.
|
# ? May 5, 2011 12:48 |
|
ghostinmyshell posted:I went to a Powershell event today and the speaker explained how you could mount Active Directory and then rm -f the entire thing like a real *nix admin. That is the moment I decided to learn Powershell. hahahahaahha I never thought about it but you definitely could. The O'Reilly Powershell Cookbook is a nice handy reference (http://oreilly.com/catalog/9780596528492) but it's a bit out of date and doesn't have anything on version 2. Other than that, the Windows Powershell in Action is also a good in depth guide, but again is only v1. I haven't read any of the newer books on V2 unfortunately, but the differences are mainly minimal - it is better because it comes with remoting, transactions, try/catch and some new cmd-lets so a V1 book is a good starter and learning the new stuff isn't that hard. adaz fucked around with this message at 17:01 on May 5, 2011 |
# ? May 5, 2011 16:52 |
|
I am in disbelief. I was writing a PowerShell script to dump our database schema to version control and ran into the stupidest bug in the whole world: [PS V2 RTM] Multiple Ambiguous Overloads Wow! This totally wasted my entire afternoon trying to figure out. Also, unfortunately, the workaround in the article doesn't appear to work for me. ALL I WANT TO DO IS DUMP USER DEFINED FUNCTIONS and AGGREGATES!!! This problem doesn't occur for Tables, Views, Triggers and Stored Procedures due to the interface differences between those and UDFs and UDAs. I tried the workaround suggested in the above link, but it was ultimately an epic failure. Right now I am trying to figure out how to use System.Reflection to invoke the right signature in PowerShell, which is gross. Help me. code:
Z-Bo fucked around with this message at 21:27 on May 6, 2011 |
# ? May 6, 2011 20:55 |
|
Which line is it actually having issues with the multiple ambiguous overloads? At home, don't have access to work stuff to play with a sql table and test.
|
# ? May 6, 2011 22:04 |
|
code:
code:
Both say roughly the following error: code:
I've also tried: code:
code:
Z-Bo fucked around with this message at 22:15 on May 6, 2011 |
# ? May 6, 2011 22:08 |
|
I can't get it to reproduce the error (also those ; aren't needed ) So I ran a parsed down version of your script and it worked fine with no errors whatsoever. This makes me think that maybe you're passing an empty $smoObjects or something stupid to it -- if you try to manually do all this by hand and check each output object what type is smoObjects.GetType() returning to you and are there actually objects being stored in it?
|
# ? May 6, 2011 22:56 |
|
So we're almost finished migrating to 2008R2 and Exchange 2010, and I heard on the wire that you can manipulate mailbox permissions using PS. For example, a user would call me and say that they need to give editor writes on their inbox to their new assistant. Could I do up a script in PS that would do this for me? If so, is there anything I can't do with powershell & exchange?
|
# ? May 7, 2011 03:52 |
|
Swink posted:So we're almost finished migrating to 2008R2 and Exchange 2010, and I heard on the wire that you can manipulate mailbox permissions using PS. For example, a user would call me and say that they need to give editor writes on their inbox to their new assistant. You heard correctly! The cmdlets you want are: Add-MailboxFolderPermission http://technet.microsoft.com/en-us/library/dd298062.aspx Remove-MailboxFolderPermission http://technet.microsoft.com/en-us/library/dd351181.aspx IF you want to apply permissions to the entire mailbox - say if you're creating a shared mail user and want to give a group of individual users full access control of it - you want Add-MailboxPermission http://technet.microsoft.com/en-us/library/bb124097.aspx The full list can be found here: http://technet.microsoft.com/en-us/library/bb124413.aspx As far as what you can't do, I don't honestly know, everything can be done as far as I know. But I'm not an exchange admin, and only really handle user facing stuff like mailboxes, calendars, and resources. In general though the exchange management console is just a pretty front-end for powershell and as far as I know drat near everything can be done through just scripts and powershell. adaz fucked around with this message at 04:30 on May 7, 2011 |
# ? May 7, 2011 04:25 |
|
I'm no exchange admin myself, but Mailbox/Folder permissions are a daily thing. So this all looks very promising. Thanks!
|
# ? May 7, 2011 06:19 |
|
adaz posted:I can't get it to reproduce the error (also those ; aren't needed ) Thanks for the help thinking through how to better debug this problem. 1) smoObjects.GetType() --> an SMO UrnCollection 2) smoObjects.Count --> 1 Can anyone help me with the reflective invoking of the Scripter.Script(...) method? I also don't understand why System.Reflection.MethodInfo's Invoke(..., ...) method isn't recognized by PowerShell. It may also help to repeat that this appears to be a known PSv2 bug and I need a workaround. Z-Bo fucked around with this message at 15:13 on May 7, 2011 |
# ? May 7, 2011 15:07 |
|
Ok, I was able to get this working with the small changes to your code connecting to the same version of sql server , powershell v2, and windows 7. Now, the one thing that might be different is the $udfs I'm passing contains 58 objects, not just 1 however the Type is still the same even if you just do 1 so it really shouldn't matter. I was also getting the multiple ambiguous overloads until I removed the type conversions you had forced in Script and changed the SmoObjects to remove the unnecessary forced cast after you declare it. code:
adaz fucked around with this message at 22:12 on May 8, 2011 |
# ? May 8, 2011 21:18 |
|
This time it is complaining it can't find the Script method on the Scripter object.code:
I am going to code this up in C# just to make sure I don't suddenly completely suck as a programmer.
|
# ? May 9, 2011 21:04 |
|
Z-Bo posted:This time it is complaining it can't find the Script method on the Scripter object. I should mention i was getting a similar error and it turned out to be because the output directory didn't exist, the exception is actually an error executing the script, not that it can't find the method
|
# ? May 9, 2011 21:08 |
|
Yup! Actually, debugging a little further thanks to C# made it a little clearer what the issue was. Basically, $udfs was null - I started testing on a new database today and hadn't compiled UDFs or UDAs yet. So this was a totally different problem from the other day. Your code works, it just needs: code:
Edit: Thanks for your help.
|
# ? May 9, 2011 21:33 |
|
Glad to hear it's finally working. It does all bring up a good point about powershell, the lack of a real built in debugger is a pretty huge pain in the rear end a lot of times, especially if (like this was) your script is calling a bunch of .NET classes. It becomes 1/3rd guesswork, 1/3rd stepping through things yourself to find out what's going on, and 1/3rd experience when the errors get thrown. It also doesn't help that powershell hides a lot of the complexity behind the scenes making debugging certain things a nightmare (hence that multiple ambiguous overloads wasn't really the problem, but the weird type being passed to it). On the other hand, playing with properties and methods of .NET classes is actually easier to do in powershell than in like visual studio if you just want to real quick know if it works, or just making some proof of concept code. I do appreciate that script though, on my List Of Things To Do had been a script to pull off all the stored procedures/functions off a DB and hey now I have one adaz fucked around with this message at 21:42 on May 9, 2011 |
# ? May 9, 2011 21:39 |
|
Thanks. For debugging, on the SQLPSX project I told everyone my general trick fellow goon Victor and I came up with for helping improve PowerShell tracing. PowerShell Tracing module just is too verbose and non-configurable as far as I can tell (compared to say C# or VB.NET or any .NET language). So what we did 2 years ago was this simple idiom: code:
If you scroll back in this thread, I promised to post stuff like this a long, long time ago but was just too lazy. The basic idea is it lets you abstract away the underlying Windows command line program you are wrapping, and provide PowerShell defensive error handling for it. You can also use it to check that the program even exists, and possibly even install it if it doesn't. I need to improve it so that it is generic. The signature should be: code:
quote:I do appreciate that script though, on my List Of Things To Do had been a script to pull off all the stored procedures/functions off a DB and hey now I have one I can post the final version here if people like, but I might put together a polished version for the ISE module in SQLPSX (I am a core contributor to that project, although none of my modules have yet to make it into a release/been reviewed by the team; I am working on a super-awesome profiling and tracing SQLPSX API that hooks into the SqlIse module since the long-term goal of the project is to let you use PowerShell-ISE to replace the god awful SQL Server Management Studio). Z-Bo fucked around with this message at 00:43 on May 10, 2011 |
# ? May 10, 2011 00:40 |
|
Just chiming in to say: Cha-CHING! Googled "Powershell ScriptBlock class" today to go lookup the documentation on how to rewrite my Invoke-Command wrapper, and found somebody else's ConvertTo-ScriptBlock snippet. This is so incredibly useful I felt I had to share it with you all and rejoice.
|
# ? May 11, 2011 20:24 |
|
When using trap, does the trap block need to be placed before the lines that could throw an exception, or can it be placed anywhere while it's still in the same block?
|
# ? Jun 21, 2011 18:42 |
|
MrSamurai posted:When using trap, does the trap block need to be placed before the lines that could throw an exception, or can it be placed anywhere while it's still in the same block? It can be placed before, it will attempt the trap statement in the block first, doesn't matter if it comes before or after the code. This is a pretty good overview of traping in powershell http://huddledmasses.org/trap-exception-in-powershell/. Honestly ever since v2 I've switched everything to try/catch blocks
|
# ? Jun 21, 2011 19:23 |
|
adaz posted:It can be placed before, it will attempt the trap statement in the block first, doesn't matter if it comes before or after the code. This is a pretty good overview of traping in powershell http://huddledmasses.org/trap-exception-in-powershell/. Honestly ever since v2 I've switched everything to try/catch blocks Thanks for the info. I found that page before, but I didn't remember if it explicitly said anything about where trap blocks need to be placed.
|
# ? Jun 21, 2011 19:31 |
|
MrSamurai posted:Thanks for the info. I found that page before, but I didn't remember if it explicitly said anything about where trap blocks need to be placed. I actually don't' remember if it does or not, but if you repeat their experiments (or just look at the code examples) you can see they are mainly placing trap blocks before the code. I tested it with a few of mine that I haven't converted to try/catch blocks (I usually place mine at the end, just swapped to front) and it worked the same.
|
# ? Jun 21, 2011 19:54 |
|
Is there an easy way to send an email to a email alias when a script throws an error? We're using Office 365 to handle our exchange server so the SMTPclient class won't work for us. We've tried to use outlook but it throws security errors. Anyone have experience with using outlook to send an email with a log attachment file when the script throws an error?
|
# ? Aug 2, 2011 19:52 |
|
Rabid Snake posted:Is there an easy way to send an email to a email alias when a script throws an error? We're using Office 365 to handle our exchange server so the SMTPclient class won't work for us. We've tried to use outlook but it throws security errors. Anyone have experience with using outlook to send an email with a log attachment file when the script throws an error? You should be able to use Send-MailMessage if you're using PS 2.0, and pass the $error variable to the body. http://technet.microsoft.com/en-us/library/dd347693.aspx I abuse the hell out of it for custom reporting, it's pretty flexible just make sure you process all your objects to formatted strings before attempting to send or it will just look like garbage.
|
# ? Aug 3, 2011 05:56 |
|
How can I get my data back after using format list? I'm using "get-eventlog -computername srv01 -logname system -source print -after $date" and then piping through fl to get just a couple properties, but if I use convertto-csv after that, I just get a bunch of UUID's instead of the textual data. Obviously I'm just being stupid and there's something obvious that I'm overlooking.
|
# ? Aug 4, 2011 17:43 |
|
Pipe it to Select-Object for conversion to a CSV format:code:
Format-List is really for display purposes or pure string output. So alternatively you can do this: code:
|
# ? Aug 4, 2011 18:30 |
|
Export-CSV is one of the worst behaved cmd-lets, or one of the worst documented one I'm not sure which but it basically never ever behaves how you would expect it to. What cronus said will work, the other trick is to make your own custom objects, add them to a array, and export that out. Also works well if you're trying to combine information from multiple cmd-lets and so forth to add to a single csv file Something like this code:
|
# ? Aug 4, 2011 22:49 |
|
Background: To kill some time at work and "increase my worth" (read: kill time) I've read up on some Powershell tutorials. I'm new to IT so tricks aren't popping out of my rear end all of the time and I'm really loving horrid at programming, but today was a day where I knew I could have streamlined a lot of stuff just if I knew a bit more; I know how batch files work and I know that PS can do all sorts of fun things. I decided to make a batch file to silently install a dozen Windows Updates, it took me a few tries but I figured out the syntax. I did it the long way and just copied the whole path and filename into notepad to do it. I know that there's a super easy way to get this done with less ctrl+c/v action. The meat: I can get PS to just dump all of the files with the code:
Basically I want to go from code:
code:
|
# ? Aug 9, 2011 22:46 |
|
You'll laugh it's so easy, but there are a few gotchas:code:
The key part here is the $_, which represents the current index within the array of items in $updates (the list of files, basically) that it iterates through. Also, by default PS will not run commandline strings as is, so that's why you use Invoke-Expression to execute the commands. Note the single quotes inside the double, without them it will treat spaces as multiple lines which you don't want. And how to do the whole thing in one line using aliases and no need for file outputs: code:
|
# ? Aug 9, 2011 23:58 |
|
That will execute the command though, not output it to a batch/text file, correct? I think that might be another gotcha. I'm looking to do it this backward rear end way because I'm updating thin clients with Windows 7 Embedded.
|
# ? Aug 10, 2011 00:06 |
|
In that case you just do the formatting with Foreach-Object and use Out-File to write the .bat file directly:code:
|
# ? Aug 10, 2011 01:55 |
|
So I borrowed Spudman's script that scrapes computer name/model stuff by subnet. I am now trying to modify it to bring back much more than that. I am having a slight problem though getting memory to come back through the script. To get back memory, I am using this. (Get-WmiObject Win32_PhysicalMemory | measure-object Capacity -sum).sum/1mb How do I throw that in my loop so I can assign it to a variable I have tried a few different things, but here is the error I am getting code:
|
# ? Aug 18, 2011 22:49 |
|
That error is coming up because it thinks you are trying to do a subtraction.code:
code:
|
# ? Aug 19, 2011 09:53 |
|
The <<<< Computer part was added in once it errored out. For the code, I need the | measure-object Capacity -sum as if I just do Get-WmiObject Win32_PhysicalMemory, it brings back a table of stuff that I am not interested in. I feel like this is getting close, I am just missing something stupid. $mem = (Get-WmiObject Win32_PhysicalMemory | measure-object Capacity -sum).sum/1mb -Computer $ip -Credential $creds -ErrorAction SilentlyContinue | ForEach-Object {$_.Capacity}
|
# ? Aug 19, 2011 15:21 |
|
Moey posted:The <<<< Computer part was added in once it errored out. So I was wrong about the inadvertent space. With your line of code I was expecting an "unexpected parameter" type error. Have you tried the line of code from my previous post? I'm about 98% positive it will work for you. -Computer, -Credentials and -ErrorAction are all parameters of Get-WmiObject so you must provide those before you can pipe to Measure-Object to do your memory calculation.
|
# ? Aug 20, 2011 06:49 |
|
|
# ? May 9, 2024 23:32 |
|
Mully Clown posted:
Yup, you were correct, I am an idiot. I think I missed a chunk of that line you wrote when I copied it. Thanks. Now time to figure out all the other cool things I can add in to this. Also, suggestions on a good PS book from anyone? I actually found this book in my office, it's almost 5 years old though. http://www.amazon.com/Microsoft-PowerShell-Programming-Absolute-Beginner/dp/1598633546/ref=sr_1_1?ie=UTF8&qid=1314050807&sr=8-1
|
# ? Aug 22, 2011 23:07 |