|
Start reading. http://ss64.com/nt/robocopy.html
|
# ? Oct 1, 2016 04:54 |
|
|
# ? May 10, 2024 00:05 |
|
Moundgarden posted:It performs like absolute garbage, presumably because of the triple wildcard in the filepath and all the sorting I need to do. I couldn't find a way around that, and unfortunately I have no power to modify the folder structure. Any tips on optimizing something like this or am I pretty much SOL? Get-ChildItem is notoriously slow, especially when working recursively with a large amount of files/folders: https://blogs.msdn.microsoft.com/powershell/2009/11/04/why-is-get-childitem-so-slow/ So yeah, Robocopy.
|
# ? Oct 1, 2016 09:46 |
|
I'm trying to figure out a way in PS (version 5) to read the metadata of a file, for example an image file, reading the EXIF data stored in it. When you go to the details tab of the file properties and see something like the camera used, that's the kind of label I want to read. Get-ItemProperty shows the properties, attributes, timestamps, etc, but I don't know how to query in on the other values. Any ideas?
|
# ? Oct 4, 2016 19:45 |
|
CLAM DOWN posted:I'm trying to figure out a way in PS (version 5) to read the metadata of a file, for example an image file, reading the EXIF data stored in it. When you go to the details tab of the file properties and see something like the camera used, that's the kind of label I want to read. Get-ItemProperty shows the properties, attributes, timestamps, etc, but I don't know how to query in on the other values. Any ideas?
|
# ? Oct 4, 2016 20:00 |
|
Uggggh COM objects. Yeah, I found something simliar to what I want to do here: http://mickitblog.blogspot.ca/2016/07/powershell-retrieving-file-details.html Ugggggggggggggh
|
# ? Oct 4, 2016 20:08 |
|
CLAM DOWN posted:Uggggh COM objects. Yeah, I found something simliar to what I want to do here: http://mickitblog.blogspot.ca/2016/07/powershell-retrieving-file-details.html You can use the FromFile(String) method of the System.Drawing.Image class to retrieve the metadata of an image file. Unfortunately it's not exactly easy to parse as the values are either integers or byte arrays. This article provides some info about how to parse it: https://msdn.microsoft.com/en-us/library/xddt0dz7(v=vs.110).aspx. It's still possible though. Using the info in that article I wrote this snippet that retrieves the value of the Equipment Manufacturer property item (ID 271 or 0x010F) from an image and then converts it to a string (The property has a type of 2 which indicates that it's a byte array of ASCII encoded text): code:
Pile Of Garbage fucked around with this message at 00:48 on Oct 5, 2016 |
# ? Oct 5, 2016 00:43 |
|
I'm working with Windows 7 and Powershell 2. I have a tab-separated value file that uses CR line endings instead of LF or CR/LF and Import-CSV isn't working. It doesn't seem to read and return anything, and doesn't produce any warnings or errors. This file comes from a state web app, with a button called SaveToXLS which produces this tab-separated values file with old Mac-style CR line endings and an XLS extension. Can I fix the line endings in Powershell 2? Or does Import-CSV support CR line endings in a later version? I can always just convert the line endings manually but I'd like to avoid any manual steps possible since the whole point of this project is to automate things... For extra points, the fields are all wrapped in ="<data>" and I'd like to remove those characters too, but I can probably figure that out on my own.
|
# ? Oct 6, 2016 00:39 |
|
Does this work in v2?code:
|
# ? Oct 6, 2016 01:30 |
|
Cpt.Wacky posted:I'm working with Windows 7 and Powershell 2. I have a tab-separated value file that uses CR line endings instead of LF or CR/LF and Import-CSV isn't working. Are you using -delimiter "`t" for tabs? If you had Powershell 3, it adds the -raw parameter (Get-Content ".\file.csv" -raw).Replace("`n","`r`n") Not sure if there's a workaround using .net or something. Edit: [System.IO.File]::ReadAllText(".\file.csv") I think that's what you want. Edit2: how big are the files. I think there's a stream version of IO stuff that would be much faster. Dr. Arbitrary fucked around with this message at 02:06 on Oct 6, 2016 |
# ? Oct 6, 2016 02:02 |
|
Mario posted:Does this work in v2? That does work. I had a similar idea overnight as I had already started using Get-Content to parse the data myself and it was handling the CR line endings correctly. Dr. Arbitrary posted:Are you using -delimiter "`t" for tabs? Yes, I'm using the tab delimiter. The files are ~100 lines so speed isn't much of an issue. Now that I'm able to import the CSV I get this type of data: code:
|
# ? Oct 6, 2016 18:46 |
|
Cpt.Wacky posted:That does work. I had a similar idea overnight as I had already started using Get-Content to parse the data myself and it was handling the CR line endings correctly. anthonypants fucked around with this message at 19:05 on Oct 6, 2016 |
# ? Oct 6, 2016 19:03 |
|
anthonypants posted:Does v2 not have Import-Csv? Does the actual .csv text contain text like ="Name",="Dude, The"? Import-CSV in v2 doesn't handle the CR line endings correctly. This is what I'm reading in: code:
|
# ? Oct 6, 2016 19:31 |
|
Cpt.Wacky posted:Import-CSV in v2 doesn't handle the CR line endings correctly. This is what I'm reading in:
|
# ? Oct 6, 2016 19:39 |
|
anthonypants posted:What character is the <CR> in your file? It might be easier to replace that character with one Import-Csv likes. Also, can you set the delimiter to '`t='? v2 doesn't have get-content -raw So you have to use the thingie I posted earlier.
|
# ? Oct 6, 2016 20:17 |
|
anthonypants posted:What character is the <CR> in your file? It might be easier to replace that character with one Import-Csv likes. Also, can you set the delimiter to '`t='? It's a carriage return, decimal byte 13, hex 0x0D. ConvertFrom-CSV doesn't like "`t=", saying it must be exactly one character long. It looks like this is what I needed: code:
|
# ? Oct 6, 2016 21:08 |
|
I'm scripting out some service cycling, and I need to do it in a specific order. I'm trying to put together a script to essentially bring down an environment gracefully - a process which normally involves myself, another sys admin, or someone from operations clicking like a madman for 15 minutes with half a dozen service dependencies in mind. Service A has to be started before service B, don't stop B if C is still running, and so forth. We're looking at probably 25 services, maybe half with dependencies I want to address, so being able to cycle these in a specified order would be great. So for stopping or for starting, the script is almost identical code:
Given what I'm trying to accomplish, is there an approach which doesn't involve 372 nested foreach loops here?
|
# ? Oct 11, 2016 05:04 |
|
Do you have Powershell 3?pre:$a = [ordered]@{a=1;b=2;d=3;c=4}
|
# ? Oct 11, 2016 05:08 |
|
When using ordered in this case, I'd need to specify every service in the script itself and not in a text file, no? I don't see a way to use both, essentially a $services = [ordered]stopservices.txt approach. Placing the services directly into the script, essentially going fromcode:
code:
Script 1 stopping in order: Base Filtering Engine, DHCP Client, IPsec Policy Agent, Task Scheduler Script 2 stopping in order: DHCP Client, Task Scheduler, IPsec Policy Agent, Base Filtering Engine Would that be right? Edit: Heh, you know what, this whole thing may be pointless. There is absolutely no reason to do this, and I mean I appreciate a good learning opportunity as much as the next guy, but this is a straight up copy paste situation. Forest for the trees, missed the drat forest for the trees. code:
MC Fruit Stripe fucked around with this message at 05:30 on Oct 11, 2016 |
# ? Oct 11, 2016 05:22 |
|
MC Fruit Stripe posted:When using ordered in this case, I'd need to specify every service in the script itself and not in a text file, no? I don't see a way to use both, essentially a $services = [ordered]stopservices.txt approach. Placing the services directly into the script, essentially going from Sounds like you have that one pretty much solved, but could you use import-csv instead of get-content, as import-csv preserves ordering?
|
# ? Oct 11, 2016 18:21 |
|
Question and advice needed about powershell! Question - How come the PSSnappin command is used for some things instead of import-module? Is there really any difference between these? Advice - I have 3 web servers. During a DR scenario, one or two of them might be offline. I am looking to create a powershell script which is on a monitoring server that will attempt to connect to any of the servers and execute a powershell script either stored locally on the servers or remotely. The script itself only needs to be executed on any one server that is up, not on every server that is up. What would be the best way to go about this? Just looking for a general direction.
|
# ? Oct 13, 2016 05:59 |
|
lol internet. posted:
Snapins were deprecated in favor of modules sometime between v2 and v3. For your second thing you can use if (test-path \\server01\share) {run script} elseif (test-path \\server02\share) {run script} elseif (test-path \\server03\share) {run script} else {echo "gently caress" > c:\failed.log} Anyone do any recursion? I like nice looking code as much as the next guy but I'm golfing for a 1 liner to print enabled users and subgroups from a given AD group and ideally want to pipe the subgroups back into the original query and enumerate those too Import-module activedirectory; get-adgroupmember 'esx admins' | where {$_.objectclass -eq 'group' -or ($_.objectclass -eq 'user' -and ((get-aduser $_.samaccountname).Enabled -eq 'True')) } | select samaccountname, name, distinguishedname, objectclass | ft -auto Roargasm fucked around with this message at 15:59 on Oct 13, 2016 |
# ? Oct 13, 2016 15:54 |
|
Thank you! Didn't realize test-path worked on UNC paths but when you think about it I guess why wouldn't it.
|
# ? Oct 13, 2016 16:16 |
|
Roargasm posted:Anyone do any recursion? I like nice looking code as much as the next guy but I'm golfing for a 1 liner to print enabled users and subgroups from a given AD group and ideally want to pipe the subgroups back into the original query and enumerate those too I recently implemented a recursive algorithm in PowerShell for the first time and didn't run into any problems. Just keep in mind all the usual CS 101 stuff about recursions (head/tail, putting in breaks against infinite loops) because otherwise you'll have to kill a LOT of PoweShellISE.exe processes.
|
# ? Oct 13, 2016 16:37 |
|
sloshmonger posted:I've pretty much already done 3 & 4 in a script that was supposed to step through all subnet IPs and inventory them. Forgot to say this earlier, but thanks! I'm trying to implement it by querying AD names instead of IP addresses but the overall structure has been real helpful so far. Also, saw this on r/powershell and it's fantastic for trolling the other members of my department.
|
# ? Oct 14, 2016 17:24 |
|
If you think that's great then you need to check out cheese-cube's post on the previous page.
|
# ? Oct 14, 2016 17:33 |
|
anthonypants posted:If you think that's great then you need to check out cheese-cube's post on the previous page. Ooh, nice. Just need to figure out how to run it remotely. Invoke-command doesn't do the trick.
|
# ? Oct 14, 2016 20:09 |
|
Just want to note that the only reason I Base64 encoded the URL was so that I could post it on Twitter and avoid their auto URL parser. Also if you really want to be an rear end in a top hat you can render their computer unusable with this: code:
Edit: I guess they could kill the powershell.exe process remotely...
|
# ? Oct 15, 2016 03:25 |
|
I wanted a script to get the second-to-last day of the month, so I took this script and modified it.code:
|
# ? Oct 18, 2016 22:40 |
|
Good stuff. Minor optimisation, you could use the DateTime.DaysInMonth method and replace the ForEach-Object loop with a while loop (Who knows, maybe there will be a month with more than 32 days lol):code:
|
# ? Oct 19, 2016 03:12 |
|
cheese-cube posted:Good stuff. Minor optimisation, you could use the DateTime.DaysInMonth method and replace the ForEach-Object loop with a while loop (Who knows, maybe there will be a month with more than 32 days lol):
|
# ? Oct 19, 2016 04:31 |
|
Is there any way to create a function in the calling scope? For variables you can do stuff like set-variable -scope 1 but it seems like (at least on powershell 2 which is what I was using) there's no way to do this for functions. Not that this is a particularly good idea anyway, but I was bored and trying to work around the lack of first class function by messing with script blocks.
|
# ? Oct 20, 2016 02:45 |
|
I made some modifications to this code that allows a powershell script to trigger a uac prompt. Specifically, I've modified it so it can run as a function, and can run from a mapped network drive. Call this function in a script that needs admin privileges, it will trigger a uac prompt, and the re-launch the script with the credentials you entered in the UAC prompt. I don't know how useful this is to other people, but I'm getting some mileage out of it. code:
|
# ? Oct 28, 2016 01:04 |
|
Hey with PowerShell gallery modules- what does the x and c prefix denote? I can't find a definitive answer on Google and it's driving me mad
|
# ? Oct 30, 2016 16:10 |
|
The Fool posted:I made some modifications to this code that allows a powershell script to trigger a uac prompt. Specifically, I've modified it so it can run as a function, and can run from a mapped network drive. Nice, this is useful. I was recently banging my head against the wall trying to get a script to elevate properly when run interactively from a batch file (I created it for helldesk people and wanted to pass it some set parameters). The bodge I came up with was using Start-Process with the RunAs verb: code:
Walked posted:Hey with PowerShell gallery modules- what does the x and c prefix denote? I can't find a definitive answer on Google and it's driving me mad Not sure what you're referring to, can you provide an example?
|
# ? Oct 31, 2016 00:47 |
|
The Fool posted:I made some modifications to this code that allows a powershell script to trigger a uac prompt. Specifically, I've modified it so it can run as a function, and can run from a mapped network drive. I've always just stuck this on top of any script that needs to run as admin and might get used by the helpdesk. code:
|
# ? Nov 1, 2016 01:54 |
|
cheese-cube posted:
Sorry missed this; but did manage to track down the info I was after. xModuleName denotes "experimental" cModuleName denotes "community provided" I was trying to figure out what to release my module in the PS gallery as, naming-wise, and cModuleName was the way to go.
|
# ? Nov 1, 2016 02:14 |
|
Late, but...sloshmonger posted:I've pretty much already done 3 & 4 in a script that was supposed to step through all subnet IPs and inventory them. code:
|
# ? Nov 1, 2016 12:48 |
I'm writing a password reset script and I figured out how to get it to say "Sorry! That user doesn't exist" but I want it to return to the start of the script when that happens. Is there an easy way to do this? I've looked at a few things like erroraction but I don't think this applies when I do GetADUser and want it to either display the results or say "whoops!". If I need to start over and look at it from another angle I'd appreciate any tips on where to start! edit: Sorry, forgot I wasn't using getADuser. This is what I'm using to return whether the account exists or not: code:
milk milk lemonade fucked around with this message at 16:23 on Nov 1, 2016 |
|
# ? Nov 1, 2016 16:20 |
|
milk milk lemonade posted:I'm writing a password reset script and I figured out how to get it to say "Sorry! That user doesn't exist" but I want it to return to the start of the script when that happens. Is there an easy way to do this? I've looked at a few things like erroraction but I don't think this applies when I do GetADUser and want it to either display the results or say "whoops!". If I need to start over and look at it from another angle I'd appreciate any tips on where to start! There's a lot of ways to handle this (relatively easily, too); can you post your full code of psuedo code, not just the if/else block
|
# ? Nov 1, 2016 16:28 |
|
|
# ? May 10, 2024 00:05 |
Sure:code:
|
|
# ? Nov 1, 2016 16:30 |