Error while fetching the files from sftp server using powershell - powershell

I'm trying to get *.xlsx files from the sftp server folder "/From/Outbound/TEST" using putty. I know it's something to do with the source path. I tried the below but nothing is working. Please please help.
"$Source/*.xlsx"
'$Source/*.xlsx'
$Source = "sftp#33.113.250.45:/From/Outbound/TEST"
[string]$TransferResults = & 'C:\Program Files\PuTTY\pscp.exe' -l 'user' -pw 'password' $Source/*.xlsx $Destination
I'm getting the below error:
pscp.exe : pscp: wildcard '*.xlsx' matched no files
At Test.ps1:29 char:31
+ [string]$TransferResults = & 'C:\Program Files\PuTTY\pscp.exe' -l 'user' - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (pscp: wildcard '*.xlsx' matched no files:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

Related

Cannot access network shared drive in unattended mode using powershell

I am new to powershell so would need need to resolve this issue.
We are trying to execute a powershell script via Azure devops pipelines and the script is made to access a network shared drive to identify a particular file.
The script runs perfectly fine in the gui session and is able to get the file.
However when we run it from AZ pipelines, it fails stating the access is denied.
The user being used in AZ Pipelines has admin access over the file.
The N/w drive path looks something like this.
\\ServerName\builds\PACKAGES\MainMasterBuildDrop\MainScheduledBuild\axmodel
Code ::
$locr = "\\ServerName\builds\PACKAGES\MainMasterBuildDrop\MainScheduledBuild\axmodel"
$axfiler = Get-ChildItem -Path $locr | Select FullName | ft -hidetableheaders | Out-String
$filever = $($($axfiler.split("\")[-1]).Split("_")[-1]).Replace(".axmodel", "")
Error ::
2022-01-14T07:19:22.6339088Z o -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\User
2022-01-14T07:19:22.6339869Z s\SRV_Deployment\AppData\Local\Temp\61e3d867-9d07-41cc-91c1-c4a04717bc2d
2022-01-14T07:19:22.6340311Z .ps1'"
2022-01-14T07:19:22.6340436Z
2022-01-14T07:19:22.7446332Z ls : Access is denied
2022-01-14T07:19:22.7446823Z At line:1 char:1
2022-01-14T07:19:22.7447184Z + & 'C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe' -NoLogo ...
2022-01-14T07:19:22.7447867Z + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-01-14T07:19:22.7448434Z + CategoryInfo : NotSpecified: (ls : Access is denied:String) [],
2022-01-14T07:19:22.7448858Z RemoteException
2022-01-14T07:19:22.7449188Z + FullyQualifiedErrorId : NativeCommandError
2022-01-14T07:19:22.7449545Z
2022-01-14T07:19:22.7449643Z
2022-01-14T07:19:22.7618724Z At E:\DeploymentTeam\Scripts\DeploymentValidation\Test.ps1:1 char:1
2022-01-14T07:19:22.7619629Z + ls "\\Server\builds\PACKAGES\MainMasterBuildDrop\ ...
2022-01-14T07:19:22.7620370Z + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-01-14T07:19:22.7620732Z
2022-01-14T07:19:22.7620874Z
2022-01-14T07:19:22.7731192Z + CategoryInfo : PermissionDenied: (\\ServerName\...edBuild\axmo
2022-01-14T07:19:22.7731830Z del:String) [Get-ChildItem], Unauthoriz
2022-01-14T07:19:22.7732125Z
2022-01-14T07:19:22.7732299Z
2022-01-14T07:19:22.7845365Z edAccessException
2022-01-14T07:19:22.7845602Z
2022-01-14T07:19:22.7846744Z
2022-01-14T07:19:22.7966282Z + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.Power
2022-01-14T07:19:22.7966826Z Shell.Commands.GetChildItemCommand
2022-01-14T07:19:22.7966978Z
2022-01-14T07:19:22.7967080Z

Running a Powershell command with a variable -Path parameter

Can someone please explain the following behaviour to me?
PS C:\Users\Kenny> $filePath = "C:\\Complicated.File.Path.That.Has.Special-Chars`[but-no.spaces`]and.definitely.exists\"
PS C:\Users\Kenny> cd $filePath
cd : Cannot find path 'C:\\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd $filePath
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
xception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
PS C:\Users\Kenny> cd 'C:\\Complicated.File.Path.That.Has.Special-Chars`[but-no.spaces`]and.definitely.exists\'
PS C:\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists> cd c:
PS C:\Users\Kenny> Write-Host $filePath
C:\\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\
PS C:\Users\Kenny> cd "$filePath"
cd : Cannot find path 'C:\\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd "$filePath"
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
xception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
PS C:\Users\Kenny> cd ${filePath}
cd : Cannot find path 'C:\\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd ${originalPath}
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
xception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
PS C:\Users\Kenny> cd ${$filePath}
cd : Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a
non-null value.
At line:1 char:1
+ cd ${$filePath}
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Location], PSArgumentNullException
+ FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SetLocationCommand
PS C:\Users\Kenny> cd $($filePath)
cd : Cannot find path 'C:\\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd $($filePath)
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
xception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
If I had a penny for every minute I've wasted on PowerShell's insistance that backticks, quotes and other meta-characters are just right, I would be the richest man on Earth! Somebody please save me from this madness!
And BTW, what I'm actually trying to do is recurse through a bunch of folders and delete everything that isn't a video file, like so...
Remove-Item -Path $filePath -Recurse -Exclude '*.avi','*.mkv','*.mp4'
...which doesn't work for (presumably) the same reason: not being able to pass a variable to the -Path parameter. And if someone is feeling really generous, they could also help me with this. MTIA! :D
Use -LiteralPath instead of -Path because some of the special characters where interpreted with -Path.
Remove-Item -LiteralPath
Special characters are just so much fun in PS right?
-LiteralPath works exactly like Patrick explained, but they don't allow for wildcards; because they're literal.
Have you tried using single quotes ' instead of double quotes ". This allows you to escape special characters, while still evaluating wildcards. Try the commands below:
New-Item -Path 'C:\Users\username\PSScripts\bracket`[\te$t.txt'
Get-Item -Path 'C:\Users\username\PSScripts\bracket`[\*'
Also, if it helps, I use VSCode for most scripting and, if you use tab completion, it will format this properly for you.
I hope that helps!
The solution to my overall problem ended up being this line of my batch script:
powershell -Command "Get-ChildItem -Recurse -LiteralPath %filePath% | Where-Object Extension -match '^(?!(\.avi|\.mkv|\.mp4)$)' | Remove-Item"
The regex was the hardest to get right, since I haven't used negative lookaheads before, but after ~2 hours of total time spent on this one line of code, it finally works!

Using powershell with 7zip - password protect each file in a folder

I am trying to password protect 10 files (using the same password) in a folder. Once completed, i would like to have 10 individual zip files each with the same password. My script is as follows but it errors out saying it cant open the file as archive.
$list=gci "C:\temp\test"
foreach ($file in $list)
{
$file1=$file|select -ExpandProperty name
& 'c:\program files\7-zip\7z.exe' a $file1.zip $file1 -p123
}
This is the error i get:
Open archive: testfile.pdf
7z.exe : ERROR: testfile.pdf
At line:6 char:5
+ & 'c:\program files\7-zip\7z.exe' a $file1.zip $file1 -p123
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: testfile.pdf:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Can not open the file as archive
System ERROR:
Incorrect function.
Any help would be greatly appreciated. Thanks
Oh boy...figured it out myself..needed quotation marks!!!
& 'c:\program files\7-zip\7z.exe' a "$file1.zip" $file1 -p123

path not found?

I am trying to run the below against multilpe servers using Powershell 3.0 but for some reason I am getting back that the path does not exist...even though it does?
Any ideas?
CODE:
clear
$computer = Get-Content -path c:\temp\servers.txt
foreach ($computer1 in $computer){
Write-Host $computer1
Get-Content -Path '\\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rscd.log' -Tail 10
}
ERROR:
SV191267 Get-Content : Cannot find path '\$computer1\C$\Program
Files\BMC Software\BladeLogic\RSCD\rscd.log' because it does not
exist. At
C:\Users\gaachm5\AppData\Local\Temp\e4651274-dcab-4a87-95a6-0f11437a7187.ps1:7
char:1
+ Get-Content -Path '\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rs ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\$computer1\C$...c\RSCD\rscd.log:String) [Get-Content],
ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
SV193936 Get-Content : Cannot find path '\$computer1\C$\Program
Files\BMC Software\BladeLogic\RSCD\rscd.log' because it does not
exist. At
C:\Users\gaachm5\AppData\Local\Temp\e4651274-dcab-4a87-95a6-0f11437a7187.ps1:7
char:1
+ Get-Content -Path '\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rs ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\$computer1\C$...c\RSCD\rscd.log:String) [Get-Content],
ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
try:
"\\$computer1\C`$\Program Files\BMC Software\BladeLogic\RSCD\rscd.log" -Tail 10
In single quotes ' the variable are not expanded but treated as literal as $computer1, the $ for the $admin share must be escaped with a backtick `
The path that can't be found only has one \ on the front of it, a UNC share must begin with two \'s. I can see from your code it looks like you are prefixing the two \'s but it doesn't look like it's being carried over.
I would use double quotes instead of single quotes as already pointed out, if you do that then you should not need to escape the $ character either.
Assuming a file called file.txt exists in the C:\temp folder on the local machine.
This fails:
$computer1 = "localhost"
Test-Path '\\$computer1\c$\temp\file.txt'
This works:
$computer1 = "localhost"
Test-Path "\\$computer1\c$\Temp\file.txt"

PowerShell copy to $null

In Windows shell you can fetch the contents of a file and copy it to "\Device\Null" with a command like copy c:\filename NUL. (This is useful for recalling externally-archived files without wasting space or updating with touch.)
But I can't figure out how to do the same in PowerShell using $null, NUL, \\.\NUL and more (and I don't want to call out a separate CMD.EXE process to do this for every file).
PS C:\> Copy-Item -Path .\filename -Destination NUL
Copy-Item : Cannot process path 'C:\NUL' because the the target represents a reserved device name.
At line:1 char:10
+ Copy-Item <<<< -Path .\filename -Destination NUL
+ CategoryInfo : WriteError: (C:\NUL:String) [Copy-Item], IOException
+ FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand
PS C:\> Copy-Item .\filename NUL
Copy-Item : Cannot process path 'C:\NUL' because the the target represents a reserved device name.
At line:1 char:10
+ Copy-Item <<<< .\filename NUL
+ CategoryInfo : WriteError: (C:\NUL:String) [Copy-Item], IOException
+ FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand
PS C:\> copy .\filename '\\.\NUL'
Copy-Item : Cannot process path '\\.\NUL' because the the target represents a reserved device name.
At line:1 char:5
+ copy <<<< .\filename '\\.\NUL'
+ CategoryInfo : WriteError: (\\.\NUL:String) [Copy-Item], IOException
+ FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand
Any other ideas how to do this?
Effectively, you just want to do a read on the file. If so, this will work:
Get-ChildItem .\filename | Get-Content | Out-Null
It's probably overkill though. You could try:
$File=[system.io.file]::OpenRead(".\filename")
$File.Close()
This just opens the file for reading (which may be enough to bring it back) and closes it again.