Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Toshimo
Aug 23, 2012

He's outta line...

But he's right!

Zaepho posted:

Have you looked at https://psappdeploytoolkit.com/? It has a bunch of pre-canned stuff specifically for app deployments that you might be able to make use of and let somebody else maintain and improve.

It is certainly something I've considered, when we get enough breathing room that I can start looking at better solutions, but for the moment, I've got an overwhelming backlog of several thousand applications that I have to review, so I fix what I can, and push the big stuff to next year's Toshimo.

Adbot
ADBOT LOVES YOU

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

New Yorp New Yorp posted:

This is insane and needs to be fixed immediately. There is zero excuse to not use source control and any manager who hasn't enforced it years ago is straight up incompetent.

Also, start using Pester to write tests for your scripts.

Then take your newfound source control and hook it up to a CI system and run the tests on every commit.

Yeah, we just have everything tossed up on a SMB share. My boss is all for source control, as is my team, and I've got a BitBucket set aside for it, but I haven't had enough time to figure out how I'm going to use it (again, we've got several thousand scripts that never really get updated, so it's a weird case).

I'll look into Pester, thanks.

Toshimo
Aug 23, 2012

He's outta line...

But he's right!
I am losing my goddamn mind and this has to be the simplest thing.


PowerShell code:

clear

$input_file="C:\Users\266520\Documents\test-data.txt"

 

$payload = Get-Content $input_file

 

echo "---"

echo "File Contents:"

echo $payload

echo "---"

 

$payload -match '(?<dollars>\d+)\.(?<cents>\d+)'

$Matches

Output:



---

File Contents:

Alpha,1,2/3/22,22.99,76.70,123-45-6789

Beta,999,4/5/1999,.75,63.69,234-56-789

---

Alpha,1,2/3/22,22.99,76.70,123-45-6789

Beta,999,4/5/1999,.75,63.69,234-56-789

 

Name                           Value                                                                                                                                                  

----                           -----                                                                                                                                                  

domain                         CONTOSO                                                                                                                                               

user                           jsmith                                                                                                                                                 

0                              was CONTOSO\jsmith        
    


Why the heck is $Matches not populating and why am I not getting a boolean output from -match?

Toshimo fucked around with this message at 19:10 on Aug 10, 2022

Toshimo
Aug 23, 2012

He's outta line...

But he's right!
Thanks, yeah, I forgot that Get-Member doesn't tell you that it's an array unless you use -inputObject because reasons... Which I still don't think -match should just drop on the floor, but here we are.

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

Toast Museum posted:

To come at the problem from another direction, if the sample data is representative of how the real data looks, have you considered using Import-Csv instead of messing around with regex?

I did, but it's not my data, and the only thing the person wants is to be able to change the money amounts easily, instead of having to do it in excel one-by-one. I had expected it to be a quick thing I knocked out over coffee until I got side-tracked over -match being buggy.

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

New Yorp New Yorp posted:

Use import-csv and export-csv. They are designed for reading and writing csv data.

I understand what they are for, but I have never seen the data, so I don't even know if they are in legit CSV format or anything.

Toshimo
Aug 23, 2012

He's outta line...

But he's right!
Losing my drat mind on this one. Got a bunch of folders I was looking to zip up (and I'll wind up doing with 7zip because this isn't working).

Reduced it to the very basic level (I've tried a dozen flavors of this):
PowerShell code:
gci  . | ForEach-Object -Process { Compress-Archive $_ $_ -Verbose }
The first instance of $_ just returns (or is interpreted as) nothing. Second one is fine.

Output:

VERBOSE: Preparing to compress...
VERBOSE: Performing the operation "Compress-Archive" on target "".
VERBOSE: The partially created archive file 'v:\Marvel\Alias\Alias - 022 [2003].zip' is deleted as it is not usable.
VERBOSE: The archive file path 'v:\Marvel\Alias\Alias - 023 [2003]' supplied to the DestinationPath patameter does not include .zip extension. Hence .zip is appended to the supplied DestinationPath path and the archive file would be created at 'v:\Marvel\Alias\Alias - 023 [2003].zip'.

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

Mario posted:

It's the square brackets -- use LiteralPath instead of Path:
PowerShell code:
Get-ChildItem -Path . -Directory | ForEach-Object -Process { Compress-Archive -LiteralPath $_ -DestinationPath $_ -Verbose }
https://superuser.com/questions/212808/powershell-bug-in-copy-move-rename-when-filename-contains-square-bracket-charac

Also added -Directory to the Get-ChildItem so that it doesn't match the (newly created) zip files.

Thanks, bud. Now, if only they hadn't hard-coded Compress-Archive to require a .zip extension.

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

kumba posted:

Performance improvement question:

There's a lot to unpack here, but (a) as mentioned, HTML parsing is generally better done a different way, but if you want to do it this sort of way (b) avoid arrays and definitely avoid using "+=" to add things to arrays because it's a massive performance hit.

There's probably a lot of ways that others will find to improve this, but here's a first pass (it's not going to give you all your pretty host output:

PowerShell code:
[psobject]$exclusions = @('number','number?','number.','number,','chatting','contacting','account','account.','account?','account,','please','thank','display:','.attachmentIcon','inline-block;','background-size:','background-image:','styleheadbodybCampaign','width:','height:','16px;')
Get-ChildItem -Path Z:\ -Include "*_CS Chat_*.html" -File -Recurse | ForEach-Object {
    (Get-Content -Path $_.FullName.ToString()).Split(' ').Trim('.',"`'","`"",",") | Where-Object { ($_.Length -lt 30) -and  
                                                                        ($_.Length -gt 0) -and 
                                                                        (-not ($_ -match "[`<`>]")) -and
                                                                        (-not ($_ -match "https|Omni")) -and
                                                                        (-not ($_ -in $exclusions))
                                                         }  
    } | ForEach-Object { $_ -replace '[\W]','' } | Set-Content -Path C:\temp\output.txt
Let me know if that's at least a little faster/more accurate.

Toshimo fucked around with this message at 22:12 on Nov 16, 2022

Toshimo
Aug 23, 2012

He's outta line...

But he's right!
If you want to skip ahead and just group up your words and count them so you can just feed that into something else, go with the following for your last line:

PowerShell code:
    } | ForEach-Object { $_ -replace '[\W]','' } | Group-Object | Select-Object Name,Count | Sort-Object Name | Set-Content -Path C:\temp\output.txt

Toshimo
Aug 23, 2012

He's outta line...

But he's right!
It's a maybe. You probably don't need Set-Location at all, but it doesn't hurt.

What are you doing with the switch, exactly? Like, what does checking the first 2 characters do?

It would probably be rasiest if you just gave us a dummy name like "JDoe" and said like:

  1. Creates R:\JDoe
  2. Sets JDoe to have Full Control of R;\JDoe
  3. Creates R:\JDoe\Public
  4. Sets Authenticated Users to have Full Control on R:\JDoe\Public

Or whatever you are doing

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

Submarine Sandpaper posted:

ACLs sorta suck unless they've changed things in PowerShell.

They kinda did but they still suck and I'll dig out what I used last time but I think I was still calling .Net methods

Toshimo
Aug 23, 2012

He's outta line...

But he's right!
The way you've described it, you want something like this:

PowerShell code:
$username = Read-Host -Prompt 'Provide username'
$office_code = $username.Substring(0,2)
$user_path = "C:\Test\$office_code\$username"

if( -not (Test-Path -Path $user_path)) {
    New-Item -Itemtype Directory -Path $user_path
} else {
    Write-Output "Collision detected at path `"$user_path`""
}

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

Pile Of Garbage posted:

Probably posted this before but I highly recommend specifying the PathType parameter whenever you're using Test-Path: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-5.1#-pathtype.

It's a minor thing for a probably unlikely edge-case but it can easily help to avoid confusion and issues when you put your things into prod.

True, but most of the time, I don't bother because I'd set it to Any because of how many compatibility issues I have if a file and a folder with the same name exist, so I just bail on either.

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

Pile Of Garbage posted:

Genuinely interested, what environment are you in where that is a compatibility issue? Windows and POSIX has no issues with files and folders having the same name, what monstrosity are you using which doesn't?

Oh, it's always some in-house piece of poo poo or something from a garbage mom-and-pop vendor who didn't account for it.

Toshimo
Aug 23, 2012

He's outta line...

But he's right!

Pile Of Garbage posted:

Guessing you work for an MSP, unless you consider say Atlassian or SAP to be mom-and-pops lol. Whether you're uplifting existing code or integrating with legacy code it's still beneficial to use strict path checking with the PathType parameter. Pro-tip: you can use Test-Path in ValidateScript statements. See also: the Uri class that you can use to vaidate URIs to make sure they're well-formed. https://learn.microsoft.com/en-us/dotnet/api/system.uri?view=net-7.0

No, I work for a monolithic agency in the US Feddral Government.

Adbot
ADBOT LOVES YOU

Toshimo
Aug 23, 2012

He's outta line...

But he's right!
Messing around with this year's Advent of Code in PS and on Day 2 (Part 2)... I have successfully solved it but I can't help but feel the way I wrote it was "some poo poo".

Any thoughts on how I could cut some of this down?

https://gist.github.com/Toshimo-Kamiya/c4c64fa4cea477bded97a14524e75ff6

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply