全局变量未由函数选择

时间:2019-03-06 13:29:16

标签: powershell global-variables

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

每个独立功能似乎都能按预期工作,但是当脚本到达“ 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

0 个答案:

没有答案
相关问题