requested file operation failed in PowerShell - powershell

I'm trying to read a txt file from my directory and getting the following error. This is the first time seeing it and I don't think it happened to me before. So, just wondering what's going on and how to fix this.
$FilePath = 'C:\Users\yzqpsn\Desktop\Audit-Log-Script\teams.txt'
$Data = Get-Content -Path $FilePath
Get-Content : The requested file operation failed because the storage policy blocks that type of file. For more information, contact your system
administrator.
At line:1 char:9
+ $Data = Get-Content -Path $FilePath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (C:\Users\yzqpsn...cript\teams.txt:String) [Get-Content], IOException
+ FullyQualifiedErrorId : GetContentReaderIOError,Microsoft.PowerShell.Commands.GetContentCommand

Related

Reading zip file in Powershell (openread)

I am trying to read zip file by openread method, but it gives error that could not find zip file.
Below is the code that I am using:
Code:
$Sourcefolder= "C:\Users\My.pc\Downloads"
$Myzipfile= gci $Sourcefolder -Filter *.zip | sort LastWriteTime | select -last 1
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::OpenRead($Myzipfile)
Error:
Exception calling "OpenRead" with "1" argument(s): "Could not find file 'C:\Users\My.pc\15367448612289827.zip'."
At line:4 char:1
+ $zip = [System.IO.Compression.ZipFile]::OpenRead($Myzipfile)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FileNotFoundException
What should I do? any help please.
Change $Myzipfile to $Myzipfile.FullName:
$zip = [System.IO.Compression.ZipFile]::OpenRead($Myzipfile.FullName)

Error while trying to change the time stamp of file share location files

I am trying to change all the file creation date stamp to new time. I am using below code to do that. I am running this from a VM with an admin ID which as access to all the "FILE share servers".
Am my missing nay permissions to make changes to the files?
Note: File share servers are stored in different servers and I am using my own VM which is in same domain to access those file shares.I can access them through Network path from "RUN". Not sure while access denied error is thrown.
Function Set-FileTimeStamps { Param (
[Parameter(mandatory=$true)]
[string[]]$path,
[datetime]$date = (Get-Date) )
Get-ChildItem -Path $path |
ForEach-Object {
$_.CreationTime = $date
$_.LastAccessTime = $date
$_.LastWriteTime = $date } } Set-FileTimeStamps -path \\nwst01\test$\rgadagot "07/10/19 10:10"
error:
Exception setting "CreationTime": "Access to the path
'\nwst01\test$\user \Data' is denied." Exception setting
At line:10 char:6
+ $_.CreationTime = $date
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception setting"LastAccessTime": "Access to the path '\nwst01\test$\user \Data' is
denied." At line:11 char:6
+ $_.LastAccessTime = $date
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception setting "LastWriteTime": "Access to the path
'\nwst01\test$\user \Data' is denied." At line:12 char:6
+ $_.LastWriteTime = $date }
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Welcome to stackoverflow.
Try to create a new file at that location (through the same script). If that fails, but you are able to create such a file otherwise, it may be a double-hop issue: Kerberos authentication prevents you from impersonating a 2nd time (even if you are logged in as a domain administrator). There is a workaround with CredSSP but maybe better look into the suggestion here: https://searchwindowsserver.techtarget.com/tutorial/How-to-avoid-the-double-hop-problem-with-PowerShell.

PowerShell Error: New-Object : Cannot find an overload for "PlatformParameters" and the argument count: "1"

I am trying to automatically upload a CSV into PowerBi using the Git repository PowerBIPS. I can't seem to get past this error:
New-Object : Cannot find an overload for "PlatformParameters" and the argument count: "1".
With the help of the article linked here (https://powerbi.microsoft.com/en-us/blog/using-a-power-bi-app-to-upload-csv-files-to-a-dataset/), I believe I have found a viable solution to solve this. However, I am running into an error I can't seem to figure out.
I have customized the code from the original article to fit my current situation (outlined in the code below). I am able to run the code up until the "send data to PowerBi comment" where this error is raised.
I have tried referencing the Git documentation to help with this issue, but have found no luck. I have a feeling it has to do with the authentication piece of connecting to PowerBi.
while ($true) {
# Iterate each CSV file and send to PowerBI
Get-ChildItem "$currentPath\CSVData" -Filter "*.csv" | % {
$file = $_
#Import csv and add column with filename
$data = Import-Csv $file.FullName |
select #{Label="File";Expression={$file.Name}}, *
# Send data to PowerBI
$data | Out-PowerBI -DataSetName "CSVSales" -TableName "Sales" -Types #{
"Sales.OrderDate" = "datetime";
"Sales.SalesAmount" = "double";
"Sales.Freight" = "double"
} -BatchSize 300 -Verbose
# Archive the file
Move-Item $file.FullName "$currentPath\CSVData\Archive\" -Force
}
Write-Output "Sleeping..."
Sleep -Seconds 5
}
VERBOSE: Getting the Authentication Token
VERBOSE: Using default authentication flow
New-Object : Cannot find an overload for "PlatformParameters" and the argument
count: "1".
At C:\Program Files\WindowsPowerShell\Modules\PowerBIPS\2.0.3.1\PowerBIPS.psm1:299 char:16
+ ... pltParams = New-Object Microsoft.IdentityModel.Clients.ActiveDirector ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

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

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.