0

I receive a set of files daily that I have to decrypt. I do this by running a PowerShell script, inputting the password and file name each time. I would like to automate this, but every time I run it, the file name gets inputted with extra data. How can I write this to only pass the file name? The two files I get end with pwd.enc and tar.enc and runs with OpenSSL:

$pwd  = Get-ChildItem D:\FILES\Claims\TEST -Filter "*.pwd.enc*" |
        Select-Object Name
$tar  = Get-ChildItem D:\FILES\Claims\TEST -Filter "*.tar.enc*" |
        Select-Object Name
$file = Get-ChildItem D:\FILES\Claims\TEST -Filter "*.tar.enc*" |
        Select-Object BaseName
dec_utility.exe rsa_key_123456.pri D:\FILES\Claims\TEST\$pwd D:\FILES\Claims\TEST\$tar D:\FILES\Claims\TEST\$file
1
  • If you have more than one file in the directory containing ".tar.enc" or ".pwd.enc" it will return more than one object name. You can use the 'FullName' property instead of 'Name' and 'BaseName' to return the full path, so you don't have to concatenate paths for your final command. Commented Apr 18, 2016 at 18:41

2 Answers 2

3

Get-ChildItem | Select-Object Name creates an object per recieved item (file from Get-ChildItem) with a Name-property. You want to access the value of Name.

You could use (Get-ChildItem -Filter "*.pwd.enc*").Name like you answered yourself, but with PS 2.0 this would fail if it found multiple files. I usually recommend -ExpandProperty or a Foreach-Object-loop. Ex..

#Selects the value stored inside Name
$pwd = Get-ChildItem D:\FILES\Claims\TEST -Filter "*.pwd.enc*" | Select-Object -ExpandProperty Name

#Foreach item, access the Name-property
$pwd = Get-ChildItem D:\FILES\Claims\TEST -Filter "*.pwd.enc*" | Foreach-Object { $_.Name }

Be aware that your dec_utility.exe-command will fail it the commands above find more than one file. You can use Select-Object -First 1 or access the first item in an array (index 0) to only get the first value.

$pwd = @(Get-ChildItem D:\FILES\Claims\TEST -Filter "*.pwd.enc*" | Select-Object -ExpandProperty Name)[0]

#or
$pwd = Get-ChildItem D:\FILES\Claims\TEST -Filter "*.pwd.enc*" | Select-Object -First 1 -ExpandProperty Name
Sign up to request clarification or add additional context in comments.

3 Comments

just a note-- dir, gci, and ls all do the same thing.
I wrote the first "edition" on a mobile so it contained a few aliases. Updating it now :-)
I will never have more than one file of each. They are specific to that file and only come in one at a time. However, this is a good rule of thumb and I will integrate it. Thank you!
0

Well I feel foolish:

$pwd = (Get-ChildItem D:\FILES\Claims\TEST -Filter "*.pwd.enc*").Name
$tar = (Get-ChildItem D:\FILES\Claims\TEST -Filter "*.tar.enc*").Name
$file = (Get-ChildItem D:\FILES\Claims\TEST -Filter "*.tar.enc*").BaseName
dec_utility.exe rsa_key_123456.pri D:\FILES\Claims\TEST\$pwd D:\FILES\Claims\TEST\$tar D:\FILES\Claims\TEST\$file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.