首页 > 解决方案 > 全局变量未被函数拾取

问题描述

我有一个脚本可以对另一个文件进行重复数据删除(这部分脚本按预期工作,这要归功于以前的帮助),但是我似乎无法正确设置全局变量。

每个单独的功能似乎都按预期工作,但是当脚本到达“Dedupe”功能时,它会失败并且找不到已选择的文件。

为了解决这个问题,我尝试将进程嵌入到 if 语句中以创建 1 个函数而不是调用其他函数,但会出现同样的问题。

这是原始脚本:

$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

我认为它几乎就在那里,但我显然错过了一些东西,任何帮助将不胜感激。

编辑:这些是我得到的错误:

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

标签: powershellglobal-variables

解决方案


推荐阅读