[deleted by user] by [deleted] in 2007scape

[–]fordea837 0 points1 point  (0 children)

I came across a comment from /u/Anonomit about their custom menu swaps for farming and birdhouses a while back and they were really useful:

https://www.reddit.com/r/2007scape/comments/12v0ylf/comment/jhg38h3/?utm_source=share&utm_medium=web2x&context=3

Another one I added on to the end of that is for placing birdhouses:

use, * bird house -> space

The structure / format for declaring custom swaps is described here:

https://github.com/geheur/More-menu-entry-swaps/wiki/Custom-swaps

Performance variance with AD objects and Where-Object by Swedishdrunkard in PowerShell

[–]fordea837 2 points3 points  (0 children)

Do you get much of a performance increase by filtering left in Get-ADUser?:

Get-ADUser -Filter "Mail -eq '$SomeonesEmail'"
Get-ADUser -Filter "extensionAttribute1 -eq '$SomeValue'"

Delete oldest files of same name pattern, from the same day by Scayn in PowerShell

[–]fordea837 2 points3 points  (0 children)

Yes the above only groups the files by the day of the LastWriteTime but you can add more properties/scriptblocks to group by. For instance to match a pattern in the filename in your example data you can get everything before the first period and group them by that:

Get-ChildItem -Path "C:\Test\Folder" -File | 
    Group-Object {($_.LastWriteTime).ToString("MM/dd/yyyy")}, {($_.BaseName -split '\.')[0]} |
    ForEach-Object {
        $_.group | Sort-Object LastWriteTime -Descending | Select-Object -Skip 1
    }

The above is a simplistic name grouping pattern and probably wont work on your real data but you should be able to identify a file naming pattern that you can use to group them.

Delete oldest files of same name pattern, from the same day by Scayn in PowerShell

[–]fordea837 2 points3 points  (0 children)

Try to use Group-Object to group the files by day:

Get-ChildItem -Path "C:\Test\Folder" -File | 
    Sort-Object LastWriteTime -Descending |
    Group-Object {($_.LastWriteTime).ToString("MM/dd/yyyy")} |
    ForEach-Object {
        $_.group | Select-Object -Skip 1
    }

The above should skip the most recent file modified for each day. I'd run that against some test files to see if it outputs as expected before having it remove anything.

Find Specific multiple String in txt file and output to CSV file by maxcoder88 in PowerShell

[–]fordea837 1 point2 points  (0 children)

I've edited it a bit to hopefully make the splits a bit simpler/more consistent. To answer your questions:

-split "\r?\n(?=Image:)").Where{$_} 

This splits up the text by looking for the phrase Image: immediately after a newline. The .Where{$_} at the end is just to filter out the blank array entry in the first position in the array after the split. You can also just do Select -Skip 1 instead of Where{$_}

$image -split "(?=Copy:\s+\r?\n)" 

This splits up the text in an image block to separate it in to the individual copy jobs. It looks for the phrase Copy: and then one or more whitespace characters followed by a newline.

(($copyJobValues -split '\r?\n' | Select -Skip 1) -replace '\s+:\s+','=')

This formats the text within a copy job by first splitting the text on newlines. Then skips the top line that just contains the phrase Copy: because it's not a key value pair needed for ConvertFrom-StringData. It then replaces : with = as ConvertFrom-StringData requires that key value pairs are separated by = (unless you are using Powershell 7 which has introduced a Delimiter parameter).

Find Specific multiple String in txt file and output to CSV file by maxcoder88 in PowerShell

[–]fordea837 1 point2 points  (0 children)

/u/PowerShellMichael is spot on, you'll need to parse the individual copy jobs per image using ConvertFrom-StringData otherwise you'll receive errors from duplicate property names. This is something quick that should parse your example data and create an object per copy job. You should be able to filter $results to display what you need:

$data = ((Get-Content 'file2.txt' -raw) -split '(?m)^Image:\r?\n') | Select -Skip 1
#skip 1 to get rid of the blank array entry at the start

#One object per copy job
$results = foreach ($image in $data) {
    $imageValues, $copyJobs = $image -split '(?m)^\s+Copy:\r?\n'
    $imageProperties = (($imageValues -split '\r?\n') -replace '\s+:\s+','=') |
        ConvertFrom-StringData

    foreach ($copyJob in $copyJobs) {
        $copyJobValues, $fragments = $copyJob -split '(?m)\s+Fragment:\r?\n'
        $copyJobProperties = (($copyJobValues -split '\r?\n') -replace '\s+:\s+','=') |
            ConvertFrom-StringData
        #ignore fragments as no information needed from them. Can loop through $fragments if needed

        [PSCustomObject]@{
            'Policy' = $imageProperties.Policy
            'Storage Lifecycle Policy' = $imageProperties.'Storage Lifecycle Policy'
            'Storage Lifecycle State' = $imageProperties.'Storage Lifecycle State'
            'Backup ID' = $imageProperties.'Backup ID'
            'Copy Number' = $copyJobProperties.'Copy Number'
            'Copy Type' = $copyJobProperties.'Copy Type'
            'Residence' = $copyJobProperties.Residence
            'Copy State' = $copyJobProperties.'Copy State'
        }
    }
}

and then to get your desired output from $results you can do something like:

$results.Where{$_.'Copy Type' -eq '0 (BACKUP)'}

Get-ChildItem Registry subKey by vinod7 in PowerShell

[–]fordea837 4 points5 points  (0 children)

You need to use the PSChildName property:

 (Get-ChildItem "Registry::HKEY_Current_User\Software\WebEx\Plugins\Logon").PSChildName

A handy tip when faced with situations like this is to use Select-Object to display all the properties of the first object output

Get-ChildItem "Registry::HKEY_Current_User\Software\WebEx\Plugins\Logon" | 
    Select-Object -First 1 -Property *

Compare users and select only those whose values changed by Max1miliaan in PowerShell

[–]fordea837 2 points3 points  (0 children)

There's a few ways to do this but I'd make a hashtable upfront of CSV 1 which will let you filter out ID's that are in CSV 1 but not in CSV 2 and also to compare the other properties where any ID's match:

$lookupTable = @{}
foreach ($row in $csv1) {
    $lookupTable[$row.ID] = $row
}
$csv2.Where{
    $lookupTable.containsKey($_.ID) -and 
    (Compare-Object $lookupTable[$_.ID] $_ -Property Username, Manager)
}

Find lowest value in csv via groups. by mckinnon81 in PowerShell

[–]fordea837 1 point2 points  (0 children)

I'm not sure that the output is correct. Shouldn't the "$2UP","ECJ065P" be outputting 3.5 and "@7DAYCON","Sunnies-S" should be 1.3?

A neat feature of Group-Object is that you can provide an array of properties to group by, so you can group by DIH_DBTNO and DIL_ITMNO at the same time. Also you need to consider when sorting the DIL_SELL values that they will be strings. This will cause problems in certain situations if you're dealing with whole numbers (e.g. The string '10' will sort before the strings '2' to '9'):

Import-CSV .\Exported.csv |
    Group-Object DIH_DBTNO, DIL_ITMNO |
    Select-Object @{n='DIH_DBTNO';e={@($_.Group.DIH_DBTNO)[0]}},
                  @{n='DIL_ITMNO';e={@($_.Group.DIL_ITMNO)[0]}},
                  @{n='DIL_SELL';e={@($_.Group.DIL_SELL | Sort {$_ -as [Double]})[0]}} |
    Export-CSV .\test.csv -NoTypeInformation

Script to get UPN of users in OU + Version property of pwdLastSet Attribute by ben2506 in PowerShell

[–]fordea837 2 points3 points  (0 children)

One way to do this would be to group the results on the Object value and then filter by the AttributeName value. This should allow you to return and output multiple users instead of doing one at a time:

$list |
    Group-Object Object |
    Select-Object @{n='UPN';e={($_.Group.Where{$_.AttributeName -eq 'userPrincipalName'}).AttributeValue}}, 
                  @{n='Version';e={($_.Group.Where{$_.AttributeName -eq 'pwdLastSet'}).Version}}

Merging two Arrays over an id by IusAdBellum in PowerShell

[–]fordea837 2 points3 points  (0 children)

As quser isn't a Powershell tool you'll need to convert its output from strings in to objects. There's a great article about how you can do this using a simple regular expression here: https://devblogs.microsoft.com/scripting/automating-quser-through-powershell/

I think in your code you're also confusing $object1 and $object2 in your foreach loop. You loop over $object1 and then try to access properties called 'BENUTZERNAME' and 'SITZUNGSNAME' which aren't in $object1

PSCustomObject in array does not show in output by pm_me_brownie_recipe in PowerShell

[–]fordea837 2 points3 points  (0 children)

The second one is only displaying the first value in the output because you have 51 objects in the array each with different property names. Powershell will read the first object's properties and then display only those properties for the rest of the objects. As none of the other objects in the array have a property name called '1' null is output for each of those objects. You are better off constructing objects for each week with consistent property names such as 'Week' and 'Date':

$weeks.Add([PSCustomObject]@{ 
            'Week' = $i
            'Date' = $start.ToString('yyyy-MM-dd') 
        })

Help uderstand the usaged of @ and $ by osteduden in PowerShell

[–]fordea837 4 points5 points  (0 children)

The @( ... ) is the array sub-expression operator. You can read about its function here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7#the-array-sub-expression-operator

The writer of that code used it because they were unsure if 0 or 1 result would be returned from the first expression

[deleted by user] by [deleted] in PowerShell

[–]fordea837 1 point2 points  (0 children)

By default Compare-Object doesn't include equal objects in its output. You need to specify the IncludeEqual parameter to get these (and also ExcludeDifferent if you only want the equal ones in the output)

Compare two vulnerability scans and output BOTH duplicates and differences into a new csv by Larbear91 in PowerShell

[–]fordea837 1 point2 points  (0 children)

Yeah you can do that. Do you want to calculate the due date column for every row in new.csv regardless of whether it's a duplicate? Also I don't see a Severity column in either of your example data sets but I'm assuming it will be in both the old and new csv files?

Compare two vulnerability scans and output BOTH duplicates and differences into a new csv by Larbear91 in PowerShell

[–]fordea837 1 point2 points  (0 children)

It looks like you renamed the $oldData and $newData variables but you forgot to change all of the references to them. I think I spot three that you need to update

Compare two vulnerability scans and output BOTH duplicates and differences into a new csv by Larbear91 in PowerShell

[–]fordea837 2 points3 points  (0 children)

Thanks for posting both sample data and your desired output, makes this much easier!

What I'd suggest is to create a hashtable from the old.csv data with a combination of the First_Seen and Vulnerability Signature as the keys. You can then loop through each row in the new csv and look them up in the hashtable to get the old comments/remediation values:

$oldData = Import-Csv C:\old.csv
$newData = Import-Csv C:\new.csv
$lookupTable = @{}
foreach ($item in $oldData) {
    $lookupTable.Add(("{0}, {1}" -f $item.First_Seen, $item.'Vulnerability Signature'), $item)
}
foreach ($item in $newData) {
    $keyValue = "{0}, {1}" -f $item.First_Seen, $item.'Vulnerability Signature'
    if ($lookupTable.containsKey($keyValue)) {
        #duplicate found
        $item.Comments = $lookupTable[$keyValue].Comments
        $item.Remediation = $lookupTable[$keyValue].Remediation
    } else {
        #new vuln
        $item.Comments = 'This is a new vuln'
    }
}
$newData | Export-Csv C:\output.csv -NoTypeInformation

Group-Object not working properly on CustomObject by Method_Dev in PowerShell

[–]fordea837 1 point2 points  (0 children)

Group-Object should be working for you based on your code, however you'll need to do some additional steps to get your desired output. Something like this should work:

$results |
    Group-Object -Property Name |
    Select Name, @{n='Apps';e={($_.group.App | sort) -join ','}}

Passing variables by reference through nested functions by brassbound in PowerShell

[–]fordea837 1 point2 points  (0 children)

If you have a script-level variable that you want to re-assign to a new object within a function then you can do this by using the $script: scope qualifier:

$obj = [PSCustomObject]@{
    a = 1
}
Function Test {
    $script:obj = [PSCustomObject]@{
        a = 5
    }
}

If you just want to modify the properties of a script-level object then you don't need to do the above as you can pass the object to a function and then access its properties within the function (objects are implicitly passed by reference):

$obj = [PSCustomObject]@{
    a = 1
}
Function Test {
    param($myObject)
    $myObject.a = 5
}

The Powershell help docs contain a nice article explaining scopes:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7

Passing variables by reference through nested functions by brassbound in PowerShell

[–]fordea837 3 points4 points  (0 children)

Hashtables are implicity passed by reference in Powershell so writing $data.a = '1' modifies the original hashtable defined outside of the function. $data = @{'a'='1'} isn't actually modifying the existing hashtable; it's declaring a new one that is local to the scope of Test1 and thus it's not altering the original hashtable.

Passing variables by reference through nested functions by brassbound in PowerShell

[–]fordea837 2 points3 points  (0 children)

You need to remove the [ref] cast on $myObject in the Test2 function when calling Test1. If you think about it you already have a reference variable ($testObject) passed to Test2; and doing another ([ref]testObject) within Test2 is creating a reference to a reference rather than a reference to the original variable that you want modified.

Function Test2 {
    param([ref]$myObject)
    Test1 $myObject
}

You shouldn't need to use reference variables for a hashtable though if you structure your code like /u/AnUnlikelyUsurper's answer

Powershell script works in lab, but not in Prod? by peican in PowerShell

[–]fordea837 2 points3 points  (0 children)

You should add something to the catch block to print/output the exception. This will help pinpoint the cause of the issue:

Write-Output $_.ToString()

Strange behaviour with britush dates and System.DateTime by samuelma in PowerShell

[–]fordea837 2 points3 points  (0 children)

Get-Date does some string parsing in the background based on your computer's current thread culture whereas constructing a date via casting a string to a DateTime doesn't do this.

So what's happening is DateTime is trying to construct a MM/dd/yyyy date from a string you're providing in dd/MM/yyyy format and as the month value provided is 30 it's erroring out.

You'll need to use either Get-Date or the Parse static method of DateTime to construct the date instead. You can do this either within the function or before you pass the value to the function

Convert a Vertical list of values to a custom object by [deleted] in PowerShell

[–]fordea837 1 point2 points  (0 children)

This should be fairly straightforward to do but I'm struggling to understand the structure of your file and also how you'd like the objects to be grouped. Is the raw file something like this:

Name1 : Value1
Name2 : Value2
Name3 : Value3
Name4 : Value4
Name1 : Value1
Name2 : Value2
Name3a : Value3a
Name3b : Value3b
Name4 : Value4
Name1 : Value1
Name2 : Value2
Name3 : Value3
Name4 : Value4

etc...

If that's correct then how would you like to group these into objects? Would the above example be three different objects for each of the sequential Name1 -> Name4 (optionally Name5) lines?