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

时间:2018-03-06 23:11:49

标签: powershell variables powershell-v2.0

我现在有一个运行良好的脚本,只有当我第一次运行它时,文件夹检查返回为空白,从而进入后备文件夹。然后,如果我再次运行脚本并选择其他用户,它将输出原始选择。因此,我总是从所需的输出中退出一个用户。

我已经尝试清除变量,但我发现在运行脚本之前变量正在被清除,从而导致变量为空。

我已经从这里尝试了这些步骤:How to clear variable content in powershellhttp://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

1 个答案:

答案 0 :(得分:1)

这里有一些基本问题。比如在定义之前引用$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()