在 powershell 中循环具有 运行 后清除变量

Clearing variables after loop has run in powershell

我有一个现在 运行 很好的脚本,只有当我 运行 它第一次文件夹检查返回为空白因此转到回退文件夹时。然后,如果我再次 运行 脚本并选择不同的用户,它将输出原始选择。结果,我总是比期望的输出落后一个用户。

我已经尝试清除变量,但我看到变量在脚本 运行 之前被清除,因此导致它为空。

我已经从这里尝试了这些步骤: and http://community.idera.com/powershell/powertips/b/tips/posts/clearing-all-user-variables 这是顶部函数的来源。

这适用于 Windows 7 的用户,因此 Powershell 2.0 是极限。

脚本部分如下:

清除变量的函数:

# Store all the start up variables so you can clean up when the script finishes.
function Get-UserVariable ($Name = '*') {
    # these variables may exist in certain environments (like ISE, or after use of foreach)
    $special  = 'ps','psise','psunsupportedconsoleapplications', 'foreach', 'profile'
    $ps       = [PowerShell]::Create()
    $null     = $ps.AddScript('$null=$host;Get-Variable') 
    $reserved = $ps.Invoke() | 
    Select-Object -ExpandProperty Name
    $ps.Runspace.Close()
    $ps.Dispose()
    Get-Variable -Scope Global | Where-Object Name -like $Name | Where-Object { $reserved -notcontains $_.Name } | Where-Object { $special -notcontains $_.Name } | Where-Object Name 
}

创建用户输出的函数:

# create a select box for users
function mbSelectBox {
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $mbForm                     = New-Object System.Windows.Forms.Form 
    $mbLabel.Text               = "Select the user to output to:"

    [void] $mbListBox.Items.Add( "User01" )
    [void] $mbListBox.Items.Add( "User02" )

    $mbSelectBoxResult          = $mbForm.ShowDialog()

    if( $mbSelectBoxResult -eq [System.Windows.Forms.DialogResult]::OK) {
        $script:mbUser          = $mbListBox.SelectedItem
    }
}

调用转换的函数:

# get the folder for conversion
function mbAudioConvert {
    [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    [System.Windows.Forms.Application]::EnableVisualStyles()

    $mbFileBrowser                      = New-Object System.Windows.Forms.FolderBrowserDialog
    $mbFileBrowser.SelectedPath         = "C:\folderwithaudio"
    $mbFileBrowser.ShowNewFolderButton  = $false
    $mbFileBrowser.Description          = "Select the folder with the audio which you wish to convert:"

    $mbLoop     = $true

    while( $mbLoop ) {
        if( $mbFileBrowser.ShowDialog() -eq "OK" ) {
            $mbLoop     = $false
            $mbCount    = 1

            $mbFolder   = ( $mbFileBrowser.SelectedPath )

            $mbHasRaw   = ( $mbFolder + "\RAW" )
            $mbUserPath = ( "\NETWORK\SHARE\" + $mbUser + "\WATCHFOLDER" )

            # the output profile path
            if( !( Test-Path -Path "$mbUserPath" ) ) {
                if( !( Test-Path -Path "$mbHasRaw" ) ) {
                    New-Item -ItemType Directory -Force -Path "$mbHasRaw"
                    $mbOutPath  = $mbHasRaw
                }
            } else {
                $mbOutPath  = $mbUserPath
            }


            # get user to select user output
            mbSelectBox

            foreach( $mbItem in $mbItemInc ) {

                $mbCount++

                # clear the user variable
                if( $mbItemNo -eq $mbCount[-1] ) {
                    Get-UserVariable | Remove-Variable
                    Write-Output ( "cleared variables" )
                }

            }


        } 

}

# call to function
mbAudioConvert

您遇到了一些基本问题。例如在定义之前引用 $mbUser$mbUserPath = ( "\NETWORK\SHARE\" + $mbUser + "\WATCHFOLDER" ) 是在您调用 mbSelectBox 之前的 14 行。此外,请保持范围一致。如果您要定义 $script:mbUser那么你应该引用 $script:mbUser。更好的是,如果函数的目的是选择一个用户,让函数输出用户并将其捕获到一个变量中。

# create a select box for users
function mbSelectBox {
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $mbForm                     = New-Object System.Windows.Forms.Form 
    $mbLabel.Text               = "Select the user to output to:"

    [void] $mbListBox.Items.Add( "User01" )
    [void] $mbListBox.Items.Add( "User02" )

    $mbSelectBoxResult          = $mbForm.ShowDialog()

    if( $mbSelectBoxResult -eq [System.Windows.Forms.DialogResult]::OK) {
        $mbListBox.SelectedItem
    }
}

然后,如果未提供参数,您可以将参数添加到第二个函数,该函数会立即调用它。

# get the folder for conversion
function mbAudioConvert {
Param($mbUser = $(mbSelectBox))
    [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    [System.Windows.Forms.Application]::EnableVisualStyles()