powershell - 全局变量未被函数拾取
问题描述
我有一个脚本可以对另一个文件进行重复数据删除(这部分脚本按预期工作,这要归功于以前的帮助),但是我似乎无法正确设置全局变量。
每个单独的功能似乎都按预期工作,但是当脚本到达“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
解决方案
推荐阅读
- c - 如何访问锁定在双指针后面的对象?(C)
- javascript - .map 不显示列表(来自 API)
- java - 复制另一个 ArrayList 的 ArrayList 时出现强制转换异常
- javascript - 无法添加 muteHttpExceptions 而不会出现语法错误
- angular - NullInjectorError:没有 FormGroup 的提供者!”
- android - Xamarin Forms 中的热重载,genymotion 不起作用
- ios - 返回指定值出现在集合中的第一个索引
- c - 从 DS4 控制器为学校项目读取数据 - 不断收到 hid_open 错误
- python - Imgur API - 如何在不分页的情况下检索所有收藏夹?
- css - 带有 Active 和 Hover 的造型垫选项 - Angular Materials