powershell method on a null-valued expression - powershell

I have a script thats been running fine for months but over the last few days when I run it I'm getting this error - any help in debugging it?
here's line 66 in the code:
$remotefilehash = ($remoteFiles | Where-Object { -not ($_ | Select-String -Quiet -NotMatch -Pattern '^[a-f0-9]{32}( )') } -replace '^[a-f0-9]{32}( )', '$0= ' -join "`n") | ConvertFrom-StringData
I wonder if for some reason $remoteFiles is zero ? ie null and hence throws this error?
Where-Object : A parameter cannot be found that matches parameter name 'replace'.
At P:\scripts\code\pcloud_sync.ps1:66 char:125
+ ... ing -Quiet -NotMatch -Pattern '^[a-f0-9]{32}( )') } -replace '^[a-f0 ...
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand
You cannot call a method on a null-valued expression.
At P:\scripts\code\pcloud_sync.ps1:74 char:1
+ $diffmd5 = $remotefilehash.GetEnumerator().Where({ -not $localHashSet ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
UPDATE2
after adding the parthensis now getting another new error on the same line:
ConvertFrom-StringData : Data item 'a3512c98c9e159c021ebbb76b238707e' in line 'a3512c98c9e159c021ebbb76b238707e = My
Pictures/Tony/Automatic Upload/Tony’s iPhone/2022-10-08 21-46-21 (2).mov' is already defined.
At P:\scripts\code\pcloud_sync.ps1:66 char:179
+ ... ace '^[a-f0-9]{32}( )', '$0= ' -join "`n") | ConvertFrom-StringData
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [ConvertFrom-StringData], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand
You cannot call a method on a null-valued expression.
At P:\scripts\code\pcloud_sync.ps1:74 char:1
+ $diffmd5 = $remotefilehash.GetEnumerator().Where({ -not $localHashSet ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Related

import list to copy select files from large folder to a new folder in powershell

I am trying to move 34k images from a large dir that has 500k images. I only need the ones on the csv I created. the code I wrote call the item but doesn't move them. I wrote the code below to test on my desktop before touching production images (10 images but only want 5 to move). I am new to scripting, appologies for any mistakes I made. Thank you for any help that can be provided. I have learned a lot on this site.
Get-Content -path "C:\Users\waemisegger\Desktop\test1.csv" | ForEach-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports - Copy\images" -Recurse $_ -Destination "C:\Users\waemisegger\Desktop\test" }
this is the error I get:
PS C:\Windows\system32> Get-Content -path "C:\Users\waemisegger\Desktop\test1.csv" | ForEach-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports - Copy\images" -Recurse $_ -Destination "C:\Users\waemisegger\Desktop\test" }
Copy-Item : A positional parameter cannot be found that accepts argument 'FullName '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument '-------- '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-087.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-097.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-106lf.png'.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-118.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-159.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
here is what the csv looks like:
FullName
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-087.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-097.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-106lf.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-118.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-159.png
Edited to show before & after folder contents.
Assuming your CSV file looks like this:
Source
------
apple-ABC.doc
apple-ABC.pdf
Then this short script will do the work.
$MoveList = Import-Csv -path "G:\Test\FilesToMove.csv"
For ($Cntr = 0; $Cntr -lt $MoveList.Count; $Cntr++) {
$MIArgs =
#{Path = "G:\Test\$($MoveList[$($Cntr)].Source)"
Destination = "G:\Test\Music"}
Move-Item #MIArgs
}
Note if your CSV has the paths you just need to delete the test path [G:\Test] from the Path argument. Of course you'll provide appropriate paths for all items in my example.
Folders before running code:
Folders after running code:
HTH

A positional parameter cannot be found that accepts argument 'permit_connections=no'

I'm trying to write a script on a Windows 2008 R2 server to change the contents of an HTM file (Statuspage.htm) from "permit_connections=yes" to "permit_connections=no". I get the following error:
Get-Process : A positional parameter cannot be found that accepts argument
'permit_connections=no '.
At line:1 char:1
+ PS C:\> ((Get-Content -path D:\inetpub\wwwroot\statuspage.htm -Raw) - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
Get-Process : A positional parameter cannot be found that accepts argument
'Get-Content'.
At line:2 char:1
+ PS C:\> Get-Content -Path (D:\inetpub\wwwroot\statuspage.htm)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
Following is my script:
PS C:\> ((Get-Content -Path D:\inetpub\wwwroot\statuspage.htm -Raw) -replace 'permit_connections=yes','permit_connections=no') | Set-Content -Path D:\inetpub\wwwroot\statuspage.htm
PS C:\> Get-Content -path D:\inetpub\wwwroot\statuspage.htm

Removing spaces from specific columns of pipe seperated file

I'm trying to remove spaces from columns 15 and 16 in a pipe seperated file.
I've tried the code at the bottom of this post after searching the web but it's failing with
You cannot call a method on a null-valued expression. At line:19
char:2
+ $.h15=$.h15.ToString().Replace(' ','')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
and
You cannot call a method on a null-valued expression. At line:20
char:5
+ $.h16=$.h16.ToString().Replace(' ','')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNul
I guess from that, that it's not reading in the data but I can't see why not. I've checked the variables and they are correct.
$header = 1..42 | ForEach-Object { "h$_" }
$dialler_out_path="$dialler_file_dir\$output_file"
$dialler_path="$dialler_file_dir\$dialler_file"
(Import-Csv -Delimiter '|' -Header $header -Path $dialler_path | %{
$_.h15=$_.h15.ToString().Replace(' ','')
$_.h16=$_.h16.ToString().Replace(' ','')
}) | export-csv $dialler_out_path -NoType
Another way to deal with null object (in addition to comments):
$_.h15 = (-join ($_.h15)).ToString().Replace(' ','')
You should produce output in foreach:
(Import-Csv -Delimiter '|' -Header $header -Path $dialler_path | %{
$_.h15 = (-join ($_.h15)).ToString().Replace(' ','')
$_.h16 = (-join ($_.h16)).ToString().Replace(' ','')
$_
}) | export-csv $dialler_out_path -NoType

How to check for big files?

I tried to do like this but not works, any ideas?
if(Test-Path ~\desktop){
$bigfiles = Get-ChildItem ~\desktop -force -Include *.* | Where-Object {$_.Length -gt 10GB}
if{$bigfiles -gt 10GB){
echo "You have big files"
}
}
this is the error:
Cannot compare "C:\Users\mario\Desktop\AngelinaJolie_photo_in_4k.jpg" because it is not IComparable.
At line:7 char:8
+ if($bigfiles -gt 10GB){
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NotIcomparable
$bigfiles is $null if there are no results, a FileInfo if there is one result, otherwise it is an array of FileInfos. You can check for results using
if($bigfiles) { ... }

How to make powershell be less verbose on errors?

For instance, consider the following console transcript:
PS C:\dev\windows> rmdir -Recurse .\bin
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Implementation\Common.DTO.XML: Access to the path 'Common.DTO.XML' is denied.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : PermissionDenied: (Common.DTO.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug\Implementation cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (Implementation:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.Contract.XML: Access to the path 'Shunra.Common.Contract.XML' is denied.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : PermissionDenied: (Shunra.Common.Contract.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.XML: Access to the path 'Shunra.Common.XML' is denied.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : PermissionDenied: (Shunra.Common.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (Debug:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (DotNet:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (C:\dev\windows\bin:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
PS C:\dev\windows>
Now compare it with the ordinary shell (cmd.exe):
C:\dev\windows>rmdir /s/q bin
bin\DotNet\Debug\IMPLEM~1\Common.DTO.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.Contract.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.XML - Access is denied.
C:\dev\windows>
The difference is obvious and I like the laconicity of cmd.exe much much more than the verbosity of the powershell.
Can I have the same laconicity in powershell? If not for all the commands then maybe just for the Remove-Item, which I use often?
The closest result you can get is by changing the global $ErrorView value to "CategoryView". Another way is to create your own view.