A
A
Alexander Pavlov2019-11-20 09:12:34
PowerShell
Alexander Pavlov, 2019-11-20 09:12:34

Why is the variable not being passed to powershell?

Hello.
I have the following script code in powershell:

function Generate-Form {
    Add-Type -assembly System.Windows.Forms
    Import-Module activedirectory

    #Создание формы
    $window_form = New-Object System.Windows.Forms.Form
    $window_form.Text ='Сбор информации о ПК компании'
    $window_form.Width = 500
    $window_form.Height = 700
    $window_form.AutoSize = $true

    #Создания подписи
    $FormLabel1 = New-Object System.Windows.Forms.Label
    $FormLabel1.Text = "Вставьте название контейнера магазина"
    $FormLabel1.Location = New-Object System.Drawing.Point(0,10)
    $FormLabel1.AutoSize = $true
    $window_form.Controls.Add($FormLabel1)

    #Создание поля для ввода
    $formTextBox = New-Object System.Windows.Forms.TextBox
    $formTextBox.Location = New-Object System.Drawing.Point(0,50)
    $formTextBox.Size = New-Object System.Drawing.Size(150,60)
    $formTextBox.AutoSize = $true
    $formTextBox.Text = "SiteName"
    $window_form.Controls.Add($formTextBox)

    #Создание кнопки "Запросить"
    $FormButton = New-Object System.Windows.Forms.Button
    $FormButton.Location = New-Object System.Drawing.Size(400,10)
    $FormButton.Size = New-Object System.Drawing.Size(100,20)
    $FormButton.Text = "Запросить"
    $window_form.Controls.Add($FormButton)

    $FormButton.Add_Click({
        Get-ADComputer -Filter * -SearchBase 'OU=Computers,OU="$formTextBox.Text",DC=domainru,DC=ru,DC=example,DC=com' | Select-Object Name | Export-Csv -Path C:\TEMP\mysite_temp.txt -Encoding UTF8
    })

    $window_form.ShowDialog()


}
Generate-Form

When you click on the "Request" button, it gives an error:
Get-ADComputer : Directory object not found
If you manually insert SiteName in place of the container, all the rules are worked out.
How to pass SiteName to Get-ADComputer -Filter * -SearchBase 'OU=Computers,OU="$formTextBox.Text",DC=domainru,DC=ru,DC=example,DC=com' ??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2019-11-20
@Sanchez9891

You have incorrect quotes in the command 'OU=Computers,OU="$formTextBox.Text",DC=domainru,DC=ru,DC=example,DC=com'
When using such quotes ' ' everything inside them will be processed as text data, and when using such " ", if there is a variable inside, it will be substituted.
In your case, you need to do this
: "OU=Computers,OU=$formTextBox.Text,DC=domainru,DC=ru,DC=example,DC=com"
Also, check that the path to the objects you are interested in is correct.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question