Powershell accessing to the desktop

Dear expert

Powershell: $env: USERPROFILE\desktop\folder\
now when I run it, it tells me a error that permission denied... ok weird... I don't use right to access my desktop? Now when I try and full path, it works well...

What is the problem?
Regards
WeTi
LVL 1
WeTiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
When you run what exactly?
You need to be more specific about what it is you're trying to do and the exact error message you're getting.
1
WeTiAuthor Commented:
Hi oBdA

Its a decryption script by calling openssl. the error I get is:

openssl.exe : Can't open C:\Users\weti\desktop\secret for reading, Permission denied

Function Decry {
$Input = "$env:USERPROFILE\desktop\secret"
$Output = "$env:USERPROFILE\desktop\secret"
$UniqueKey = '(stdin)= aef44983e90b10***4af1742a15***8942'
$KeyFile = "$env:USERPROFILE\desktop\secret"
$RandomkeyFileName = Get-Random
$KeyFileExtractPath = "$Env:APPDATA\Microsoft"
openssl.exe enc -aes-256-cbc -salt -d -in $KeyFile -out $KeyFileExtractPath\"$RandomkeyFileName.key" -k $UniqueKey
$ReadKey = Get-Content -Path $KeyFileExtractPath\"$RandomkeyFileName.key"
$AllFiles = Get-ChildItem -Path $Input | ?{$_.Extension -eq $null -or $_.Extension -eq ""}
ForEach ($EachFile in $AllFiles){
$fullPath = $EachFile.FullName
$FileName = $EachFile.Name
    ForEach ($FileNameS in $ReadKey){
        $FileH,$FileN = $FileNameS.split(",",2)
       Write-Host "$FileH" -ForegroundColor Yellow
       Write-Host "$FileName" -ForegroundColor Green
            if($FileH -eq $FileName){
                Write-Host "Decrypted File : $FileN" -ForegroundColor Black -BackgroundColor White
                openssl.exe enc -aes-256-cbc -salt -d -in $fullPath -out $Output\$FileN -k $UniqueKey
            }
    }
}
Remove-Item $KeyFileExtractPath\"$RandomkeyFileName.key" -Force
}
Decry

Open in new window

0
WeTiAuthor Commented:
Thing is if I directly type:

$Input = 'C:\Users\weti\desktop\secret' This works... now because it will be a network shared script so env: must work tho...
0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

WeTiAuthor Commented:
now I lied... direct input didn't work as well, same problem
0
oBdACommented:
Replace $Input with $SourceFolder or whatever. $Input is a default variable.

$input gotchas
https://dmitrysotnikov.wordpress.com/2008/11/26/input-gotchas/
0
WeTiAuthor Commented:
Did, didn't help, same problem. Now I really dont know what the hell is going on... It worked before with direct path...
0
Shaun VermaakTechnical SpecialistCommented:
Use
"$($env:USERPROFILE)\desktop\folder\"

Open in new window


NVM
now I lied... direct input didn't work as well, same problem
0
oBdACommented:
Just to exclude the way-too-obvious:
You remembered to change the $Input variable in line 10 as well?
And made sure that the variable you used is identical in lines 2 and 10 (stranger things than typos have happened)?

Open a new PS console, paste the following:
$SourceFolder = "$env:USERPROFILE\desktop\secret"
Get-ChildItem -Path $SourceFolder -File | Where-Object {-not $_.Extension}

Open in new window

This works just fine for me.
You can't set the $Input variable, so PS will just get the children of the folder you're currently in, which is probably your profile folder. In your profile folder, it will find the "secret" folder (which has no extension), and try to open it as a file (which will fail for the obvious reason that it's not a file).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
After a restart, things started working well again. With my old option: $env:USERPROFILE
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.