'Variable not carrying over to function
I've made a MDT tool for work that creates a computer and it needs to add the MDT LocationRoles to the computer.
I'm using a Combobox, where you can select the role and I'm using the SelectedIndexChanged to get the SelectedItem:
$comBox_LocationRole_SelectedIndexChanged = {
$locationRole = $comBox_LocationRole.SelectedItem
Write-Host $locationRole
}
I can see in the console, that it works fine and it writes out the location role.
I then have a function that creates the MDT computer object in the MDT database - which works just fine.
However, I'm not able to use my $locationRole in the function - it's just empty.
The part of the function where I use the $locationRole is here:
if(!($mdtComputer -match $args[0]))
{
$pinCode = Get-Random -Maximum 999999 -Minimum 100000
New-MDTComputer -assetTag $args[0] -description $args[0] -settings @{ OSInstall = 'YES'; SkipWizard = 'NO'; ServerA = $PinCode; OSDComputerName = $args[0]; ComputerName = $args[0] } | Out-Null
Get-MDTComputer -assetTag $args[0] | Select-Object -ExpandProperty ServerA
if($mdtComputerID -ne $null -and $locationRole -ne $null)
{
Clear-MDTComputerRole -Id $mdtComputerID
Set-MDTComputerRole -id $mdtComputerID -roles OS-Windows_10_Enterprise_X64,AP-Normal,$lbl_LocationRole.Text
}
Write-Host "Role : $locationRole"
}
Any help is much appreciated!
EDIT - someone wanted the full script
Script with functions:
$locationRole = ""
$comBox_LocationRole_SelectedIndexChanged = {
$script:locationRole = $comBox_LocationRole.SelectedItem
Write-Host $locationRole
}
$btn_DeleteMDTComputer_Click = {
Delete-MDTComputer
}
$btn_CreateMDTPinCode_Click = {
Create-MDTComputer
}
. (Join-Path $PSScriptRoot 'MDT_Pincode.designer.ps1')
#region method
function Create-MDTComputer
{
$lbl_Output.Text = ""
$computerName = $txtBox_ComputerName.Text
$user = $userAccount
$passwordFile = "\\$server\SWCTools\SWCScripts\MDT\PasswordNew.txt"
$keyFile = "\\$server\SWCTools\SWCScripts\MDT\AES.key"
$key = Get-Content $keyFile
$myCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $passwordFile | ConvertTo-SecureString -Key $key)
$remoteSession = Invoke-Command -ComputerName dklyscms1 -Credential $myCredential -ArgumentList $computerName -ScriptBlock {
Import-Module -Name "C:\MDT\MDT\MDTDB.psm1"
Connect-MDTDatabase -sqlServer $server -database MDT | Out-Null
$mdtComputer = Get-MDTComputer -assetTag $args[0] | Select-Object AssetTag
Write-Host $locationRole
Write-Host $role
if(!($mdtComputer -match $args[0]))
{
$pinCode = Get-Random -Maximum 999999 -Minimum 100000
New-MDTComputer -assetTag $args[0] -description $args[0] -settings @{ OSInstall = 'YES'; SkipWizard = 'NO'; ServerA = $PinCode; OSDComputerName = $args[0]; ComputerName = $args[0] } | Out-Null
Get-MDTComputer -assetTag $args[0] | Select-Object -ExpandProperty ServerA
$mdtComputerID = (Get-MDTComputer -assetTag $args[0]).ID
Write-Host "ID :"
Write-Host $mdtComputerID.ID
if($mdtComputerID -ne $null -and $locationRole -ne $null)
{
Clear-MDTComputerRole -Id $mdtComputerID
Set-MDTComputerRole -id $mdtComputerID -roles OS-Windows_10_Enterprise_X64,AP-Normal,$lbl_LocationRole.Text
}
$RoleText = $comBox_LocationRole.SelectedItem
Write-Host $RoleText
Write-Host "lbltext : $script:locationRole"
}
}
if($remoteSession)
{
$lbl_Output.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Verdana',[System.Single]46,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))
$lbl_Output.Text = $remoteSession
}
else
{
$lbl_Output.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Verdana',[System.Single]15.75,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))
$lbl_Output.Text = "Computer already exists"
}
}
Design:
[void][System.Reflection.Assembly]::Load('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][System.Reflection.Assembly]::Load('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
$MDT_Pincode = New-Object -TypeName System.Windows.Forms.Form
[System.Windows.Forms.Label]$label1 = $null
[System.Windows.Forms.Label]$lbl_Output = $null
[System.Windows.Forms.PictureBox]$pictureBox1 = $null
[System.Windows.Forms.TextBox]$txtBox_ComputerName = $null
[System.Windows.Forms.Button]$btn_CreateMDTPinCode = $null
[System.Windows.Forms.Button]$btn_DeleteMDTComputer = $null
[System.Windows.Forms.ComboBox]$comBox_LocationRole = $null
[System.Windows.Forms.Label]$lbl_LocationRole = $null
[System.Windows.Forms.Button]$button1 = $null
function InitializeComponent
{
$resources = . (Join-Path $PSScriptRoot 'MDT_Pincode.resources.ps1')
$label1 = (New-Object -TypeName System.Windows.Forms.Label)
$txtBox_ComputerName = (New-Object -TypeName System.Windows.Forms.TextBox)
$btn_CreateMDTPinCode = (New-Object -TypeName System.Windows.Forms.Button)
$btn_DeleteMDTComputer = (New-Object -TypeName System.Windows.Forms.Button)
$lbl_Output = (New-Object -TypeName System.Windows.Forms.Label)
$pictureBox1 = (New-Object -TypeName System.Windows.Forms.PictureBox)
$comBox_LocationRole = (New-Object -TypeName System.Windows.Forms.ComboBox)
$lbl_LocationRole = (New-Object -TypeName System.Windows.Forms.Label)
([System.ComponentModel.ISupportInitialize]$pictureBox1).BeginInit()
$MDT_Pincode.SuspendLayout()
#
#label1
#
$label1.AutoSize = $true
$label1.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Verdana',[System.Single]8.25,[System.Drawing.FontStyle]::Bold,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))
$label1.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]26,[System.Int32]79))
$label1.Name = [System.String]'label1'
$label1.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]81,[System.Int32]13))
$label1.TabIndex = [System.Int32]0
$label1.Text = [System.String]'PC number:'
#
#txtBox_ComputerName
#
$txtBox_ComputerName.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
$txtBox_ComputerName.CharacterCasing = [System.Windows.Forms.CharacterCasing]::Upper
$txtBox_ComputerName.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]26,[System.Int32]99))
$txtBox_ComputerName.Name = [System.String]'txtBox_ComputerName'
$txtBox_ComputerName.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]99,[System.Int32]21))
$txtBox_ComputerName.TabIndex = [System.Int32]1
#
#btn_CreateMDTPinCode
#
$btn_CreateMDTPinCode.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$btn_CreateMDTPinCode.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]314,[System.Int32]99))
$btn_CreateMDTPinCode.Name = [System.String]'btn_CreateMDTPinCode'
$btn_CreateMDTPinCode.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]75,[System.Int32]23))
$btn_CreateMDTPinCode.TabIndex = [System.Int32]2
$btn_CreateMDTPinCode.Text = [System.String]'Create'
$btn_CreateMDTPinCode.UseVisualStyleBackColor = $true
$btn_CreateMDTPinCode.add_Click($btn_CreateMDTPinCode_Click)
#
#btn_DeleteMDTComputer
#
$btn_DeleteMDTComputer.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$btn_DeleteMDTComputer.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]233,[System.Int32]99))
$btn_DeleteMDTComputer.Name = [System.String]'btn_DeleteMDTComputer'
$btn_DeleteMDTComputer.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]75,[System.Int32]23))
$btn_DeleteMDTComputer.TabIndex = [System.Int32]3
$btn_DeleteMDTComputer.Text = [System.String]'Delete'
$btn_DeleteMDTComputer.UseVisualStyleBackColor = $true
$btn_DeleteMDTComputer.add_Click($btn_DeleteMDTComputer_Click)
#
#lbl_Output
#
$lbl_Output.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Verdana',[System.Single]15.75,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))
$lbl_Output.ForeColor = [System.Drawing.Color]::FromArgb(([System.Int32]([System.Byte][System.Byte]240)),([System.Int32]([System.Byte][System.Byte]78)),([System.Int32]([System.Byte][System.Byte]35)))
$lbl_Output.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12,[System.Int32]151))
$lbl_Output.Name = [System.String]'lbl_Output'
$lbl_Output.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]388,[System.Int32]61))
$lbl_Output.TabIndex = [System.Int32]5
$lbl_Output.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
#
#pictureBox1
#
$pictureBox1.Image = ([System.Drawing.Image]$resources.'pictureBox1.Image')
$pictureBox1.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]-5,[System.Int32]-5))
$pictureBox1.Name = [System.String]'pictureBox1'
$pictureBox1.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]418,[System.Int32]53))
$pictureBox1.TabIndex = [System.Int32]6
$pictureBox1.TabStop = $false
#
#comBox_LocationRole
#
$comBox_LocationRole.FlatStyle = [System.Windows.Forms.FlatStyle]::Popup
$comBox_LocationRole.FormattingEnabled = $true
$comBox_LocationRole.Items.AddRange([System.Object[]]@([System.String]'LT-Norway',[System.String]'LT-Denmark',[System.String]'LT-Sweden',[System.String]'LT-LITHUANIA',[System.String]'LT-Belgium',[System.String]'LT-UK_Flint_Niell',[System.String]'LT-US_OCC',[System.String]'LT-US_Ben_C_Gerwick',[System.String]'LT-CA_Buckland_Taylor',[System.String]'LT-US_JENNY',[System.String]'LT-Oman',[System.String]'LT-Qatar',[System.String]'LT-INDIA',[System.String]'LT-Korea',[System.String]'LT-UK',[System.String]'LT-Poland',[System.String]'LT-Uganda',[System.String]'LT-Singapore',[System.String]'LT-Denmark_Arkitema',[System.String]'LT-Norway_Arkitema',[System.String]'LT-Sweden_Arkitema',[System.String]'LT-Canada',[System.String]'LT-United_States'))
$comBox_LocationRole.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]132,[System.Int32]99))
$comBox_LocationRole.Name = [System.String]'comBox_LocationRole'
$comBox_LocationRole.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]95,[System.Int32]21))
$comBox_LocationRole.TabIndex = [System.Int32]7
$comBox_LocationRole.add_SelectedIndexChanged($comBox_LocationRole_SelectedIndexChanged)
#
#lbl_LocationRole
#
$lbl_LocationRole.AutoSize = $true
$lbl_LocationRole.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Verdana',[System.Single]8.25,[System.Drawing.FontStyle]::Bold,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))
$lbl_LocationRole.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]129,[System.Int32]79))
$lbl_LocationRole.Name = [System.String]'lbl_LocationRole'
$lbl_LocationRole.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]39,[System.Int32]13))
$lbl_LocationRole.TabIndex = [System.Int32]8
$lbl_LocationRole.Text = [System.String]'Role:'
#
#MDT_Pincode
#
$MDT_Pincode.ClientSize = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]412,[System.Int32]236))
$MDT_Pincode.Controls.Add($lbl_LocationRole)
$MDT_Pincode.Controls.Add($comBox_LocationRole)
$MDT_Pincode.Controls.Add($pictureBox1)
$MDT_Pincode.Controls.Add($lbl_Output)
$MDT_Pincode.Controls.Add($btn_DeleteMDTComputer)
$MDT_Pincode.Controls.Add($btn_CreateMDTPinCode)
$MDT_Pincode.Controls.Add($txtBox_ComputerName)
$MDT_Pincode.Controls.Add($label1)
$MDT_Pincode.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Verdana',[System.Single]8.25,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))
$MDT_Pincode.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$MDT_Pincode.Icon = ([System.Drawing.Icon]$resources.'$this.Icon')
$MDT_Pincode.MaximizeBox = $false
$MDT_Pincode.Name = [System.String]'MDT_Pincode'
$MDT_Pincode.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$MDT_Pincode.Text = [System.String]'MDT Pincode Manager'
$MDT_Pincode.add_Load($MDT_Pincode_Load)
([System.ComponentModel.ISupportInitialize]$pictureBox1).EndInit()
$MDT_Pincode.ResumeLayout($false)
$MDT_Pincode.PerformLayout()
Add-Member -InputObject $MDT_Pincode -Name base -Value $base -MemberType NoteProperty
Add-Member -InputObject $MDT_Pincode -Name label1 -Value $label1 -MemberType NoteProperty
Add-Member -InputObject $MDT_Pincode -Name lbl_Output -Value $lbl_Output -MemberType NoteProperty
Add-Member -InputObject $MDT_Pincode -Name pictureBox1 -Value $pictureBox1 -MemberType NoteProperty
Add-Member -InputObject $MDT_Pincode -Name txtBox_ComputerName -Value $txtBox_ComputerName -MemberType NoteProperty
Add-Member -InputObject $MDT_Pincode -Name btn_CreateMDTPinCode -Value $btn_CreateMDTPinCode -MemberType NoteProperty
Add-Member -InputObject $MDT_Pincode -Name btn_DeleteMDTComputer -Value $btn_DeleteMDTComputer -MemberType NoteProperty
Add-Member -InputObject $MDT_Pincode -Name comBox_LocationRole -Value $comBox_LocationRole -MemberType NoteProperty
Add-Member -InputObject $MDT_Pincode -Name lbl_LocationRole -Value $lbl_LocationRole -MemberType NoteProperty
Add-Member -InputObject $MDT_Pincode -Name button1 -Value $button1 -MemberType NoteProperty
}
. InitializeComponent
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
