Removing spaces from specific columns of pipe seperated file - powershell

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

Related

powershell method on a null-valued expression

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

Why does export-csv not run properly in Powershell on SQL Server?

I am no expert with Powershell; however, I am trying to simply run an Invoke-Sqlcmd and save the results as a .csv file. I am looping through multiple *.sql files as the queries. When I run the following Powershell from a Windows10 desktop, everything runs fine but when I run this from our SQL Server, I am receiving errors on the export-csv command.
Here is my Powershell:
#Prompt for date
$DatePrompt = Read-Host -Prompt 'Enter the database date. (YYYY-MM-DD)'
#Convert to date
$DatabaseDate = [DateTime]::Parse($DatePrompt)
#Create FileDate string
$FileDate = $DatabaseDate.ToString("yyyyMMdd")
#Create SQLDate string
$SQLDate = $DatabaseDate.ToString("yyyy-MM-dd")
#--------------------------------------------------------------------------------------------------------------------------------
#Provide SQLServerName
$SQLServer ="SQLSERVER\MISDATA"
#Provide Database Name
$DatabaseName ="StagingDB"
#Set MonthStart and MonthEnd (YYYY-MM-DD)
$Variables = "FileDate = $SQLDate"
#Scripts Folder Path
$FolderPath ="\\SQLSERVER\MISDATA\Queries"
#Output Folder Path
$OutputFolderPath ="\\SQLSERVER\MISDATA\Results"
#--------------------------------------------------------------------------------------------------------------------------------
#Loop through the .sql files and run them
foreach ($filename in get-childitem -path $FolderPath -filter "*.sql")
{ #Run SQL Query
Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DatabaseName -InputFile $filename.fullname -Variable $Variables -Querytimeout 0|
#Export to CSV
Export-Csv -NoTypeInformation -Path ($OutputFolderPath + "\" + $FileDate + "-" + $filename.basename + ".csv") -Encoding UTF8
#Write to ConsoleHost
Write-Host $filename.name "completed"
}
Here is the error:
Export-Csv : Cannot open file because the current provider (Microsoft.SqlServer.Management.PSProvider\SqlServer) cannot open a file.
At C:\MISDATA\Queries\Run Scripts.ps1:26 char:5
+ Export-Csv -NoTypeInformation -Path ($OutputFolderPath + "\" + $FileDate + " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], PSInvalidOperationException
+ FullyQualifiedErrorId : ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.ExportCsvCommand
sqlquery1.sql completed
Export-Csv : Cannot open file because the current provider (Microsoft.SqlServer.Management.PSProvider\SqlServer) cannot open a file.
At C:\MISDATA\Queries\Run Scripts.ps1:26 char:5
+ Export-Csv -NoTypeInformation -Path ($OutputFolderPath + "\" + $FileDate + " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], PSInvalidOperationException
+ FullyQualifiedErrorId : ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.ExportCsvCommand
sqlquery2.sql completed
Export-Csv : Cannot open file because the current provider (Microsoft.SqlServer.Management.PSProvider\SqlServer) cannot open a file.
At C:\MISDATA\Queries\Run Scripts.ps1:26 char:5
+ Export-Csv -NoTypeInformation -Path ($OutputFolderPath + "\" + $FileDate + " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], PSInvalidOperationException
+ FullyQualifiedErrorId : ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.ExportCsvCommand
sqlquery3.sql completed
You need to specify powershell ruled UNC format.
$OutputFolderPath ="Filesystem::\\SQLSERVER\MISDATA\Results"
or
$OutputFolderPath ="Microsoft.PowerShell.Core\FileSystem::\\SQLSERVER\MISDATA\Results"

Find & Replace text from multiple specific files from sub folders dynamically using powershell

I am working on azure CD pipeline, i want to change content of multiple files which exist in following folder structure.
MainFolder => SubFolder1 => myFile1.txt
SubFolder2 => myFile2.txt
SubFolder3 => myFile3.txt
I want to achieve my above requirement using powershell, and i have tried the following code.
$filepath = 'C:\Users\ashishjain06\Desktop\MainFolder'
$mydata = Get-ChildItem $filepath -include *.txt -recurse | Select-Object fullName
$totalRecords = $mydata.Count
for($x = 0; $x -lt $totalRecords; $x++)
{
((Get-Content -path $mydata[$x] -Force) -replace 'oldText','newText') | Set-Content -Path $mydata[$x]
}
When i run above code it's give me following output.
Get-Content : Cannot find drive. A drive with the name '#{FullName=C' does not exist.
At line:6 char:7
+ ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{FullName=C:String) [Get-Content], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+ ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+ ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+ ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+ ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
I am newbie on powershell, Please help me out to resolve this issue.
Get-ChildIItem returns (an array of) FileInfoObjects with a bunch of properties,
Select-Object drops all but the selected one, but it is still an object with one property FullName.
See $mydata | Get-Member
One method is Select-Object -ExpandProperty FullName,
another one is the property dereference operator . used in the following script.
To have the script work on any user don't include a fixed path, either use $Env:USERPROFILE or better yet, let the system evaluate current users Desktop folder (which might be relocated).
Instead of iterating the arrray $mydata by index let powershell do that with a foreach:
$filepath = Join-Path ([environment]::GetFolderPath('Desktop')) 'MainFolder'
$files = (Get-ChildItem $filepath -Filter *.txt -Recurse).FullName
foreach($file in $files){
(Get-Content -path $file -Force) -replace 'oldText','newText' | Set-Content -Path $file
}
It's still an object with a property unless you do this:
select-object -expandproperty fullname
Replace Select-Object with ForEach-Object, i.e.:
$filepath = 'C:\Users\ashishjain06\Desktop\MainFolder'
#$mydata = Get-ChildItem $filepath -include *.txt -recurse | Select-Object fullName
$mydata = Get-ChildItem $filepath -include *.txt -recurse | ForEach-Object fullName
$totalRecords = $mydata.Count
for($x = 0; $x -lt $totalRecords; $x++)
{
((Get-Content -path $mydata[$x] -Force) -replace 'oldText','newText') | Set-Content -Path $mydata[$x]
}

Powershell script to search mutiple remote PC's for specific files

I am attempting to run a powershell script from my PC against a txt file that contains numerous remote PC's. I want to run a script against that list of PC's that will search a certain file path for specific file name and/or file extension.
I have been attempting to do a for each loop with my txt file inserted as a variable. I am also attempting to get the file names returned to me via the cmdlet "Get-ChildItem."
I have not been able to get anything to work correctly so far. Can anyone point me in the right direction with this? Below are a couple of things I have tried so far...
**
PS C:\Windows\system32> $name= gc env:computername
$computers= get-content -path c:\users\person\Desktop\book2.txt
$csvfile = "c:\temp\$name.csv"
foreach ($computer in $computers) {Get-ChildItem -recurse -filter "C:\Users\*.locky"}
export-csv -filepath $csvfile
**
*
Get-ChildItem : Second path fragment must not be a drive or UNC name.
Parameter name: path2
At line:4 char:36
+ foreach ($computer in $computers) {Get-ChildItem -recurse -filter "C:\Users\*.lo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Windows\system32:String) [Get-ChildItem], Argume
ntException
+ FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand
Export-Csv : A parameter cannot be found that matches parameter name 'filepath'.
At line:5 char:12
+ export-csv -filepath $csvfile
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand
*
AND
C:\Windows\system32> ForEach ($system in (Get-Content C:\temp\Book2.txt))
if ($exists in (Test-Path \\$system\c$\Users\*.locky))
{
Get-Command $exists | fl Path,FileVersion | Out-File c:\temp\results.csv -Append
}
At line:1 char:53
+ ForEach ($system in (Get-Content C:\temp\Book2.txt))
+ ~
Missing statement body in foreach loop.
At line:3 char:14
+ if ($exists in (Test-Path \\$system\c$\Users\*.locky))
+ ~~
Unexpected token 'in' in expression or statement.
At line:3 char:14
+ if ($exists in (Test-Path \\$system\c$\Users\*.locky))
+ ~~
Missing closing ')' after expression in 'if' statement.
At line:3 char:55
+ if ($exists in (Test-Path \\$system\c$\Users\*.locky))
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingForeachStatement
Wrong -filter parameter in Get-ChildItem cmdlet. Next code snippet should work:
foreach ($computer in $computers) {"\\$computer\C$\Users"|`
Get-ChildItem -recurse -filter "*.locky"}

PowerShell v4 Get-Content : A parameter cannot be found that matches parameter name 'Delimiter'

I am hoping someone can give me the push I need to get this done, but I have had no luck with this exact situation online.
I need to use PowerShell (unfortunately I can't use Python or .NET to so this:( ) to parse though a list of files to determine if they contain a line termination of /r rather than /r/n. This script was previously in production and working, when single files were passed.
I am making adjustments so that multiple files can be accommodated.
I am getting the list of filenames and putting them into an array (which is working) but when I tey to loop the files through the if statement I get this error.
Here is my code:
param(
#[Parameter(Mandatory=$True)]
[String]$FileName = "C:\LineTermTest\*C*.txt"
)
$FileNameArray = Get-ChildItem -Path $FileName | where {!$_.psicontainter }| Select-Object fullname
for ($i=0; $i -le $FileNameArray.Length -1; $i++ )
{
$File = $FileNameArray[$i]
if (Get-Content -path $File -Delimiter "`0" | Select-String "[^`r]`n" )
{
$content = Get-Content $File
$content | Set-Content $File -Replace "`n", "'r'n" -Encoding ASCII
[gc]::collect()
[gc]::WaitForPendingFinalizers()
}
}
and here is the error I get
Get-Content : A parameter cannot be found that matches parameter name 'Delimiter'.
At line:13 char:39
+ if (Get-Content -path $File -Delimiter <<<< "0" | Select-String "[^r]`n" )
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : A parameter cannot be found that matches parameter name 'Delimiter'.
At line:13 char:39
+ if (Get-Content -path $File -Delimiter <<<< "0" | Select-String "[^r]`n" )
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : A parameter cannot be found that matches parameter name 'Delimiter'.
At line:13 char:39
+ if (Get-Content -path $File -Delimiter <<<< "0" | Select-String "[^r]`n" )
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand
in your script, $File is a 'PSCustomObject', not a FileInfo object, so you need to use its property 'fullname' like this: Get-Content -path $File.fullname -Delimiter "`0". This is because the objects in $FileNameArray are created by using Select-Object.
You can avoid this by leaving out "| Select-Object fullname" when creating $FileNameArray. That way, the $FileNameArray contains FileInfo objects, and Get-Content can use those directly. This is the preferred way.
So this line:
$FileNameArray = Get-ChildItem -Path $FileName | where {!$_.psicontainter }| Select-Object fullname
becomes this line:
$FileNameArray = Get-ChildItem -Path $FileName | where {!$_.psicontainter }