1

I've got a script that runs well now, only when I run it the first time the folder check comes back as blank thus going to the fall back folder. Then if I run the script again and choose a different user, it will output the original selection. As a result, I'm always one user behind from the desired output.

I have tried clearing the variables, but I'm seeing that the variables are being clears before the script is run, thus causing it to be null.

I have tried these steps from here: How to clear variable content in powershell and http://community.idera.com/powershell/powertips/b/tips/posts/clearing-all-user-variables which is where the function at the top is from.

This is for users on Windows 7, so Powershell 2.0 is the limit.

Here are the script parts:

Function to clear the variables :

# 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 
}

Function to create the user output:

# 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
    }
}

Function to call the conversion:

# 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
0

1 Answer 1

1

You've got some fundamental issues here. Such as referencing $mbUser before it is defined ($mbUserPath = ( "\\NETWORK\SHARE\" + $mbUser + "\WATCHFOLDER" ) is 14 lines before your call to mbSelectBox. Also, keep your scope consistent. If you're going to define $script:mbUser then you should reference $script:mbUser. Better yet, if the purpose of the function is to pick a user, have the function output the user and capture that in a variable.

# 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
    }
}

Then you can just add a parameter to the second function that calls that right up front if the parameter isn't provided.

# get the folder for conversion
function mbAudioConvert {
Param($mbUser = $(mbSelectBox))
    [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    [System.Windows.Forms.Application]::EnableVisualStyles()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.