I have a script that dedupes 1 file against another (this part of the script works as expected, thanks to previous help), however I cannot seem to set the global variables properly.
Each indidual function appears to work as expected, but when the script reaches the "Dedupe" function it fails and cannot find the files that have been selected.
To get around the issue, I have tried embedding the processes into the if statements to create 1 function instead of calling others, but the same problem occurs.
Here is the original script:
$Global:SelectedSendFile = $null
$Global:SelectedSuppressionFile = $null
$Global:SelectedSaveFile = $null
function Instruction ($Message = "Select your send and suppression / unsubscribe lists,`nthen enter a new name to save the deduped list.", $Title = " Instructions") {
Add-Type -AssemblyName System.Windows.Forms | Out-Null
$MsgBox = [System.Windows.Forms.MessageBox]
$Decision = $MsgBox::Show($Message,$Title,"OkCancel", " Information")
If ($Decision -eq "OK") {BrowseSend}
If ($Decision -eq "Cancel") {exit}
}
#End Instruction
function BrowseSend($initialDirectory)
{
Add-Type -AssemblyName System.Windows.Forms | Out-Null
$SendFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$SendFileDialog.initialDirectory = $initialDirectory
$SendFileDialog.Title = 'Please Select Send List'
$SendFileDialog.filter = 'CSV (*.csv)| *.csv'
$SelectedSendFile = $SendFileDialog.filename
if($SendFileDialog.ShowDialog() -eq "OK") {
Write-Host
""
" Selected Send List:"
$SendFileDialog.filename
BrowseSuppression
}
else {exit}
}
#End BrowseSend
function BrowseSuppression($initialDirectory)
{
Add-Type -AssemblyName System.Windows.Forms | Out-Null
$SuppressionFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$SuppressionFileDialog.initialDirectory = $initialDirectory
$SuppressionFileDialog.Title = 'Please Select Suppression / Unsubscribe List'
$SuppressionFileDialog.filter = 'CSV (*.csv)| *.csv'
$SelectedSuppressionFile = $SuppressionFileDialog.filename
if($SuppressionFileDialog.ShowDialog() -eq "OK") {
Write-Host " Selected Suppression List:"
$SuppressionFileDialog.filename
SaveFile
}
else {exit}
}
#End Suppression
function SaveFile($initialDirectory)
{
Add-Type -AssemblyName System.Windows.Forms | Out-Null
$SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$SaveFileDialog.Title = 'Save deduped list as'
$SaveFileDialog.initialDirectory = $initialDirectory
$SaveFileDialog.filter = "CSV (*.csv)| *.csv"
$SelectedSaveFile = $SaveFileDialog.filename
if($SaveFileDialog.ShowDialog() -eq "OK") {
Write-Host " Save File As:"
$SaveFileDialog.filename
Dedupe
}
else {exit}
}
#End SaveFile
function Dedupe
{
""
""
" Reading lists, please wait... (this may take several minutes depending on the size of the file)"
$fileA = Import-csv $SendFileDialog.filename
$fileB = Import-csv $SuppressionFileDialog.filename
$deduped = Compare-Object -Ref $fileA -Diff $fileB -Property email -PassThru |
Where-Object Sideindicator -eq '<=' |
Select-Object * -ExcludeProperty Sideindicator
$deduped
$deduped | Export-Csv $SaveFileDialog.filename -NoTypeInformation
$Cleanup = Get-ChildItem . $SaveFileDialog.filename -rec
foreach ($file in $Cleanup)
{
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace , """" } "" |
Set-Content $file.PSPath
}
checkfile
}
#End Dedupe
function checkfile{
if ( (get-childitem $SaveFileDialog.filename).length -eq 0 )
{Error}
else
{Complete}
}
#End checkfile
function Error ($Message = "Process has not been completed", $Title = " Error") {
Add-Type -AssemblyName System.Windows.Forms | Out-Null
$MsgBox = [System.Windows.Forms.MessageBox]
$Decision = $MsgBox::Show($Message,$Title,"RetryCancel", "Error")
If ($Decision -eq "Retry") {Instruction}
If ($Decision -eq "Cancel") {exit}
}
#End Error
function Complete ($Message = "Export of the deduped list has been completed.", $Title = " Complete") {
Add-Type -AssemblyName System.Windows.Forms | Out-Null
$MsgBox = [System.Windows.Forms.MessageBox]
$Decision = $MsgBox::Show($Message,$Title,"OK", " Information")
If ($Decision -eq "OK") {exit}
}
#End Complete
Instruction
I think it's almost there, but I'm obviously missing something, any help would be appreciated.
EDIT: These are the errors I am getting:
Import-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then
try the command again.
At \\Mbp-nt01\dma\Digitial Marketing\___Shared folder for Email Team\_Fun_Template_2018\_remove dup - TEST\TEST2.ps1:98 char:22
+ $fileB = Import-csv $SupressionFileDialog.filename
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Import-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ImportCsvCommand
Compare-Object : Cannot bind argument to parameter 'DifferenceObject' because it is null.
At \\Mbp-nt01\dma\Digitial Marketing\___Shared folder for Email Team\_Fun_Template_2018\_remove dup - TEST\TEST2.ps1:100 char:46
+ $deduped = Compare-Object -Ref $fileA -Diff $fileB -Property emai ...
+ ~~~~~~
+ CategoryInfo : InvalidData: (:) [Compare-Object], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At \\Mbp-nt01\dma\Digitial Marketing\___Shared folder for Email Team\_Fun_Template_2018\_remove dup - TEST\TEST2.ps1:105 char:13
+ $deduped | Export-Csv $SaveFileDialog.filename -NoTypeInformation
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
Get-ChildItem : Second path fragment must not be a drive or UNC name.
Parameter name: path2
At \\Mbp-nt01\dma\Digitial Marketing\___Shared folder for Email Team\_Fun_Template_2018\_remove dup - TEST\TEST2.ps1:107 char:13
+ $Cleanup = Get-ChildItem . $SaveFileDialog.filename -rec
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (\\Mbp-nt01\dma\...move dup - TEST:String) [Get-ChildItem], ArgumentException
+ FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand