|
Yes, you could do that with something like:code:
|
# ? Apr 5, 2015 20:54 |
|
|
# ? May 15, 2024 03:36 |
|
Awesome, that worked, thanks! (I had to remove the -Directory filter, but that's fine) So, it seems getting it to work as a script I can double click on is a bit of a challenge... edit: weird, if I run powershell as administrator it does not see mapped network drives, but running it normally works. edit2: got it working with right click, thanks again! monkey fucked around with this message at 22:10 on Apr 5, 2015 |
# ? Apr 5, 2015 21:49 |
|
Mapped drives are per user, and when you run as admin you're running as another user, and that other user doesn't have the drives that your user does.
|
# ? Apr 5, 2015 22:09 |
|
How do you use powershell regex on multi-line files? Given a file foo.txt: code:
code:
code:
|
# ? Apr 7, 2015 15:13 |
|
Newf posted:How do you use powershell regex on multi-line files? Well, for starters, ".*" is a like a loaded gun. It matches on an empty string so you get weird situations like this. You probably want to match on ".+" for most situations like that since it won't match on nothing. That's why you are getting double "hello" on each line. Also, get-content is going to split objects on line breaks. If you really want it to be a big long stream, you'll want to -Replace "`n|`r","" to clear the linebreaks. To get your desired output, you'd want to use something line: code:
|
# ? Apr 7, 2015 16:13 |
|
This is helpful, thanks. The real situation is that I need to swap all of the text (.*, I thought) between a couple of 'tags' in a file. Eg,code:
Is there any documentation on the actual behavior of .? I think that in js, for example, it works the way that I expected with my first attempt.
|
# ? Apr 7, 2015 16:29 |
|
Newf posted:This is helpful, thanks. The real situation is that I need to swap all of the text (.*, I thought) between a couple of 'tags' in a file. Eg, If you must use regex, you can use balanced regex to match tags like this (.NET is one of the few regex engines to support this).
|
# ? Apr 7, 2015 17:00 |
|
Briantist posted:If you're parsing valid XML, you may want to actually cast your string as [XML] and then use the XML object to replace the string. No XML here - this is a total hack and it's actually a .cpp file with the comments //<swap>(don't delete me) and //</swap>(don't delete me) as endpoints. Thanks for the link - that's neato. My working (well enough) solution is -replace "(//<swap>.*`n)[\s\S]*(`n//</swap>)", ('$1' + $newContent + '$2').
|
# ? Apr 7, 2015 17:11 |
|
Is there a way of using Get-WMIObject to determine which files are currently in use by a specific process? Trying to troubleshoot what I suspect could be a file locking issue with one of our internal apps.
|
# ? Apr 8, 2015 16:46 |
|
Venusy posted:Is there a way of using Get-WMIObject to determine which files are currently in use by a specific process? Trying to troubleshoot what I suspect could be a file locking issue with one of our internal apps. I think you can actually do this with Get-Process. Here's a starting point, replace 'conhost' with the name of the process you're looking for or the whole expression with whatever you need: code:
|
# ? Apr 8, 2015 17:16 |
|
That shows the DLLs in use, but I'm looking for the same kind of information you could see in the Disk section of Resource Monitor, just for remote machines. I can't use Get-Process because either it's blocked by our firewall for some reason (though Get-Service and Get-WMIObject aren't) or it requires WinRM, which isn't enabled on these machines.
|
# ? Apr 9, 2015 10:11 |
|
Venusy posted:That shows the DLLs in use, but I'm looking for the same kind of information you could see in the Disk section of Resource Monitor, just for remote machines. I can't use Get-Process because either it's blocked by our firewall for some reason (though Get-Service and Get-WMIObject aren't) or it requires WinRM, which isn't enabled on these machines. So you're trying to get the file handles of running processes on a remote machine without using WinRM / PS remoting. You may already know that you can probably get the win32_process WMI object from it using -ComputerName, but that object doesn't have the info you want. From what I can tell, you simply can't get the info you want through WMI. I did find something using .NET which may help as a starting point, but I can't get it to work for me: $processlist = [System.Diagnostics.Process]::GetProcesses("ComputerName") ..unfortunately every time I try it I get a 'couldn't connect to remote machine' error, which makes me suspect it's using the same mechanism as Get-Process, or Get-Process is just a wrapper for that object anyway. Honestly, I think the right answer here if you want to use Powershell for this is to turn on WinRM & PS Remoting on the remote system if at all possible, then you can just connect directly and run it locally. Or there's always PSExec. Edit: Actually I just got the above .NET object, and get-process, to work by starting the RemoteRegistry service on the remote machine. I did that using PS remoting though.. Yeah, get-process and the above .NET object are literally exactly the same. Start RemoteRegistry on the remote machine, then use Get-Process -Computername X. code:
code:
zzMisc fucked around with this message at 14:48 on Apr 9, 2015 |
# ? Apr 9, 2015 14:27 |
|
That's pretty cool. Is it possible to do the reverse? Enter a file and see what processes are using it?
|
# ? Apr 12, 2015 05:05 |
|
I'm trying to make a script to check if a login name is available this script is intented for HR so it needs to be as easy as possible i've made a script where it exports the samaccountname to a csv i'm a bit stumbled on how to make a gui or something for HR where they only enter the desired logon name and can see if its taken or not for a new employee. this is all I have code:
i'm still learning powershell. Is what i wanna do even possible? worst case scenario i let them run the script, open the csv and ctrl+f the name
|
# ? Apr 15, 2015 14:26 |
|
You could do a simple if/else statement looking for the user in the list.code:
|
# ? Apr 15, 2015 15:00 |
|
Spazz posted:You could do a simple if/else statement looking for the user in the list. Thank you! I didn't even know you can create a pop-up input box. Thank you for steering me in the right direction
|
# ? Apr 15, 2015 15:17 |
|
Sefal posted:I'm trying to make a script to check if a login name is available Take away the entire though process on their part. Make them feed the script First, Middle and Last names. From there the script should construct a username and test it, if it's in use, try the next combination in your username format and so on and so forth until the script gives HR the username back. The idea is to make their jobs easier. If they had rights to create use accounts in a specific OU, it might be worth actually creating the empty shell of the account for them. Extra bonus points if the script can go into the HR system and get all of the data to completely fill out the user account data.
|
# ? Apr 15, 2015 15:39 |
|
Zaepho posted:Take away the entire though process on their part. Make them feed the script First, Middle and Last names. From there the script should construct a username and test it, if it's in use, try the next combination in your username format and so on and so forth until the script gives HR the username back. The idea is to make their jobs easier. If they had rights to create use accounts in a specific OU, it might be worth actually creating the empty shell of the account for them. That's actually a really good idea. Going to try to make this.
|
# ? Apr 15, 2015 15:51 |
|
This sounds more like a process issue. Why does HR care about user names? In my experience it goes like this: HR says "hey we have a new person starting named Joe Smith, we need normal computer stuff for him" IT grunts set up computers and AD accounts and stuff, go back to HR and say "okay it's all set up, his username is jsmith12334164" or whatever HR gives the information to Joe on his first day
|
# ? Apr 15, 2015 17:17 |
|
ello Powershell thread I thought there must be one somewhere. So for my new employer and I've taken the plunge and (tried) to leave my beloved vbs behind. I can't really.. already I've found stuff I can't quite do as well in PS. For instance I write large scripts that query Active Directory and output in Excel, but in PS because it's .NET and not .COM doing this is unreliable. Apparently there is no real way to access the exposed API of Excel, I've read of people going to HTML and then CSV but opening the CSV using Excel which preserves the formatting.. Yuck. Anyways, I'm just accepting that being good with both scripting languages and using the right ones for the right tasks is best. I've got a pretty simple question for today, though. So I've got a dirty great log from a DNS server and I've written a nice PS one-liner that uses Select-String to find IPs in it. The idea is to go through the file, parse it and give an output of unique IPs, just one unique IP on each line. I've got Select-String working fine and defined IPs via a regular expression, using Select Matches I get a return of just the IP (like using grep or something). But now I want to use the Sort-Object function followed by the Get-Unique function. In the script I've tried the inital line and then just pipe to each function, finishing with piping to an output file.. but it doesn't work. I've tried putting the results of the inital parse into variable and then piping that variable to each function on new line in order (like you'd do in vbs) but that didn't work either. How do I progressive operations on a variable with functions?
|
# ? Apr 16, 2015 06:57 |
|
Ithaqua posted:This sounds more like a process issue. Why does HR care about user names? In my experience it goes like this: That's how we do it now. But we are now in the process of changing things arround and i guess make it more streamlined? one of the wishes is that u get all the info you need when HR gives you the new employee data.
|
# ? Apr 16, 2015 08:44 |
|
Tony Montana posted:ello Powershell thread Here's an example of exporting all the users in a certain OU with the last name "Lowtax" to a CSV: code:
code:
quote:I've got a pretty simple question for today, though.
|
# ? Apr 16, 2015 19:57 |
|
Kind of a long shot, I recently found this script here Kind of a long shot, I recently found this script here that can be used to clean up folders on a server. I'm going to use it, but I was wondering if there would be an easy way that I might be overlooking to include the name of the server on which it was run, either as the email subject or in the log file anywhere. I'm kind of new with Powershell, but I figure I'd ask before I start trying to edit the script to do something like that.
|
# ? Apr 17, 2015 02:18 |
|
Find the lines where it sets the subject of the email and add another variable to that. You can pretty easily get the hostname of a computer using built in powershell variables. Since you're new and sound like you want to learn I'm being kind of vague in the hopes that it will point you in the right direction of figuring it out yourself. Teach a man to fish blah blah blah. Also I'm too lazy to download the script myself and look at it
|
# ? Apr 17, 2015 02:22 |
|
I scanned over the file, and the guy commented the dick out of it. If those comments were maintained even remotely like the file is on that site, you should be fine to read through it and understand what's happening. It is daunting that there's a lot to read, but be cautious with powershell doing remove-item, because it will not go to the recycle bin. I am not certain you can recover those files once they are removed.
|
# ? Apr 17, 2015 04:22 |
|
Briantist posted:Accessing and managing AD in PowerShell is a dream I agree, in fact, I would say you don't even need to worry about outputting to excel spreadsheets because you can just do whatever it is you need to do. Instead of "Get, Format, Output, Read, Do" you can just do "Get, Do". quote:So I've got a dirty great log from a DNS server and I've written a nice PS one-liner that uses Select-String to find IPs in it. The idea is to go through the file, parse it and give an output of unique IPs, just one unique IP on each line. I've got Select-String working fine and defined IPs via a regular expression, using Select Matches I get a return of just the IP (like using grep or something). But now I want to use the Sort-Object function followed by the Get-Unique function. I've never used get-unique but there should be no reason why you can't use it. Both of these work for me: code:
code:
|
# ? Apr 17, 2015 16:05 |
|
Forgot that there was an optional -EmailSubject field that isn't required, but is automatically set to a string with a few variables. Just set $ComputerName = gc env:computername and called the variable in the default subject line. Not really too pretty, but it works. code:
|
# ? Apr 17, 2015 20:08 |
|
I've been converting my digital library over to MP4 using HandBrake and as I had thousands of files the UI would have been tedious to use. So, I wrote a quick PowerShell script that given a directory and file type will use the command line version of HandBrake to just auto convert everything to MP4. Just last weekend I completed everything and I'm streaming MP4 files via Plex to my Roku 3 just fine so I thought perhaps people here could use it. Usage is simple: .\CLIBatch.ps1 -cliPath <path to where HandBrake is installed> -sourceDirectory <directory to convert> -filter <file types, i.e., *.avi> Sample: .\CLIBatch.ps1 -cliPath 'C:\Program Files\Handbrake' -sourceDirectory D:\Scratch -filter *.avi And here's the code (sorry about the long string but HandBrake has a lot of command line switches): code:
|
# ? Apr 18, 2015 13:41 |
|
Use join-path to combine path parts, it will make your life easier.
|
# ? Apr 18, 2015 16:10 |
|
Ithaqua posted:Use join-path to combine path parts, it will make your life easier. code:
code:
|
# ? Apr 18, 2015 19:56 |
|
Is it ever actually worth it to force invoke the GC?code:
|
# ? Apr 20, 2015 21:01 |
|
Spazz posted:Is it ever actually worth it to force invoke the GC? If you have to ask then the answer is no
|
# ? Apr 20, 2015 23:48 |
|
I have a question about output formatting. Hours of googling hasn't helped. I'm trying to output an array of server names within an array of server sets. I've tried building custom objects with properties, so that I can use format-table, but a serverset object contains an array of strings as a property, which doesn't output properly. I can change the data structure if it helps solve the problem. The kind of formatting I'm looking for is like this: code:
Thanks for any advice.
|
# ? Apr 24, 2015 18:46 |
|
Nahrix posted:I have a question about output formatting. Hours of googling hasn't helped. code:
However going off of what you've given, I'm assuming you created a single object that contains a separate property (NoteProperty) for each server set, so what you might do is simultaneously create a ScriptProperty for each NoteProperty that joins the array with newlines, and then you can call Format-Table with those props only. code:
You can also look into the -View parameter of Format-Table and try working with a formatting file. I've never tried this before, so if you go that way please post your experience. Anyway, seeing some code of yours or having the object to work with would really go a long way in getting the output you want (not even touching on why you want that output yet). Briantist fucked around with this message at 21:41 on Apr 24, 2015 |
# ? Apr 24, 2015 21:35 |
|
You're correct; I have an object with NoteProperties to store the data. I deliberately didn't provide any code because I'm aware that the way that I chose to store the data might not be the best way to accomplish the desired output. I would be willing to change the way data was stored, allowing anyone willing to help the freedom to choose the easiest implementation. The reason I want that display format is because I'm working with a lot of servers / server sets, and I'm writing a script to manage deploying files to all of them, and I'd like it to open up with a view of all the servers on a single screen. That display format, in my opinion, maximizes the console's real-estate, so that I can view all servers simultaneously without having to scroll. Let me gather the xml, and I'll post it in a follow-up reply shortly. Edit: Here's 1 "serverset" object within the array of serversets Edit2: And here's the function that creates the objects, in case it's easier to follow than the xml Edit3: The ScriptProperty method you described works perfectly, thank you! Nahrix fucked around with this message at 23:00 on Apr 24, 2015 |
# ? Apr 24, 2015 22:31 |
|
Briantist: I just realised you're briantist.com. A blog I've referenced several times. Nice shout out from Ned Pyle the other day.
|
# ? Apr 27, 2015 03:16 |
|
Swink posted:Briantist: I just realised you're briantist.com. A blog I've referenced several times. Just curious, which articles have you referenced before?
|
# ? Apr 27, 2015 16:42 |
|
snackcakes posted:That's pretty cool. Is it possible to do the reverse? Enter a file and see what processes are using it? Hey I know this was from weeks ago now, but I've been meaning to post this and now it's cleaned up, so here: code:
zzMisc fucked around with this message at 19:06 on Apr 28, 2015 |
# ? Apr 28, 2015 18:46 |
|
Ali Aces posted:Hey I know this was from weeks ago now, but I've been meaning to post this and now it's cleaned up, so here: This is really cool, totally stealing this. Just a comment about this: code:
code:
Anyway it probably isn't important for this particular function, but I've been bitten by this before (not realizing how much time could pass between iterations).
|
# ? Apr 28, 2015 19:42 |
|
|
# ? May 15, 2024 03:36 |
|
Briantist posted:In theory I agree with the concept, but the way powershell's pipeline works means that you can't tell how long it will be between process{} calls, so the information could be stale as you proceed through each pipeline object. Consider this pipeline (as an example): I'm not sure I follow (still kind of a neophyte at this), but if I'm getting you correctly: the process{} block runs, and each iteration of it sends its output down the pipeline before the next iteration? It's already weird to me that the foreach loop doesn't seem to work like you'd expect it (I'd expect it to work in the begin{} block just as well, but it doesn't with pipeline input), but now I think I'm beginning to understand; so each call to write-output will actually send that output down the pipeline to the next, even before this function is finished? I didn't even realize that. In any case, yeah for this particular function I wasn't really concerned about the process list going stale while it's running, but it's possible to end up with multiple of the same process, pipe the first instance into stop-process then get errors on the rest since it's pulling them from a cache. Not sure that's really a problem worth fixing to me, but it is good to understand. So I guess what happens with pipeline input is: Every function in the pipeline's begin{} block is run The process{} block for the first command that's receiving pipeline input is run on the first object in the pipeline Any output from that process is sent on down to any following commands to process{} Then (or in parallel?) the first command process{}es the next element in its pipeline Once all elements are finished the end{} blocks are called Have I got that right?
|
# ? Apr 28, 2015 23:45 |