|
I've been writing all my output to CSV in my scripts because it's been for human consumption. But I discovered that if one of the properties of your exported objects is an array, export-csv doesn't like that (just prints System.Object[] instead the values in the array). So now I've discovered outputting XML and JSON. This changes everything!
|
# ? Oct 13, 2015 02:57 |
|
|
# ? May 15, 2024 04:01 |
|
Ithaqua posted:Agreed on most points. I don't agree with writing a WebAPI service to provide a RESTful API for a simple PS script, though -- that turns into a whole deployment and maintenance scenario. Any well-architected, modern application should be able to perform logging and handle error conditions itself without needing an external helper script. Since we're talking about an external helper script, we can assume it's probably a legacy application that's already suffering from spaghetti dependency syndrome. Adding WebAPI on top of that is just throwing another noodle in the pile. Yeah, I'm certainly not saying this is something to do for every situation. It's a large undertaking even when it's a simple database and all you need is read-only access; not something to be taken lightly. In my case it's not so much about managing the application, or the database itself, it's often tying some administrative/operations process into whatever that existing app/DB does/stores. Like being able to use that information in other processes (which are done exclusively via script), without making the scripts depend directly on the database (its server name, its schema, etc.) and without embedding a crapload of SQL statements. FISHMANPET posted:I've been writing all my output to CSV in my scripts because it's been for human consumption. But I discovered that if one of the properties of your exported objects is an array, export-csv doesn't like that (just prints System.Object[] instead the values in the array). So now I've discovered outputting XML and JSON. This changes everything!
|
# ? Oct 13, 2015 03:51 |
|
Amazing discovery: run "invoke-item ." To open the current directory in Explorer. But use the alias: code:
|
# ? Oct 13, 2015 04:49 |
|
Swink posted:Amazing discovery: run "invoke-item ." To open the current directory in Explorer.
|
# ? Oct 13, 2015 16:49 |
|
Well originally I was using osql where I could just pass in a small simple query, but as soon as they figured out I knew what the hell I was doing they kept piling more and more on me, and I went from simple sub-10 line queries to finding out that there's an exactly 1008 character limit to osql queries on the command line. I have one script that queries a configuration database with a couple joins that determines what and in what order to remove servers from our load balancer during code deployment. We bought one software suite I recommended which has deep hooks for powershell and I think the plan was to purchase another suite about this time this year. I think the plan was that I would eventually become supervisor of our code deployment team, but then our manager died and post-merger they decided to spin our group off to the software vendor so welp. So now I'm furiously writing scripts to keep everything afloat until the end of the year when either I get my offer letter from the vendor or I get formally reassigned under our app dev group. We're way past the point where we should have gotten some sort of management framework, but I still feel like I should be able to say something like code:
Hadlock fucked around with this message at 04:48 on Oct 16, 2015 |
# ? Oct 16, 2015 04:43 |
|
Hadlock posted:Well originally I was using osql where I could just pass in a small simple query, but as soon as they figured out I knew what the hell I was doing they kept piling more and more on me, and I went from simple sub-10 line queries to finding out that there's an exactly 1008 character limit to osql queries on the command line. Not sure if you'll find anything you need here, but Chrissy LeMaire has some excellent SQL PowerShell scripts on her blog.
|
# ? Oct 16, 2015 23:04 |
|
I've reached the limit of where the SCCM Cmdlets can take me, so I'm having to branch out into WMI queries for some of my stuff. And I have to say I'm just sort of lost. If I'm lucky enough to find someone online doing the same thing as I am I can just copy their query. But once I try and modify them I get lost, and I'm not really sure where to look for more help. I can find the MSDN documentation on the WMI classes, but that's pretty useless. Maybe I need a language primer/tutorial? Not really sure. I know some basic SQL so it's not like a WQL query is completely foreign to me.code:
Ok I figured out the join and why I don't need it, and rewrote the query like this: code:
To add more confusion to this, when I try and find articles about the WHERE clause, "in" isn't one of the listed operators, and I in fact found a Stackoverflow question that is my exact question, and the answer was "WQL doesn't have an IN operator" and yet there it is and it works. So I can't even really do research on what the found code is doing, because apparently what it's doing is impossible! http://stackoverflow.com/questions/19530825/in-operator-in-wql FISHMANPET fucked around with this message at 23:53 on Oct 16, 2015 |
# ? Oct 16, 2015 23:19 |
|
FISHMANPET posted:Ok I figured out the join and why I don't need it, and rewrote the query like this:
|
# ? Oct 17, 2015 16:31 |
|
You may be able to speed it up a little bit by creating a CIM session to $SiteServer:code:
|
# ? Oct 17, 2015 21:17 |
|
Venusy posted:You may be able to speed it up a little bit by creating a CIM session to $SiteServer: D'Oh! That's a much better first step. I looked for the -ComputerName parameter first so I could suggest that, and I completely missed it. If using the CIM cmdlets doesn't quite work right for whatever reason, and you have powershell remoting enabled on the machine, you can also do the WMI calls through remoting (don't supply -ComputerName in that instance).
|
# ? Oct 17, 2015 21:48 |
|
I did something interesting today. I don't want to try and reproduce it on my home computer today/now but this was able to consistently crash clr.dll in Powershell ISE on a patched WS2008 R2 box running $psversiontable.version 4 psuedo-code php:<? $global:array += @() $global:array += "stuff" function checkpath{ $path = "c:\fldr\" $directory = get-childitem $path if($global:array.contains($directory.FullName)){} #do nothing else{ #do stuff } } checkpath ?> Any idea here? I've never ever seen PowershellISE.exe loving crash, let alone due to clr.dll. Holy poo poo edit: tried it at home, did not work. I will give it another go with the actual code. It's reproducible on that one machine though... double edit: clarified intellisense, added runnable code Hadlock fucked around with this message at 01:40 on Oct 20, 2015 |
# ? Oct 20, 2015 01:22 |
|
Hadlock posted:I did something interesting today. I don't want to try and reproduce it on my home computer today/now but this was able to consistently crash clr.dll in Powershell ISE on a patched WS2008 R2 box running $psversiontable.version 4 I've seen ISE crash based on using windows forms, but nothing like you've described with intellisense. There's always the possibility that if it only happens on the one machine, it's some kind of installation problem, dll mismatch, corrupted cache, I dunno. Also could be bad RAM (I hate saying this because it's such a boogeyman). So weird.
|
# ? Oct 21, 2015 14:57 |
|
I'm back with another optimization question! (I gave up on that last one and just let the loop run 1000 queries because it was basically a one off thing) So now I'm working on a script to check some stuff in SCCM and notify people if they're doing dumb stuff, so this will be running at regular intervals. Part of it is that I have an object array of 1600 Collection objects. If you know anything about SCCM, we have a complex setup of limiting collections, and I'm trying to find the "root" limiting collection for each collection. So I have this poor man's recursion: code:
It'll run at night so ultimately I don't care how long it takes but it's a bear to test and if I can optimize it that'd be good either way, and also learning new things is good too.
|
# ? Oct 26, 2015 18:20 |
|
e: Figured this out. Going to use DirectoryName.substring(3) to get the file path without a drive letter so the end result looks like this:PHP code:
How do I preserve the folder structure at the destination? Everything is in "D:\ABCPath\A\Myfile.encrypted", "D:\ABCPath\B\Myfile.encrypted", "D:\ABCPath\C\MyFile.encrypted" I'd like to keep these files in the directories $phase2tempDir\A, $phase2tempDir\B, $phase2tempDir\C, but only copy the folder over if that folder contains an encrypted file that was last modified before $delDate. Right now it's mashing them all together and replacing existing .encrypted files with the same name in the root directory. PHP code:
Roargasm fucked around with this message at 01:21 on Oct 27, 2015 |
# ? Oct 26, 2015 19:33 |
|
So I know that this line specifically is what's taking so long, because it's where I'm searching through 1600 items:code:
With this code: code:
|
# ? Oct 26, 2015 21:22 |
|
And Victory! I limited the properties in $collections to only those I wanted. So basically: code:
So the moral of the story is, the bigger your object, the longer where-object spends looking through it!
|
# ? Oct 28, 2015 05:16 |
|
I tried doing this todayC# code:
And if you go in to powershell ISE and do this C# code:
cmd.exe /c "powershell.exe -file \\path\to\code.ps1 -serverslist 'IISSERVER1,IISSERVER2,IISSERVER3'" It just blows up I'm guessing there's special handling of arguments when passing in parameters with commas in them, and they're not treated literally. What if I did this? cmd.exe /c "powershell.exe -file \\path\to\code.ps1 -serverslist 'IISSERVER1`,IISSERVER2`,IISSERVER3'" With backticks after the , to have it interpret it literally?
|
# ? Oct 28, 2015 05:29 |
|
nvm
|
# ? Oct 28, 2015 07:00 |
|
Hadlock posted:I tried doing this today What if you remove the single quotes? Don't escape the commas.
|
# ? Oct 28, 2015 16:29 |
|
If I did a "Write-Host $servernames" it output server1,server2,server3 The main problem was putting $servernames in the body of a send-mailmessage code:
ICM $emailhost {param($servernames) send-mailmessage -to 'Leroy Jeknins <recipient@company.com>' -from 'name <name@fizzbuzz.com>' -subject "[ SUCCESS ] AUTH IISRESET Report in $env:USERDOMAIN - $(Get-Date -format g)" -body "All server Resets completed, ran on servertype:<br> $servernames ; <br><Br> Script completed at $(Get-Date -format T) ." -BodyAsHtml -smtpServer pop.company.com} -ArgumentList $servernames I thought for a while maybe it was having an issue with the [ BRACKETS ] so I took that out of the equasion, even tried playing around with the double and single quotes.
|
# ? Oct 28, 2015 17:35 |
|
Hadlock posted:If I did a "Write-Host $servernames" it output server1,server2,server3 If your Write-Host is showing commas, then that's incorrect. That's why I was saying to remove the single quotes, as that would pass your computer list a single string with all the names separated by commas. What you want is an array of strings. Write-Host should print an array as all of the elements separated by spaces or newlines.
|
# ? Oct 28, 2015 19:54 |
|
Mailmessage body hates anything besides HTML and strings. I don't get what the error message is about, but try converting your serverlist into a string and hold it in a separate variable for the email
|
# ? Oct 29, 2015 02:17 |
|
I'm trying to put a script together in Powershell to reformat a file, and having some troubles.code:
|
# ? Oct 30, 2015 16:03 |
|
Harl37 posted:I'm trying to put a script together in Powershell to reformat a file, and having some troubles. StartsWith isn't a Powershell method, it's a .Net String method and it only takes strings, not regexes. Just use: if ($line -match "^2")
|
# ? Oct 30, 2015 16:30 |
|
Harl37 posted:I'm trying to put a script together in Powershell to reformat a file, and having some troubles. Maybe use something like $a = 0..9 if($a -contains $line.SubString(0,0)) ?
|
# ? Oct 30, 2015 16:32 |
|
BTW, very useful tip from powershell.com today:code:
|
# ? Oct 30, 2015 16:35 |
|
Harl37 posted:I'm trying to put a script together in Powershell to reformat a file, and having some troubles. Use regex with the -match operator: code:
code:
Adding -eq $true is often redundant. When the expression you're testing evaluates to a [bool] you can just leave it off.
|
# ? Oct 30, 2015 20:06 |
|
I'm getting an access denied error while trying to move items. Which is weird, because I could move one item successfully. It's only when I'm trying to move multiple that it doesn't work.code:
code:
22 Eargesplitten fucked around with this message at 04:08 on Nov 2, 2015 |
# ? Nov 2, 2015 03:55 |
|
22 Eargesplitten posted:I'm getting an access denied error while trying to move items. Which is weird, because I could move one item successfully. It's only when I'm trying to move multiple that it doesn't work. Have you checked to make sure you are actually getting any $OldItems and that just touching the files hasn't reset all their LastAccessTime to Now?
|
# ? Nov 2, 2015 04:05 |
|
Yes, I am. Moving the item isn't changing the access time. I ended up getting it working, but only if I'm doing \downloads\test1, which is what I meant it to be. Unfortunately, if I get it past testing, that's what it needs to be.
|
# ? Nov 2, 2015 04:12 |
|
22 Eargesplitten posted:Yes, I am. Moving the item isn't changing the access time. I ended up getting it working, but only if I'm doing \downloads\test1, which is what I meant it to be. Unfortunately, if I get it past testing, that's what it needs to be. This does the job for me (assuming "test1" and "Test2" are directory names): code:
|
# ? Nov 2, 2015 04:15 |
|
Thanks. I can't believe I forgot the Pipe, that was one of the parts that I've read in A Month Of Lunches. From what I was doing, it looks like I was making several mistakes. First, I was doing \downloads, rather than selecting objects from downloads. Then I was moving all of the items in the folder, regardless of age. That's because to fix the first problem, I did \downloads\* instead of the $($_.Name). For some reason, I thought that ForEach would be only going through the objects in $OldItems.
|
# ? Nov 2, 2015 04:34 |
|
Hey guys, I've been attending the Mississippi PowerShell User Group meetings (I started 2 months ago). It's all virtual, so you can attend from any location. The next meeting is tonight: http://mspsug.com/2015/11/10/mississippi-powershell-user-group-november-virtual-meeting-tonight-at-830pm-cst/ The Lync/Skype for Business client is a bit weird, so you might want to test it out before hand.
|
# ? Nov 10, 2015 22:45 |
|
Briantist posted:Hey guys, I've been attending the Mississippi PowerShell User Group meetings (I started 2 months ago). It's all virtual, so you can attend from any location. The next meeting is tonight: http://mspsug.com/2015/11/10/mississippi-powershell-user-group-november-virtual-meeting-tonight-at-830pm-cst/ What's the talent level look like in there? Looks cool but do they cover higher level stuff?
|
# ? Nov 10, 2015 22:53 |
|
Roargasm posted:What's the talent level look like in there? Looks cool but do they cover higher level stuff? For the most part, there's a bit of Q&A at the end of the presentations and then everyone leaves. This time there's no presenter so I'm hoping for a more lively / involved discussion.
|
# ? Nov 10, 2015 22:59 |
|
Just dove into powershell today when I got a request that seemed like it would be a pain in the rear end to write in a batch script: take a list of FQDNs and do an nslookup on them. The only issue I'm encountering is getting the Dns Client info as I'm running on Windows 7 and Get-DnsClient is only available on Windows 8 and above despite Powershell 3. How else can I grab the client DNS server name and IP address?
|
# ? Nov 12, 2015 21:15 |
|
Uziel posted:Just dove into powershell today when I got a request that seemed like it would be a pain in the rear end to write in a batch script: take a list of FQDNs and do an nslookup on them. Does "Get-WMIObject -Class Win32_NetworkAdapterConfiguration" not contain what you are looking for?
|
# ? Nov 12, 2015 21:32 |
|
Yeah, gwmi win32_networkadapterconfiguration has a property called DnsServerSearchOrder or something that should contain an array of your configured dns servers. This seems like something that could be done better elsewhere, though, or there's more to the task than "run nslookup on some fqdns". There are a lot of modules for getting various things from your DHCP and DNS servers and barring some insane weirdness in your environment, you should be able to get just about everything you need from there. It's been a long time since I've messed around with windows domains but don't you normally push DNS server configurations as part of a DHCP lease? It would probably be a lot easier to get them from there in that case.
|
# ? Nov 13, 2015 00:56 |
|
Reiz posted:Yeah, gwmi win32_networkadapterconfiguration has a property called DnsServerSearchOrder or something that should contain an array of your configured dns servers. So it makes the most sense to retrieve whatever the adapter is actually using, and given the OS restrictions here WMI might be the most straightforward way. I'm not clear why these servers are even needed to do a name lookup though. Uziel posted:Just dove into powershell today when I got a request that seemed like it would be a pain in the rear end to write in a batch script: take a list of FQDNs and do an nslookup on them. code:
|
# ? Nov 13, 2015 02:08 |
|
|
# ? May 15, 2024 04:01 |
|
Toshimo posted:Does "Get-WMIObject -Class Win32_NetworkAdapterConfiguration" not contain what you are looking for? Reiz posted:Yeah, gwmi win32_networkadapterconfiguration has a property called DnsServerSearchOrder or something that should contain an array of your configured dns servers. The real input are all internal servers, but for example: input: https://www.google.com output: Server: ns1.ispname.com, Address: 192.168.1.1, https://www.google.com 2607:f8b0:4004:809::1003, 216.58.217.142 Edit: In looking at this, I need to know which DNS server (name/ip) is being used for each nslookup to get those IP Addresses. Is this possible? Uziel fucked around with this message at 14:21 on Nov 13, 2015 |
# ? Nov 13, 2015 02:19 |