我有一个带有用户表单的脚本,您可以使用该脚本编辑广告用户对象。
该脚本可以直接在我们的服务器上运行,但是当我尝试使用管理员权限从我的系统运行它时,New-PSSession
和一些Invoke-Command
的必要变量正在丢失它们的值。
这是我的代码(有些内容已被审查):
#--------------------#
# 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
}
我的userform中有两个选项。在用户输入数据后,他必须通过单击“验证”来验证它。如果一切正确,则会激活“确认”按钮,并且可以更改AD用户对象。
然而,在验证数据然后点击“确认”之后,所有必要的变量都会丢失它们的值,我不知道为什么。
如下所示,在验证数据后设置了所有内容,但是当我尝试确认数据时,所有值都消失了。
该脚本在我们的AD服务器上直接运行时工作得很好。我所做的只是尝试通过添加远程会话和Invoke-Command
来通过我的系统运行它。这真的是一个问题吗?
感谢您的帮助。