Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell:变量在userform中丢失其值_Powershell - Fatal编程技术网

PowerShell:变量在userform中丢失其值

PowerShell:变量在userform中丢失其值,powershell,Powershell,我有一个带有userform的脚本,您可以使用它编辑广告用户对象 该脚本直接在我们的服务器上运行良好,但当我尝试使用管理员权限从系统中运行它时,New PSSession和一些Invoke Command的必要变量正在丢失它们的值 这是我的代码(有些东西已经被审查过了): 我的userform中有两个选项。用户输入数据后,必须通过单击“验证”来验证数据。如果一切正常,按钮“确认”被激活,广告用户对象可能被更改 然而,在验证数据并单击“确认”之后,所有必要的变量都丢失了它们的值,我不知道为什么 正

我有一个带有userform的脚本,您可以使用它编辑广告用户对象

该脚本直接在我们的服务器上运行良好,但当我尝试使用管理员权限从系统中运行它时,
New PSSession
和一些
Invoke Command
的必要变量正在丢失它们的值

这是我的代码(有些东西已经被审查过了):

我的userform中有两个选项。用户输入数据后,必须通过单击“验证”来验证数据。如果一切正常,按钮“确认”被激活,广告用户对象可能被更改

然而,在验证数据并单击“确认”之后,所有必要的变量都丢失了它们的值,我不知道为什么

正如您在下面看到的,所有设置都是在验证数据之后设置的,但是当我尝试确认它时,所有值都消失了

当脚本直接在我们的广告服务器上运行时,它与现在一样工作正常。我所做的只是通过添加一个远程会话和
Invoke命令来尝试通过我的系统运行它。这真的是个问题吗


感谢您的帮助。

尝试将变量添加到父范围。您可以使用Set变量并将作用域设置为1来执行此操作。非常感谢。现在可以了。您知道为什么脚本直接在服务器上运行时,这是必需的,而不是必需的吗?请尝试将变量添加到父作用域。您可以使用Set变量并将作用域设置为1来执行此操作。非常感谢。现在可以了。您知道为什么脚本直接在服务器上运行时,这是必需的,而不是必需的吗?
#--------------------#
# Function GUI > GUI #
#--------------------#
Function GUI
{
    #---Param
    [String]$BadgeListCSV = "link" # -> Badge list

    [Bool]$UserChk        = $False                                                             # -> Indicates if the user has been found

    [Bool]$BadgeChk       = $False                                                             # -> Indicates if the badge is already linked with an ad object

    [Int]$BadgeIndex      = 1                                                                  # -> Badge Nr.

    #Global variables
    Set-Variable -Name Exit -Value $True -Scope Global

    #---Importing badge list
    Try
    {
        $BadgeList = Import-CSV $BadgeListCSV
    }

    Catch
    {
        DisplayMSGBox -MSGText "Could not access CSV file." -AddErrorInfo $True | Out-Null

        LogFileCleanUp

        [System.Environment]::Exit(0)
    }

    ShowWallBoard

    #---Form
    $Form                 = New-Object System.Windows.Forms.Form
    $Form.Size            = New-Object System.Drawing.Size(184,197)
    $Form.Font            = New-Object System.Drawing.Font("Segoe UI",9,0,3,1)
    $Form.FormBorderStyle = "FixedSingle"    
    $Form.MaximizeBox     = $False
    $Form.Text            = "S/R Visitor Badge"
    $Form.StartPosition   = "CenterScreen"

    #---GroupBox Badges
    $GBBadges          = New-Object System.Windows.Forms.GroupBox
    $GBBadges.Location = New-Object System.Drawing.Size(10,5)
    $GBBadges.Size     = New-Object System.Drawing.Size(158,70)
    $GBBadges.Text     = "Badge"

    #Badge choice
    $CBBadgeChoice               = New-Object System.Windows.Forms.ComboBox
    $CBBadgeChoice.Location      = New-Object System.Drawing.Size(10,20)
    $CBBadgeChoice.Size          = New-Object System.Drawing.Size(110)
    $CBBadgeChoice.FlatStyle     = "PopUp"
    $CBBadgeChoice.DropDownStyle = 2

    #---Retrieving badge list
    Try
    {
        ForEach($Badge in $BadgeList)
        {
            $CBBadgeChoice.Items.Add("Visitor Badge $BadgeIndex") | Out-Null

            $BadgeIndex++
        }
    }

    Catch
    {
        DisplayMSGBox -MSGText "Could generate badge choice combobox." -AddErrorInfo $True | Out-Null

        LogFileCleanUp

        [System.Environment]::Exit(0)
    }

    $CBBadgeChoice.SelectedIndex = 0

    #Unlink
    $ChBUnlink           = New-Object System.Windows.Forms.CheckBox
    $ChBUnlink.Location  = New-Object System.Drawing.Size(10,47)
    $ChBUnlink.Size      = New-Object System.Drawing.Size(135,18)
    $ChBUnlink.FlatStyle = "Flat"
    $ChBUnlink.Text      = "Unlink this badge"

    #PictureBox Badge
    $PBBadge          = New-Object System.Windows.Forms.PictureBox
    $PBBadge.Location = New-Object System.Drawing.Size(130,23)
    $PBBadge.Size     = New-Object System.Drawing.Size(16,16)   
    $PBBadge.Image    = [System.Drawing.Image]::FromFile("$ScriptPath\images\cross.PNG")

    #---GroupBox User
    $GBUser          = New-Object System.Windows.Forms.GroupBox
    $GBUser.Location = New-Object System.Drawing.Size(10,77)
    $GBUser.Size     = New-Object System.Drawing.Size(158,53)
    $GBUser.Text     = "User"

    #UserName
    $TBUserName             = New-Object System.Windows.Forms.TextBox
    $TBUserName.Location    = New-Object System.Drawing.Size(10,20)
    $TBUserName.Size        = New-Object System.Drawing.Size(110)
    $TBUserName.BorderStyle = 1

    #PictureBox UserName
    $PBUserName           = New-Object System.Windows.Forms.PictureBox
    $PBUserName.Location  = New-Object System.Drawing.Size(130,23)
    $PBUserName.Size      = New-Object System.Drawing.Size(16,16)   
    $PBUserName.Image     = [System.Drawing.Image]::FromFile("$ScriptPath\images\cross.PNG")

    #---Buttons
    $BValidate           = New-Object System.Windows.Forms.Button
    $BValidate.Location  = New-Object System.Drawing.Size(10,139)
    $BValidate.Size      = New-Object System.Drawing.Size(75,23)
    $BValidate.FlatStyle = "PopUp"
    $BValidate.Text      = "Validate"

    $BConfirm           = New-Object System.Windows.Forms.Button
    $BConfirm.Location  = New-Object System.Drawing.Size(93,139)
    $BConfirm.Size      = New-Object System.Drawing.Size(75,23)
    $BConfirm.FlatStyle = "PopUp"
    $BConfirm.Text      = "Confirm"
    $BConfirm.Enabled   = $False

    #---Eventhandling
    $DisableTextBox=
    {
        #---"Unlink this badge" is unchecked
        if($ChBUnlink.Checked -eq $False)
        {
            #---Enable texbox
            $TBUserName.Enabled = $True

            #---Clear textbox
            $TBUserName.Text = ""

            #---Set both picture boxes to cross.png
            $PBBadge.Image = $PBUserName.Image = [System.Drawing.Image]::FromFile("$ScriptPath\images\cross.PNG")
        }

        #---"Unlink this badge" is checked
        if($ChBUnlink.Checked -eq $True)
        {
            #---Disable textbox
            $TBUserName.Enabled = $False

            #---Clear textbox
            $TBUserName.Text = ""

            #---Set the picture box next to the username input textbox to tick.png
            $PBUserName.Image = [System.Drawing.Image]::FromFile("$ScriptPath\images\tick.PNG")

            #---Setting badge check
            $BadgeChk = $False

            #---Set the picture box next to badge choice combobox to tick.png
            $PBBadge.Image = [System.Drawing.Image]::FromFile("$ScriptPath\images\cross.PNG")
        }
    }

    #---Buttons On-Click-Actions
    $BValidate_OnClick= 
    {   
        #---Param
        [String]$UserName = $TBUserName.Text             # -> Username

        [Int]$BadgeCSVID  = $CBBadgeChoice.SelectedIndex # -> Index which indicates which badge got selected: Index = 0 -> Visitor Badge 1, Index 1 -> Visitor Badge 2

        [Long]$BadgeID    = $BadgeList[$BadgeCSVID].ID   # -> Getting badge id from csv file 

        $Form.Enabled = $False

        Try
        {
            #---UserChk
            if($UserName -ne "")
            {
                $UserChk = Invoke-Command -Session $OurADServer -ScriptBlock{Param($UserName) [Bool](Get-ADUser -Filter { sAMAccountName -eq $UserName } -Searchbase "SB")} -ArgumentList $UserName

                if($UserChk -eq $False)
                {
                    $PBUserName.Image = [System.Drawing.Image]::FromFile("$ScriptPath\images\cross.PNG")
                }

                ElseIf($UserChk -eq $True)
                {
                    $PBUserName.Image = [System.Drawing.Image]::FromFile("$ScriptPath\images\tick.PNG")
                }
            }
        }

        Catch
        {
            DisplayMSGBox -MSGText "Could not validate the user." -AddErrorInfo $True | Out-Null

            LogFileCleanUp

            [System.Environment]::Exit(0)
        }

        Try
        {
            #---BadgeChk
            $BadgeChk = Invoke-Command -Session $OurADServer -ScriptBlock{Param($BadgeID) [Bool](Get-ADObject -Filter { Pager -eq $BadgeID })} -ArgumentList $BadgeID

            if($BadgeChk -eq $True)
            {   
                #---Retrieving all linked ad objects
                $AllLinkedObjectsArray = New-Object System.Collections.ArrayList

                $AllLinkedObjects = Invoke-Command -Session $OurADServer -ScriptBlock{Param($BadgeID) Get-ADObject -Filter { Pager -eq $BadgeID }} -ArgumentList $BadgeID

                ForEach($Object in $AllLinkedObjects)
                {
                    $AllLinkedObjectsArray.Add($Object.Name) | Out-Null
                }

                #---Joins array into string with breaks
                $AllLinkedObjectsString = $AllLinkedObjectsArray -Join "
    "

                if($ChBUnlink.Checked -eq $False)
                {
                    $PBBadge.Image = [System.Drawing.Image]::FromFile("$ScriptPath\images\cross.PNG")

                    if((DisplayMSGBox -MSGText ($CBBadgeChoice.SelectedItem + " is already linked with the following objects:`n`n$AllLinkedObjectsString`n`nDo you wish to clear these links?”) -MSGButtons YesNo -MSGIcon Question) -eq "Yes")
                    {
                        ReleaseVisitorBadge $AllLinkedObjectsString $BadgeCSVID $BadgeID
                    }
                }

                elseif($ChBUnlink.Checked -eq $True)
                {
                    $PBBadge.Image = [System.Drawing.Image]::FromFile("$ScriptPath\images\tick.PNG")
                }
            }

            elseif($BadgeChk -eq $False)
            {
                if($ChBUnlink.Checked -eq $False)
                {
                    $PBBadge.Image = [System.Drawing.Image]::FromFile("$ScriptPath\images\tick.PNG")
                }

                elseif($ChBUnlink.Checked -eq $True)
                {
                    $PBBadge.Image = [System.Drawing.Image]::FromFile("$ScriptPath\images\cross.PNG")

                    DisplayMSGBox -MSGText ($CBBadgeChoice.SelectedItem + " is currently not in use.") -MSGIcon Information | Out-Null
                }
            }
        }

        Catch
        {
            DisplayMSGBox -MSGText "Could not validate the badge." -AddErrorInfo $True | Out-Null

            LogFileCleanUp

            [System.Environment]::Exit(0)
        }

        $Form.Enabled = $True

        #---Enabling / disabling the button "Confirm"
        if(($BadgeChk -eq $False -and $UserChk -eq $True) -or ($BadgeChk -eq $True -and $ChBUnlink.Checked -eq $True))
        {
            $BConfirm.Enabled = $True
        }

        else
        {
            $BConfirm.Enabled = $False
        }
    }

    $BConfirm_OnClick= 
    {
        if($BadgeChk -eq $False -and $UserChk -eq $True)
        {
            Set-Variable -Name Exit -Value $False -Scope Global

            $Form.Close()

            SetVisitorBadge $UserName $BadgeCSVID $BadgeID
        }

        ElseIf($BadgeChk -eq $True -and $ChBUnlink.Checked -eq $True)
        {
            Set-Variable -Name Exit -Value $False -Scope Global

            $Form.Close()

            ReleaseVisitorBadge $AllLinkedObjectsString $BadgeCSVID $BadgeID
        }
    }

    #---Adding Elements to the form
    $BValidate.Add_Click($BValidate_OnClick)
    $BConfirm.Add_Click($BConfirm_OnClick)

    $ChBUnlink.Add_CheckedChanged($DisableTextBox)

    $Form.Add_Shown({$Form.Activate(); $TBUserName.Focus()})

    $GBBadges.Controls.Add($CBBadgeChoice)
    $GBBadges.Controls.Add($PBBadge)
    $GBBadges.Controls.Add($ChBUnlink)  

    $GBUser.Controls.Add($TBUserName)
    $GBUser.Controls.Add($PBUserName)

    $Form.Controls.Add($GBBadges)
    $Form.Controls.Add($GBUser)

    $Form.Controls.Add($BValidate)
    $Form.Controls.Add($BConfirm)

    $Form.ShowDialog()| Out-Null
}