0

I have a main PS script to that passes variables to a dot script for my form but when I run the script using PS itself it says you cannot call a method on a null-valued expression, but it works fine in ISE. Could someone advise if there is a way of fixing this please?

Main Script

formSetting -formSizeW 300 -formSizeH 300 -formText "New Service Account" 
formLabel -labLocW 10 -labLocH 20 -labSizeW 280 -labSizeH 20 -labText "Enter SVC Account Name (Do Not Include SVC_)"
$accountName = formTextBox -tboxLocW 10 -tboxLocH 40 -tboxSizeW 260 -tboxSizeH 30
formLabel -labLocW 10 -labLocH 70 -labSizeW 280 -labSizeH 30 -labText "Select the Site name"
$site = formdrop -droplocW 10 -dropLocH 100  -dropSizeW 260 -dropSizeH 30 -dropInput $siteNames   
formLabel -labLocW 10 -labLocH 130 -labSizeW 280 -labSizeH 30 -labText "Enter a Password"
$password = formTextBox -mastbLocW 10 -mastbLocH 160 -mastbSizeW 260 -mastbSizeH 30 -asSecureString
$ok = formOKBut -okLocW 100 -okLocH 200 -okSizeW 60 -okSizeH 30 -okText "OK"
$checkBox = formCheckBox -checkLocW 30 -checkLocH 220 -checkSizeW 20 -checkSizeH 20 -checkText "Tick this"

dot script:

function formSetting ($formSizeW,$formSizeH,$formText) {
    $form = New-Object System.Windows.Forms.Form
    $form.Text = $formText
    $form.Size = New-Object System.Drawing.Size($formSizeW,$formSizeH) 
    $form.StartPosition = "CenterScreen"
}

function formLabel($labLocW,$labLocH,$labSizeW,$labSizeH,$labText) {
    $label = New-Object System.Windows.Forms.Label
    $label.Text = $labText
    $label.Location = New-Object System.Drawing.Point($labLocW,$labLocH) 
    $label.Size = New-Object System.Drawing.Size($LabSizeW,$labSizeH)
    $form.Controls.Add($label)
}

function formTextBox($tboxLocW,$tboxLocH,$tboxSizeW,$tboxSizeH) {
    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point($tboxLocW,$tboxLocH) 
    $textBox.Size = New-Object System.Drawing.Size($tboxSizeW,$tboxSizeH) 
    $form.Controls.Add($textBox)
    return $textBox
}

function formDrop($dropLocW,$dropLocH,$dropSizeW,$dropSizeH,$dropInput){
    $comboBox = New-Object System.Windows.Forms.ComboBox
    $comboBox.Location = New-Object System.Drawing.Point($dropLocW,$dropLocH)
    $comboBox.Size = New-Object System.Drawing.Size($dropSizeW,$dropSizeH)
    foreach($input in $dropInput) {
        $comboBox.Items.add($input) | Out-Null
    }
    $Form.Controls.Add($comboBox)
    return $comboBox
}

function formOKBut($okLocW,$okLocH,$okSizeW,$okSizeH,$okText) {
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point($okLocW,$okLocH)
    $OKButton.Size = New-Object System.Drawing.Size($okSizeW,$okSizeH)
    $OKButton.Text = $okText
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $OKButton
    $form.Controls.Add($OKButton)
    return $OKButton
}

function maskedTextBox($mastbLocW,$mastbLocH,$mastbSizeW,$mastbSizeH) {
    $MaskedTextBox = New-Object System.Windows.Forms.MaskedTextBox
    $MaskedTextBox.PasswordChar = '*'
    $MaskedTextBox.Top = $mastbLocH
    $MaskedTextBox.Left = $mastbLocW
    $MaskedTextBox.Width = $mastbSizeW
    $MaskedTextBox.Height = $mastbSizeH
    $form.Controls.Add($MaskedTextBox)
}

function formCheckBox($checkLocW,$checkLocH,$checkSizeW,$checkSizeH,$checkText) {
    $Checkbox = New-Object System.Windows.Forms.Checkbox 
    $Checkbox.Location = New-Object System.Drawing.Size($checkLocW,$checkLocH) 
    $Checkbox.Size = New-Object System.Drawing.Size($checkSizeW,$checkSizeH)
    $Checkbox.Text = $checkText
    $Checkbox.TabIndex = 4
    $Form.Controls.Add($Checkbox)
    return $Checkbox
}

enter image description here

1
  • 1
    Your $form is not defined in the context. Your problem with using the ISE is that variables live forever in the session. You need to define the elements outside of the functions or they stop existing outside of their scopes. Commented Mar 12, 2018 at 15:18

1 Answer 1

2

Redefine this function and it should work for you. Your problem is with scoping. Your objects don't exist outside of the function script block.

Function FormSetting ($formSizeW, $formSizeH, $formText)
{
    $form = New-Object System.Windows.Forms.Form
    $form.Text = $formText
    $form.Size = New-Object System.Drawing.Size($formSizeW,$formSizeH) 
    $form.StartPosition = "CenterScreen"

    Return $form
}

$Form = FormSetting -formSizeW 300 -formSizeH 300 -formText "New Service Account" 
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.